source: other-projects/trunk/anttasks/src/org/greenstone/anttasks/SplitResource.java@ 17089

Last change on this file since 17089 was 17089, checked in by oranfry, 16 years ago

moving shared ant tasks to a shared area

File size: 5.8 KB
Line 
1/*
2 * SplitResource.java
3 * A task to split a file into chunks and optionally add references to those
4 * chunks in an rc (resource compiler) script
5 *
6 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23package org.greenstone.anttasks;
24
25import org.apache.tools.ant.*;
26import org.apache.tools.ant.taskdefs.*;
27import java.util.*;
28import java.io.*;
29import java.util.regex.*;
30
31/**
32 * Splits a file (resource) into chunks of a given size and puts them in files
33 * with the same name as the resource but appended with a dot followed by the chunk number
34 */
35public class SplitResource extends Task {
36
37 private File resource = null;
38 private File resourceScript = null;
39 private File outputDir = null;
40 private String resourceName = null;
41 private String resourceType = null;
42 private int chunkSize = 0;
43
44
45 /**
46 * for testing
47 */
48 public static void main( String[] args ) {
49
50 SplitResource sr = new SplitResource();
51 sr.setResource( new File(args[0]) );
52 sr.setOutputDir( new File(args[1]) );
53 sr.setResourceScript( new File(args[2]) );
54 sr.setResourceName( args[3] );
55 sr.setResourceType( args[4] );
56 sr.setChunkSize( Integer.parseInt(args[5]) );
57 sr.execute();
58
59 }
60
61 public void execute() {
62
63 if ( resource == null ) {
64 throw new BuildException( "Error - No resource specified !!" );
65 }
66
67 if ( outputDir == null ) {
68 outputDir = new File(".");
69 }
70 outputDir.mkdir();
71
72 if ( chunkSize == 0 ) {
73 throw new BuildException( "Error - No chunk size specified !!" );
74 }
75
76 DataInputStream is;
77
78 try {
79 is = new DataInputStream( new FileInputStream( resource ) );
80 } catch ( IOException e ) {
81 throw new BuildException( "Error - couldn't open the resource file" );
82 }
83
84 //figure out how many chunks to do this in
85 int noChunks = (int)(resource.length() / chunkSize) + 1;
86
87 System.out.println( "Splitting file into chunks" );
88 System.out.println( "file: " + resource.getAbsolutePath() );
89 System.out.println( "file size: " + resource.length() );
90 System.out.println( "chunk size: " + chunkSize );
91 System.out.println( "ergo, num chunks: " + noChunks );
92
93 long written = 0;
94 for ( int i=0; i<noChunks; i++ ) {
95
96 //open a file for output
97 DataOutputStream os;
98 File partFile = new File( outputDir, resource.getName() + "." + i );
99 //System.out.println( "partFile: " + partFile );
100
101 try {
102 os = new DataOutputStream( new FileOutputStream( partFile ) );
103 } catch ( IOException e ) {
104 throw new BuildException( "Error - couldn't create a part file" );
105 }
106
107
108 //sort out thisChunkSize
109 int thisChunkSize = chunkSize;
110 if ( i == noChunks - 1 ) {
111 //System.out.println( "(Last Chunk)");
112 thisChunkSize = (int)(resource.length() % chunkSize);
113 }
114
115 //System.out.println( "Read a chunk (" + thisChunkSize + " bytes)" );
116 byte[] data = new byte[thisChunkSize];
117 try {
118 is.readFully( data, 0, thisChunkSize );
119 } catch( EOFException eof ) {
120 throw new BuildException( "Error - unexpectedly hit the end of the resource file" );
121 } catch ( IOException e ) {
122 throw new BuildException( "Error - couldn't read a byte from the resource file" );
123 }
124
125 //System.out.println( "Write to the file");
126 try {
127 os.write( data, 0, thisChunkSize );
128 written += thisChunkSize;
129 } catch ( IOException e ) {
130 throw new BuildException( "Error - couldn't write out a byte to the part file" );
131 }
132
133 try {
134 os.close();
135 } catch ( IOException e ) {
136 throw new BuildException( "Error - couldn't close a chunk output stream" );
137 }
138 }
139
140 //System.out.println( "Total written: " + written + " bytes" );
141
142 try {
143 is.close();
144 } catch ( IOException e ) {
145 throw new BuildException( "Error - couldn't close the input stream" );
146 }
147
148
149 //if they have specified a resource script and resource name, write an rc file
150 if ( resourceScript != null && resourceName != null && resourceType != null ) {
151 System.out.println( "Putting references into a resource script" );
152 System.out.println( "resourceScript: " + resourceScript.getAbsolutePath() );
153 System.out.println( "resourceName : " + resourceName );
154 System.out.println( "resourceType : " + resourceType );
155
156 //open the resource script
157 PrintWriter out = null;
158 try {
159 out = new PrintWriter(new FileWriter( resourceScript, true ));
160 } catch ( IOException e ) {
161 throw new BuildException( "Error - couldn't open the resource script" );
162 }
163
164 //put a line in for each chunk
165 for ( int i=0; i<noChunks; i++ ){
166 out.println(
167 resourceName + "_" + (i+1) + " " + resourceType + " \"" + resource.getName() + "." + i + "\""
168 );
169 }
170
171 //close the resource script
172 out.close();
173 }
174 }
175
176
177 public void setResource(File resource) {
178 this.resource = resource;
179 }
180
181 public void setResourceScript(File resourceScript) {
182 this.resourceScript = resourceScript;
183 }
184
185 public void setResourceName(String resourceName) {
186 this.resourceName = resourceName;
187 }
188 public void setResourceType(String resourceType) {
189 this.resourceType = resourceType;
190 }
191
192 public void setOutputDir(File outputDir) {
193 this.outputDir = outputDir;
194 }
195
196 public void setChunkSize(int chunkSize) {
197 this.chunkSize = chunkSize;
198 }
199
200}
Note: See TracBrowser for help on using the repository browser.