下面列出了怎么用org.apache.commons.io.output.DeferredFileOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。
private static DiskFileItem makePayload ( int thresh, String repoPath, String filePath, byte[] data ) throws IOException, Exception {
// if thresh < written length, delete outputFile after copying to repository temp file
// otherwise write the contents to repository temp file
File repository = new File(repoPath);
DiskFileItem diskFileItem = new DiskFileItem("test", "application/octet-stream", false, "test", 100000, repository);
File outputFile = new File(filePath);
DeferredFileOutputStream dfos = new DeferredFileOutputStream(thresh, outputFile);
OutputStream os = (OutputStream) Reflections.getFieldValue(dfos, "memoryOutputStream");
os.write(data);
Reflections.getField(ThresholdingOutputStream.class, "written").set(dfos, data.length);
Reflections.setFieldValue(diskFileItem, "dfos", dfos);
Reflections.setFieldValue(diskFileItem, "sizeThreshold", 0);
return diskFileItem;
}
private static Response updateFile(InputStream contents, MCRPath mcrPath) {
LogManager.getLogger().info("Updating file: {}", mcrPath);
int memBuf = getUploadMemThreshold();
java.io.File uploadDirectory = getUploadTempStorage();
try (DeferredFileOutputStream dfos = new DeferredFileOutputStream(memBuf, mcrPath.getOwner(),
mcrPath.getFileName().toString(), uploadDirectory);
MaxBytesOutputStream mbos = new MaxBytesOutputStream(dfos)) {
IOUtils.copy(contents, mbos);
mbos.close(); //required if temporary file was used
OutputStream out = Files.newOutputStream(mcrPath);
try {
if (dfos.isInMemory()) {
out.write(dfos.getData());
} else {
java.io.File tempFile = dfos.getFile();
if (tempFile != null) {
try {
Files.copy(tempFile.toPath(), out);
} finally {
LogManager.getLogger().debug("Deleting file {} of size {}.", tempFile.getAbsolutePath(),
tempFile.length());
tempFile.delete();
}
}
}
} finally {
//close writes data to database
doWithinTransaction(out::close);
}
} catch (IOException e) {
throw MCRErrorResponse.fromStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())
.withErrorCode(MCRErrorCodeConstants.MCRDERIVATE_UPDATE_FILE)
.withMessage("Could not update file " + mcrPath + ".")
.withDetail(e.getMessage())
.withCause(e)
.toException();
}
return Response.noContent().build();
}
private static DiskFileItem makePayload ( int thresh, String repoPath, String filePath, byte[] data ) throws IOException, Exception {
// if thresh < written length, delete outputFile after copying to repository temp file
// otherwise write the contents to repository temp file
File repository = new File(repoPath);
DiskFileItem diskFileItem = new DiskFileItem("test", "application/octet-stream", false, "test", 100000, repository);
File outputFile = new File(filePath);
DeferredFileOutputStream dfos = new DeferredFileOutputStream(thresh, outputFile);
OutputStream os = (OutputStream) Reflections.getFieldValue(dfos, "memoryOutputStream");
os.write(data);
Reflections.getField(ThresholdingOutputStream.class, "written").set(dfos, data.length);
Reflections.setFieldValue(diskFileItem, "dfos", dfos);
Reflections.setFieldValue(diskFileItem, "sizeThreshold", 0);
return diskFileItem;
}
/**
* {@inheritDoc}
*/
public InputStream getInputStream() throws IOException, RepositoryException {
try (DeferredFileOutputStream out = new DeferredFileOutputStream(8192, "vlttmp", ".tmp", null)) {
spool(out);
if (out.isInMemory()) {
return new ByteArrayInputStream(out.getData());
} else {
return new TempFileInputStream(out.getFile());
}
}
}
public void release ( DiskFileItem obj ) throws Exception {
// otherwise the finalizer deletes the file
DeferredFileOutputStream dfos = new DeferredFileOutputStream(0, null);
Reflections.setFieldValue(obj, "dfos", dfos);
}
public void release ( DiskFileItem obj ) throws Exception {
// otherwise the finalizer deletes the file
DeferredFileOutputStream dfos = new DeferredFileOutputStream(0, null);
Reflections.setFieldValue(obj, "dfos", dfos);
}
public MimeMessageInputStreamSource(String key) {
super();
out = new DeferredFileOutputStream(THRESHOLD, key, ".m64", TMPDIR);
sourceId = key;
}
/**
* Create a result iterator by iterating through the results of a scan, spooling them to disk once
* a threshold has been reached. The scanner passed in is closed prior to returning.
* @param scanner the results of a table scan
* @param mm memory manager tracking memory usage across threads.
* @param thresholdBytes the requested threshold. Will be dialed down if memory usage (as determined by
* the memory manager) is exceeded.
* @throws SQLException
*/
SpoolingResultIterator(ResultIterator scanner, MemoryManager mm, final int thresholdBytes, final long maxSpoolToDisk) throws SQLException {
boolean success = false;
boolean usedOnDiskIterator = false;
final MemoryChunk chunk = mm.allocate(0, thresholdBytes);
File tempFile = null;
try {
// Can't be bigger than int, since it's the max of the above allocation
int size = (int)chunk.getSize();
tempFile = File.createTempFile("ResultSpooler",".bin");
DeferredFileOutputStream spoolTo = new DeferredFileOutputStream(size, tempFile) {
@Override
protected void thresholdReached() throws IOException {
super.thresholdReached();
chunk.close();
}
};
DataOutputStream out = new DataOutputStream(spoolTo);
final long maxBytesAllowed = maxSpoolToDisk == -1 ?
Long.MAX_VALUE : thresholdBytes + maxSpoolToDisk;
long bytesWritten = 0L;
int maxSize = 0;
for (Tuple result = scanner.next(); result != null; result = scanner.next()) {
int length = TupleUtil.write(result, out);
bytesWritten += length;
if(bytesWritten > maxBytesAllowed){
throw new SpoolTooBigToDiskException("result too big, max allowed(bytes): " + maxBytesAllowed);
}
maxSize = Math.max(length, maxSize);
}
spoolTo.close();
if (spoolTo.isInMemory()) {
byte[] data = spoolTo.getData();
chunk.resize(data.length);
spoolFrom = new InMemoryResultIterator(data, chunk);
} else {
spoolFrom = new OnDiskResultIterator(maxSize, spoolTo.getFile());
usedOnDiskIterator = true;
}
success = true;
} catch (IOException e) {
throw ServerUtil.parseServerException(e);
} finally {
try {
scanner.close();
} finally {
try {
if (!usedOnDiskIterator) {
tempFile.delete();
}
} finally {
if (!success) {
chunk.close();
}
}
}
}
}