/** * Returns an output stream for the requested file. * * After completing the write the contents must be committed using the {@link #commit(java.io.OutputStream)} * method and the stream must be released using the {@link #release()} method. * * Example usage: * * DataStore dataStore = new DataStore(...); * try (OutputStream os = dataStore.getOutputStream()) { * os.write(..); * dataStore.commit(os); * } catch (IOException e) { * ... * } finally { * dataStore.release(); * } * * @return * @throws IOException */ public OutputStream getOutputStream() throws IOException { acquireLock(); try { isClosed = false; forWrite = true; LOG.trace("Starts write '{}'", file); verifyAndRecover(); if (Files.exists(file)) { Files.move(file, fileOld); LOG.trace("Starting write, move '{}' to '{}'", file, fileOld); } OutputStream os = new ProxyOutputStream(new FileOutputStream(fileTmp.toFile())) { @Override public void close() throws IOException { if (isClosed) { return; } try { super.close(); } finally { isClosed = true; stream = null; } LOG.trace("Finishes write '{}'", file); } }; stream = os; return os; } catch (Exception ex) { release(); throw ex; } }