Skip to content

Commit 3d200a2

Browse files
committed
Merge pull request #5 from vlsi/overwrite_fix
Make compareDestination=true detect non-existing files in preprocessing mode
2 parents 5d9262d + 9721316 commit 3d200a2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,22 @@ public boolean saveBuffersToFile(final File outFile, final boolean removeComment
399399
content = ((StringWriter)new JavaCommentsRemover(new StringReader(content), new StringWriter(totatBufferedChars)).process()).toString();
400400
}
401401

402-
if (outFile.isFile()){
403-
final byte [] contentInBinaryForm = content.getBytes(globalOutCharacterEncoding);
404-
final InputStream currentFileInputStream = new BufferedInputStream(new FileInputStream(outFile),Math.max(16384, (int)outFile.length()));
405-
if (!IOUtils.contentEquals(currentFileInputStream,new ByteArrayInputStream(contentInBinaryForm))){
406-
currentFileInputStream.close();
407-
FileUtils.writeByteArrayToFile(outFile, contentInBinaryForm, false);
408-
wasSaved = true;
402+
boolean needWrite = true; // better write than not
403+
final byte [] contentInBinaryForm = content.getBytes(globalOutCharacterEncoding);
404+
if (outFile.isFile() && outFile.length() == contentInBinaryForm.length) {
405+
// If file exists and has the same content, then skip overwriting it
406+
InputStream currentFileInputStream = null;
407+
try {
408+
currentFileInputStream = new BufferedInputStream(new FileInputStream(outFile),Math.max(16384, (int)outFile.length()));
409+
needWrite = !IOUtils.contentEquals(currentFileInputStream,new ByteArrayInputStream(contentInBinaryForm));
410+
} finally {
411+
IOUtils.closeQuietly(currentFileInputStream);
409412
}
410413
}
414+
if (needWrite) {
415+
FileUtils.writeByteArrayToFile(outFile, contentInBinaryForm, false);
416+
wasSaved = true;
417+
}
411418
}else{
412419
if (removeComments) {
413420
final String joinedBufferContent = ((StringWriter) writePrinterBuffers(new StringWriter(totatBufferedChars))).toString();

0 commit comments

Comments
 (0)