Lines 121-126
Link Here
|
121 |
public static final int INITIAL_HEADER_SIZE = 1500; |
121 |
public static final int INITIAL_HEADER_SIZE = 1500; |
122 |
public static final int INNER_CLASSES_SIZE = 5; |
122 |
public static final int INNER_CLASSES_SIZE = 5; |
123 |
|
123 |
|
|
|
124 |
private static int MAX_TIME = -1; |
125 |
private static final int MAX_COUNT = 50; |
124 |
/** |
126 |
/** |
125 |
* INTERNAL USE-ONLY |
127 |
* INTERNAL USE-ONLY |
126 |
* Build all the directories and subdirectories corresponding to the packages names |
128 |
* Build all the directories and subdirectories corresponding to the packages names |
Lines 142-155
Link Here
|
142 |
char fileSeparatorChar = File.separatorChar; |
144 |
char fileSeparatorChar = File.separatorChar; |
143 |
String fileSeparator = File.separator; |
145 |
String fileSeparator = File.separator; |
144 |
File f; |
146 |
File f; |
145 |
outputPath = outputPath.replace('/', fileSeparatorChar); |
147 |
if (fileSeparatorChar != '/') { |
146 |
// these could be optimized out if we normalized paths once and for |
148 |
// these could be optimized out if we normalized paths once and for all |
147 |
// all |
149 |
outputPath = outputPath.replace('/', fileSeparatorChar); |
148 |
relativeFileName = relativeFileName.replace('/', fileSeparatorChar); |
150 |
relativeFileName = relativeFileName.replace('/', fileSeparatorChar); |
|
|
151 |
} |
149 |
String outputDirPath, fileName; |
152 |
String outputDirPath, fileName; |
150 |
int separatorIndex = relativeFileName.lastIndexOf(fileSeparatorChar); |
153 |
int separatorIndex = relativeFileName.lastIndexOf(fileSeparatorChar); |
|
|
154 |
boolean pathEndsWithSep = outputPath.endsWith(fileSeparator); |
151 |
if (separatorIndex == -1) { |
155 |
if (separatorIndex == -1) { |
152 |
if (outputPath.endsWith(fileSeparator)) { |
156 |
if (pathEndsWithSep) { |
153 |
outputDirPath = outputPath.substring(0, outputPath.length() - 1); |
157 |
outputDirPath = outputPath.substring(0, outputPath.length() - 1); |
154 |
fileName = outputPath + relativeFileName; |
158 |
fileName = outputPath + relativeFileName; |
155 |
} else { |
159 |
} else { |
Lines 157-169
Link Here
|
157 |
fileName = outputPath + fileSeparator + relativeFileName; |
161 |
fileName = outputPath + fileSeparator + relativeFileName; |
158 |
} |
162 |
} |
159 |
} else { |
163 |
} else { |
160 |
if (outputPath.endsWith(fileSeparator)) { |
164 |
if (pathEndsWithSep) { |
161 |
outputDirPath = outputPath + |
165 |
outputDirPath = outputPath + relativeFileName.substring(0, separatorIndex); |
162 |
relativeFileName.substring(0, separatorIndex); |
|
|
163 |
fileName = outputPath + relativeFileName; |
166 |
fileName = outputPath + relativeFileName; |
164 |
} else { |
167 |
} else { |
165 |
outputDirPath = outputPath + fileSeparator + |
168 |
outputDirPath = outputPath + fileSeparator + relativeFileName.substring(0, separatorIndex); |
166 |
relativeFileName.substring(0, separatorIndex); |
|
|
167 |
fileName = outputPath + fileSeparator + relativeFileName; |
169 |
fileName = outputPath + fileSeparator + relativeFileName; |
168 |
} |
170 |
} |
169 |
} |
171 |
} |
Lines 171-243
Link Here
|
171 |
f.mkdirs(); |
173 |
f.mkdirs(); |
172 |
if (f.isDirectory()) { |
174 |
if (f.isDirectory()) { |
173 |
return fileName; |
175 |
return fileName; |
|
|
176 |
} |
177 |
|
178 |
// the directory creation failed for some reason - retry using |
179 |
// a slower algorithm so as to refine the diagnostic |
180 |
// First create the output dir |
181 |
if (pathEndsWithSep) { |
182 |
outputPath = outputPath.substring(0, outputPath.length() - 1); |
183 |
} |
184 |
f = new File(outputPath); |
185 |
boolean checkFileType = false; |
186 |
if (f.exists()) { |
187 |
checkFileType = true; // pre-existed |
174 |
} else { |
188 |
} else { |
175 |
// the directory creation failed for some reason - retry using |
189 |
// we have to create that directory |
176 |
// a slower algorithm so as to refine the diagnostic |
190 |
int count = 0; |
177 |
if (outputPath.endsWith(fileSeparator)) { |
191 |
while (!f.mkdirs() && count < MAX_COUNT) { |
178 |
outputPath = outputPath.substring(0, outputPath.length() - 1); |
192 |
if (f.exists()) { |
|
|
193 |
// someone else created f -- need to check its type |
194 |
checkFileType = true; |
195 |
break; |
196 |
} |
197 |
try { |
198 |
Thread.sleep(10); |
199 |
count++; |
200 |
} catch (InterruptedException e) { |
201 |
break; |
202 |
} |
179 |
} |
203 |
} |
180 |
f = new File(outputPath); |
204 |
if (!f.exists()) { |
181 |
boolean checkFileType = false; |
205 |
System.out.println("ClassFile: output directory "+f+" does not exist after creation!"); |
|
|
206 |
// no one could create f -- complain |
207 |
throw new IOException(Messages.bind( |
208 |
Messages.output_notValidAll, f.getAbsolutePath())); |
209 |
} |
210 |
if (count > 0) { |
211 |
int time = count * 10; |
212 |
if (time > MAX_TIME) MAX_TIME = time; |
213 |
System.out.println("ClassFile: takes "+time+"ms (max="+MAX_TIME+"ms) before the output directory "+f+" was created..."); |
214 |
} |
215 |
} |
216 |
if (checkFileType) { |
217 |
if (!f.isDirectory()) { |
218 |
System.out.println("ClassFile: output "+f+" is not a directory!"); |
219 |
throw new IOException(Messages.bind( |
220 |
Messages.output_isFile, f.getAbsolutePath())); |
221 |
} |
222 |
} |
223 |
|
224 |
// Then create sub-directories |
225 |
StringBuffer outDir = new StringBuffer(outputPath); |
226 |
outDir.append(fileSeparator); |
227 |
StringTokenizer tokenizer = new StringTokenizer(relativeFileName, fileSeparator); |
228 |
String token = tokenizer.nextToken(); |
229 |
while (tokenizer.hasMoreTokens()) { |
230 |
f = new File(outDir.append(token).append(fileSeparator).toString()); |
231 |
checkFileType = false; // reset |
182 |
if (f.exists()) { |
232 |
if (f.exists()) { |
183 |
checkFileType = true; // pre-existed |
233 |
checkFileType = true; // this is suboptimal, but it catches corner cases |
|
|
234 |
// in which a regular file pre-exists |
184 |
} else { |
235 |
} else { |
185 |
// we have to create that directory |
236 |
// we have to create that directory |
186 |
if (!f.mkdirs()) { |
237 |
int count = 0; |
187 |
if (f.exists()) { |
238 |
while (!f.mkdir() && count < MAX_COUNT) { |
188 |
// someone else created f -- need to check its type |
239 |
if (f.exists()) { |
189 |
checkFileType = true; |
240 |
// someone else created f -- need to check its type |
190 |
} else { |
241 |
checkFileType = true; |
191 |
// no one could create f -- complain |
242 |
break; |
192 |
throw new IOException(Messages.bind( |
243 |
} |
193 |
Messages.output_notValidAll, f.getAbsolutePath())); |
244 |
try { |
194 |
} |
245 |
// need to try again some times as some OS can take some times |
195 |
} |
246 |
// before the directory really exists... |
196 |
} |
247 |
Thread.sleep(10); |
197 |
if (checkFileType) { |
248 |
count++; |
198 |
if (!f.isDirectory()) { |
249 |
} catch (InterruptedException e) { |
199 |
throw new IOException(Messages.bind( |
250 |
break; |
200 |
Messages.output_isFile, f.getAbsolutePath())); |
251 |
} |
|
|
252 |
} |
253 |
if (!f.exists()) { |
254 |
System.out.println("ClassFile: output sub-directory "+f+" does not exist after creation!"); |
255 |
// no one could create f -- complain |
256 |
throw new IOException(Messages.bind( |
257 |
Messages.output_notValid, |
258 |
outDir.substring(outputPath.length() + 1, |
259 |
outDir.length() - 1), |
260 |
outputPath)); |
201 |
} |
261 |
} |
|
|
262 |
if (count > 0) { |
263 |
int time = count * 10; |
264 |
if (time > MAX_TIME) MAX_TIME = time; |
265 |
System.out.println("ClassFile: takes "+time+"ms (max="+MAX_TIME+"ms) before the output sub-directory "+f+" was created..."); |
266 |
} |
202 |
} |
267 |
} |
203 |
StringBuffer outDir = new StringBuffer(outputPath); |
268 |
if (checkFileType) { |
204 |
outDir.append(fileSeparator); |
269 |
if (!f.isDirectory()) { |
205 |
StringTokenizer tokenizer = |
270 |
System.out.println("ClassFile: sub output "+f+" is not a directory!"); |
206 |
new StringTokenizer(relativeFileName, fileSeparator); |
271 |
throw new IOException(Messages.bind( |
207 |
String token = tokenizer.nextToken(); |
272 |
Messages.output_isFile, f.getAbsolutePath())); |
208 |
while (tokenizer.hasMoreTokens()) { |
273 |
} |
209 |
f = new File(outDir.append(token).append(fileSeparator).toString()); |
274 |
} |
210 |
checkFileType = false; // reset |
275 |
token = tokenizer.nextToken(); |
211 |
if (f.exists()) { |
|
|
212 |
checkFileType = true; // this is suboptimal, but it catches corner cases |
213 |
// in which a regular file pre-exists |
214 |
} else { |
215 |
// we have to create that directory |
216 |
if (!f.mkdir()) { |
217 |
if (f.exists()) { |
218 |
// someone else created f -- need to check its type |
219 |
checkFileType = true; |
220 |
} else { |
221 |
// no one could create f -- complain |
222 |
throw new IOException(Messages.bind( |
223 |
Messages.output_notValid, |
224 |
outDir.substring(outputPath.length() + 1, |
225 |
outDir.length() - 1), |
226 |
outputPath)); |
227 |
} |
228 |
} |
229 |
} |
230 |
if (checkFileType) { |
231 |
if (!f.isDirectory()) { |
232 |
throw new IOException(Messages.bind( |
233 |
Messages.output_isFile, f.getAbsolutePath())); |
234 |
} |
235 |
} |
236 |
token = tokenizer.nextToken(); |
237 |
} |
238 |
// token contains the last one |
239 |
return outDir.append(token).toString(); |
240 |
} |
276 |
} |
|
|
277 |
// token contains the last one |
278 |
return outDir.append(token).toString(); |
241 |
} |
279 |
} |
242 |
/** |
280 |
/** |
243 |
* INTERNAL USE-ONLY |
281 |
* INTERNAL USE-ONLY |
Lines 343-353
Link Here
|
343 |
String relativeFileName, |
381 |
String relativeFileName, |
344 |
ClassFile classFile) throws IOException { |
382 |
ClassFile classFile) throws IOException { |
345 |
|
383 |
|
346 |
BufferedOutputStream output = null; |
384 |
// BufferedOutputStream output = null; |
|
|
385 |
File file = null; |
347 |
if (generatePackagesStructure) { |
386 |
if (generatePackagesStructure) { |
348 |
output = new BufferedOutputStream( |
387 |
// output = new BufferedOutputStream( |
349 |
new FileOutputStream( |
388 |
// new FileOutputStream( |
350 |
new File(buildAllDirectoriesInto(outputPath, relativeFileName)))); |
389 |
// new File(buildAllDirectoriesInto(outputPath, relativeFileName)))); |
|
|
390 |
file = new File(buildAllDirectoriesInto(outputPath, relativeFileName)); |
351 |
} else { |
391 |
} else { |
352 |
String fileName = null; |
392 |
String fileName = null; |
353 |
char fileSeparatorChar = File.separatorChar; |
393 |
char fileSeparatorChar = File.separatorChar; |
Lines 370-385
Link Here
|
370 |
fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length); |
410 |
fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length); |
371 |
} |
411 |
} |
372 |
} |
412 |
} |
373 |
output = new BufferedOutputStream( |
413 |
// output = new BufferedOutputStream( |
374 |
new FileOutputStream( |
414 |
// new FileOutputStream( |
375 |
new File(fileName))); |
415 |
// new File(fileName))); |
|
|
416 |
file = new File(fileName); |
376 |
} |
417 |
} |
|
|
418 |
|
419 |
// Create file |
420 |
BufferedOutputStream output = new BufferedOutputStream( |
421 |
new FileOutputStream(file)); |
422 |
|
423 |
// Wait for file creation |
424 |
int count = 0, max = 50; |
425 |
while (!file.exists() && count < max) { |
426 |
try { |
427 |
Thread.sleep(10); |
428 |
count++; |
429 |
} catch (InterruptedException e) { |
430 |
break; |
431 |
} |
432 |
} |
433 |
if (count > 0) { |
434 |
int time = count * 10; |
435 |
if (time > MAX_TIME) MAX_TIME = time; |
436 |
System.out.println("ClassFile: takes "+time+"ms (max="+MAX_TIME+"ms) before the file "+file+" was created..."); |
437 |
} |
438 |
|
439 |
// Write contents |
377 |
try { |
440 |
try { |
378 |
// if no IOException occured, output cannot be null |
441 |
// if no IOException occured, output cannot be null |
379 |
output.write(classFile.header, 0, classFile.headerOffset); |
442 |
output.write(classFile.header, 0, classFile.headerOffset); |
380 |
output.write(classFile.contents, 0, classFile.contentsOffset); |
443 |
output.write(classFile.contents, 0, classFile.contentsOffset); |
381 |
output.flush(); |
444 |
output.flush(); |
382 |
} catch(IOException e) { |
445 |
} catch(IOException e) { |
|
|
446 |
System.out.println("ClassFile: exception ["+e.getMessage()+"] while writing class file "+file); |
383 |
throw e; |
447 |
throw e; |
384 |
} finally { |
448 |
} finally { |
385 |
output.close(); |
449 |
output.close(); |