Source:
private File handleInputStreamMessage(final InputStream sourceFileInputStream, File originalFile, File tempFile, final File resultFile) throws IOException { final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode) || FileExistsMode.APPEND_NO_FLUSH.equals(this.fileExistsMode); if (append) { final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); final FileState state = getFileState(fileToWriteTo, false); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, fileToWriteTo.getAbsolutePath()) { @Override protected void whileLocked() throws IOException { BufferedOutputStream bos = state != null ? state.stream : createOutputStream(fileToWriteTo, append); try { byte[] buffer = new byte[StreamUtils.BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = sourceFileInputStream.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } if (FileWritingMessageHandler.this.appendNewLine) { bos.write(LINE_SEPARATOR.getBytes()); } } finally { try { sourceFileInputStream.close(); } catch (IOException ex) { } try { if (state == null) { bos.close(); } else { state.lastWrite = System.currentTimeMillis(); } } catch (IOException ex) { } } } }; whileLockedProcessor.doWhileLocked(); cleanUpAfterCopy(fileToWriteTo, resultFile, originalFile); return resultFile; } else { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempFile), this.bufferSize); try { byte[] buffer = new byte[StreamUtils.BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = sourceFileInputStream.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } if (this.appendNewLine) { bos.write(LINE_SEPARATOR.getBytes()); } bos.flush(); } finally { try { sourceFileInputStream.close(); } catch (IOException ex) { } try { bos.close(); } catch (IOException ex) { } } cleanUpAfterCopy(tempFile, resultFile, originalFile); return resultFile; } }

Target:
private File handleInputStreamMessage(final InputStream sourceFileInputStream, File originalFile, File tempFile, final File resultFile) throws IOException { final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode) || FileExistsMode.APPEND_NO_FLUSH.equals(this.fileExistsMode); if (append) { final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); final FileState state = getFileState(fileToWriteTo, false); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, fileToWriteTo.getAbsolutePath()) { @Override protected void whileLocked() throws IOException { BufferedOutputStream bos = state != null ? state.stream : createOutputStream(fileToWriteTo, true); try { byte[] buffer = new byte[StreamUtils.BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = sourceFileInputStream.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } if (FileWritingMessageHandler.this.appendNewLine) { bos.write(LINE_SEPARATOR.getBytes()); } } finally { try { sourceFileInputStream.close(); } catch (IOException ex) { } try { if (state == null || FileWritingMessageHandler.this.flushTask == null) { bos.close(); } else { state.lastWrite = System.currentTimeMillis(); } } catch (IOException ex) { } } } }; whileLockedProcessor.doWhileLocked(); cleanUpAfterCopy(fileToWriteTo, resultFile, originalFile); return resultFile; } else { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempFile), this.bufferSize); try { byte[] buffer = new byte[StreamUtils.BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = sourceFileInputStream.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } if (this.appendNewLine) { bos.write(LINE_SEPARATOR.getBytes()); } bos.flush(); } finally { try { sourceFileInputStream.close(); } catch (IOException ex) { } try { bos.close(); } catch (IOException ex) { } } cleanUpAfterCopy(tempFile, resultFile, originalFile); return resultFile; } }