Skip to content

Make compareDestination=true detect non-existing files in preprocessing mode #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/main/java/com/igormaznitsa/jcp/context/PreprocessingState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down