org.apache.commons.io.output.DeferredFileOutputStream Java Examples

The following examples show how to use org.apache.commons.io.output.DeferredFileOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileUpload1.java    From ysoserial-modified with MIT License 5 votes vote down vote up
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;
}
 
Example #2
Source File: MCRRestDerivateContents.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example #3
Source File: FileUpload1.java    From ysoserial with MIT License 5 votes vote down vote up
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;
}
 
Example #4
Source File: SerializerArtifact.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * {@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());
        }
    }
}
 
Example #5
Source File: FileUpload1.java    From ysoserial-modified with MIT License 4 votes vote down vote up
public void release ( DiskFileItem obj ) throws Exception {
    // otherwise the finalizer deletes the file
    DeferredFileOutputStream dfos = new DeferredFileOutputStream(0, null);
    Reflections.setFieldValue(obj, "dfos", dfos);
}
 
Example #6
Source File: FileUpload1.java    From ysoserial with MIT License 4 votes vote down vote up
public void release ( DiskFileItem obj ) throws Exception {
    // otherwise the finalizer deletes the file
    DeferredFileOutputStream dfos = new DeferredFileOutputStream(0, null);
    Reflections.setFieldValue(obj, "dfos", dfos);
}
 
Example #7
Source File: MimeMessageInputStreamSource.java    From james-project with Apache License 2.0 4 votes vote down vote up
public MimeMessageInputStreamSource(String key) {
    super();
    out = new DeferredFileOutputStream(THRESHOLD, key, ".m64", TMPDIR);
    sourceId = key;
}
 
Example #8
Source File: SpoolingResultIterator.java    From phoenix with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
* 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();
                }
            }
        }
    }
}