diff --git a/src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java b/src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java index e5df1b07..3c9274c4 100755 --- a/src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java +++ b/src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java @@ -399,15 +399,22 @@ public boolean saveBuffersToFile(final File outFile, final boolean removeComment content = ((StringWriter)new JavaCommentsRemover(new StringReader(content), new StringWriter(totatBufferedChars)).process()).toString(); } - if (outFile.isFile()){ - final byte [] contentInBinaryForm = content.getBytes(globalOutCharacterEncoding); - final InputStream currentFileInputStream = new BufferedInputStream(new FileInputStream(outFile),Math.max(16384, (int)outFile.length())); - if (!IOUtils.contentEquals(currentFileInputStream,new ByteArrayInputStream(contentInBinaryForm))){ - currentFileInputStream.close(); - FileUtils.writeByteArrayToFile(outFile, contentInBinaryForm, false); - wasSaved = true; + boolean needWrite = true; // better write than not + final byte [] contentInBinaryForm = content.getBytes(globalOutCharacterEncoding); + if (outFile.isFile() && outFile.length() == contentInBinaryForm.length) { + // If file exists and has the same content, then skip overwriting it + InputStream currentFileInputStream = null; + try { + currentFileInputStream = new BufferedInputStream(new FileInputStream(outFile),Math.max(16384, (int)outFile.length())); + needWrite = !IOUtils.contentEquals(currentFileInputStream,new ByteArrayInputStream(contentInBinaryForm)); + } finally { + IOUtils.closeQuietly(currentFileInputStream); } } + if (needWrite) { + FileUtils.writeByteArrayToFile(outFile, contentInBinaryForm, false); + wasSaved = true; + } }else{ if (removeComments) { final String joinedBufferContent = ((StringWriter) writePrinterBuffers(new StringWriter(totatBufferedChars))).toString();