source: release-kits/shared/ant-tasks/orans/SplitResource.java@ 15885

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

made the splitter task produce less output

File size: 4.8 KB
Line 
1import org.apache.tools.ant.*;
2import org.apache.tools.ant.taskdefs.*;
3import java.util.*;
4import java.io.*;
5import java.util.regex.*;
6
7/**
8 * Splits a file (resource) into chunks of a given size and puts them in files
9 * with the same name as the resource but appended with a dot followed by the chunk number
10 */
11public class SplitResource extends Task {
12
13 private File resource = null;
14 private File resourceScript = null;
15 private File outputDir = null;
16 private String resourceName = null;
17 private String resourceType = null;
18 private int chunkSize = 0;
19
20
21 /**
22 * for testing
23 */
24 public static void main( String[] args ) {
25
26 SplitResource sr = new SplitResource();
27 sr.setResource( new File(args[0]) );
28 sr.setOutputDir( new File(args[1]) );
29 sr.setResourceScript( new File(args[2]) );
30 sr.setResourceName( args[3] );
31 sr.setResourceType( args[4] );
32 sr.setChunkSize( Integer.parseInt(args[5]) );
33 sr.execute();
34
35 }
36
37 public void execute() {
38
39 if ( resource == null ) {
40 throw new BuildException( "Error - No resource specified !!" );
41 }
42
43 if ( outputDir == null ) {
44 outputDir = new File(".");
45 }
46 outputDir.mkdir();
47
48 if ( chunkSize == 0 ) {
49 throw new BuildException( "Error - No chunk size specified !!" );
50 }
51
52 DataInputStream is;
53
54 try {
55 is = new DataInputStream( new FileInputStream( resource ) );
56 } catch ( IOException e ) {
57 throw new BuildException( "Error - couldn't open the resource file" );
58 }
59
60 //figure out how many chunks to do this in
61 int noChunks = (int)(resource.length() / chunkSize) + 1;
62
63 System.out.println( "Splitting file into chunks" );
64 System.out.println( "file: " + resource.getAbsolutePath() );
65 System.out.println( "file size: " + resource.length() );
66 System.out.println( "chunk size: " + chunkSize );
67 System.out.println( "ergo, num chunks: " + noChunks );
68
69 long written = 0;
70 for ( int i=0; i<noChunks; i++ ) {
71
72 //open a file for output
73 DataOutputStream os;
74 File partFile = new File( outputDir, resource.getName() + "." + i );
75 //System.out.println( "partFile: " + partFile );
76
77 try {
78 os = new DataOutputStream( new FileOutputStream( partFile ) );
79 } catch ( IOException e ) {
80 throw new BuildException( "Error - couldn't create a part file" );
81 }
82
83
84 //sort out thisChunkSize
85 int thisChunkSize = chunkSize;
86 if ( i == noChunks - 1 ) {
87 //System.out.println( "(Last Chunk)");
88 thisChunkSize = (int)(resource.length() % chunkSize);
89 }
90
91 //System.out.println( "Read a chunk (" + thisChunkSize + " bytes)" );
92 byte[] data = new byte[thisChunkSize];
93 try {
94 is.readFully( data, 0, thisChunkSize );
95 } catch( EOFException eof ) {
96 throw new BuildException( "Error - unexpectedly hit the end of the resource file" );
97 } catch ( IOException e ) {
98 throw new BuildException( "Error - couldn't read a byte from the resource file" );
99 }
100
101 //System.out.println( "Write to the file");
102 try {
103 os.write( data, 0, thisChunkSize );
104 written += thisChunkSize;
105 } catch ( IOException e ) {
106 throw new BuildException( "Error - couldn't write out a byte to the part file" );
107 }
108
109 try {
110 os.close();
111 } catch ( IOException e ) {
112 throw new BuildException( "Error - couldn't close a chunk output stream" );
113 }
114
115 }
116
117 //System.out.println( "Total written: " + written + " bytes" );
118
119 try {
120 is.close();
121 } catch ( IOException e ) {
122 throw new BuildException( "Error - couldn't close the input stream" );
123 }
124
125
126 //if they have specified a resource script and resource name, write an rc file
127 if ( resourceScript != null && resourceName != null && resourceType != null ) {
128 System.out.println( "Putting references into a resource script" );
129 System.out.println( "resourceScript: " + resourceScript.getAbsolutePath() );
130 System.out.println( "resourceName : " + resourceName );
131 System.out.println( "resourceType : " + resourceType );
132
133 //open the resource script
134 PrintWriter out = null;
135 try {
136 out = new PrintWriter(new FileWriter( resourceScript, true ));
137 } catch ( IOException e ) {
138 throw new BuildException( "Error - couldn't open the resource script" );
139 }
140
141 //put a line in for each chunk
142 for ( int i=0; i<noChunks; i++ ){
143 out.println(
144 resourceName + "_" + i + " " + resourceType + " \"" + resource.getName() + "." + i + "\""
145 );
146 }
147
148 //close the resource script
149 out.close();
150
151 }
152
153 }
154
155
156 public void setResource(File resource) {
157 this.resource = resource;
158 }
159
160 public void setResourceScript(File resourceScript) {
161 this.resourceScript = resourceScript;
162 }
163
164 public void setResourceName(String resourceName) {
165 this.resourceName = resourceName;
166 }
167 public void setResourceType(String resourceType) {
168 this.resourceType = resourceType;
169 }
170
171 public void setOutputDir(File outputDir) {
172 this.outputDir = outputDir;
173 }
174
175 public void setChunkSize(int chunkSize) {
176 this.chunkSize = chunkSize;
177 }
178
179}
Note: See TracBrowser for help on using the repository browser.