本文整理汇总了Java中org.apache.commons.io.output.ProxyOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java ProxyOutputStream类的具体用法?Java ProxyOutputStream怎么用?Java ProxyOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ProxyOutputStream类属于org.apache.commons.io.output包,在下文中一共展示了ProxyOutputStream类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOutputStream
import org.apache.commons.io.output.ProxyOutputStream; //导入依赖的package包/类
/**
* 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;
}
}