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

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

fixed and off-by-one error

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 //System.out.println( "Total written: " + written + " bytes" );
117
118 try {
119 is.close();
120 } catch ( IOException e ) {
121 throw new BuildException( "Error - couldn't close the input stream" );
122 }
123
124
125 //if they have specified a resource script and resource name, write an rc file
126 if ( resourceScript != null && resourceName != null && resourceType != null ) {
127 System.out.println( "Putting references into a resource script" );
128 System.out.println( "resourceScript: " + resourceScript.getAbsolutePath() );
129 System.out.println( "resourceName : " + resourceName );
130 System.out.println( "resourceType : " + resourceType );
131
132 //open the resource script
133 PrintWriter out = null;
134 try {
135 out = new PrintWriter(new FileWriter( resourceScript, true ));
136 } catch ( IOException e ) {
137 throw new BuildException( "Error - couldn't open the resource script" );
138 }
139
140 //put a line in for each chunk
141 for ( int i=0; i<noChunks; i++ ){
142 out.println(
143 resourceName + "_" + (i+1) + " " + resourceType + " \"" + resource.getName() + "." + i + "\""
144 );
145 }
146
147 //close the resource script
148 out.close();
149 }
150 }
151
152
153 public void setResource(File resource) {
154 this.resource = resource;
155 }
156
157 public void setResourceScript(File resourceScript) {
158 this.resourceScript = resourceScript;
159 }
160
161 public void setResourceName(String resourceName) {
162 this.resourceName = resourceName;
163 }
164 public void setResourceType(String resourceType) {
165 this.resourceType = resourceType;
166 }
167
168 public void setOutputDir(File outputDir) {
169 this.outputDir = outputDir;
170 }
171
172 public void setChunkSize(int chunkSize) {
173 this.chunkSize = chunkSize;
174 }
175
176}
Note: See TracBrowser for help on using the repository browser.