@@ -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