public void release ( DiskFileItem obj ) throws Exception { // otherwise the finalizer deletes the file DeferredFileOutputStream dfos = new DeferredFileOutputStream(0, null); Reflections.setFieldValue(obj, "dfos", dfos); }
/** * Returns an {@link java.io.OutputStream OutputStream} that can * be used for storing the contents of the file. * * @return An {@link java.io.OutputStream OutputStream} that can be used * for storing the contents of the file. * * @throws IOException if an error occurs. */ @Override public OutputStream getOutputStream() throws IOException { if (dfos == null) { File outputFile = getTempFile(); dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); } return dfos; }
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; }
/** * Test specifying a temporary file and the threshold is reached. * @throws Exception */ @Test public void testTempFileError() throws Exception { final String prefix = null; final String suffix = ".out"; final File tempDir = new File("."); try { (new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir)).close(); fail("Expected IllegalArgumentException "); } catch (final IllegalArgumentException e) { // expected } }
/** * Test whether writeTo() properly writes small content. */ @Test public void testWriteToSmall(){ final File testFile = new File("testWriteToMem.dat"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize); // Ensure that the test starts from a clean base. testFile.delete(); final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length *2, initialBufferSize, testFile); try{ dfos.write(testBytes); assertFalse(testFile.exists()); assertTrue(dfos.isInMemory()); try { dfos.writeTo(baos); fail("Should not have been able to write before closing"); } catch (final IOException ioe) { // ok, as expected } dfos.close(); dfos.writeTo(baos); } catch (final IOException ioe) { fail("Unexpected IOException"); } final byte[] copiedBytes = baos.toByteArray(); assertTrue(Arrays.equals(testBytes, copiedBytes)); testFile.delete(); }
new DeferredFileOutputStream(testBytes.length / 2, testFile); try{ dfos.write(testBytes);
/** * Tests the case where the amount of data exceeds the threshold, and is * therefore written to disk. The actual data written to disk is verified, * as is the file itself. */ @Test public void testAboveThreshold() { final File testFile = new File("testAboveThreshold.dat"); // Ensure that the test starts from a clean base. testFile.delete(); final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, testFile); try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); assertNull(dfos.getData()); verifyResultFile(testFile); // Ensure that the test starts from a clean base. testFile.delete(); }
/** * Tests the case where there are multiple writes beyond the threshold, to * ensure that the <code>thresholdReached()</code> method is only called * once, as the threshold is crossed for the first time. */ @Test public void testThresholdReached() { final File testFile = new File("testThresholdReached.dat"); // Ensure that the test starts from a clean base. testFile.delete(); final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize, testFile); final int chunkSize = testBytes.length / 3; try { dfos.write(testBytes, 0, chunkSize); dfos.write(testBytes, chunkSize, chunkSize); dfos.write(testBytes, chunkSize * 2, testBytes.length - chunkSize * 2); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); assertNull(dfos.getData()); verifyResultFile(testFile); // Ensure that the test starts from a clean base. testFile.delete(); }
/** * Test specifying a temporary file and the threshold is reached. */ @Test public void testTempFileAboveThreshold() { final String prefix = "commons-io-test"; final String suffix = ".out"; final File tempDir = new File("."); final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir); assertNull("Check file is null-A", dfos.getFile()); try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); assertNull(dfos.getData()); assertNotNull("Check file not null", dfos.getFile()); assertTrue("Check file exists", dfos.getFile().exists()); assertTrue("Check prefix", dfos.getFile().getName().startsWith(prefix)); assertTrue("Check suffix", dfos.getFile().getName().endsWith(suffix)); assertEquals("Check dir", tempDir.getPath(), dfos.getFile().getParent()); verifyResultFile(dfos.getFile()); // Delete the temporary file. dfos.getFile().delete(); }
/** * Tests the case where the amount of data falls below the threshold, and * is therefore confined to memory. */ @Test public void testBelowThreshold() { final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, null); try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertTrue(dfos.isInMemory()); final byte[] resultBytes = dfos.getData(); assertEquals(testBytes.length, resultBytes.length); assertTrue(Arrays.equals(resultBytes, testBytes)); }
/** * Tests the case where the amount of data is exactly the same as the * threshold. The behavior should be the same as that for the amount of * data being below (i.e. not exceeding) the threshold. */ @Test public void testAtThreshold() { final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length, initialBufferSize, null); try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertTrue(dfos.isInMemory()); final byte[] resultBytes = dfos.getData(); assertEquals(testBytes.length, resultBytes.length); assertTrue(Arrays.equals(resultBytes, testBytes)); }
/** * Test specifying a temporary file and the threshold is reached. */ @Test public void testTempFileAboveThresholdPrefixOnly() { final String prefix = "commons-io-test"; final String suffix = null; final File tempDir = null; final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir); assertNull("Check file is null-A", dfos.getFile()); try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertFalse(dfos.isInMemory()); assertNull(dfos.getData()); assertNotNull("Check file not null", dfos.getFile()); assertTrue("Check file exists", dfos.getFile().exists()); assertTrue("Check prefix", dfos.getFile().getName().startsWith(prefix)); assertTrue("Check suffix", dfos.getFile().getName().endsWith(".tmp")); // ".tmp" is default verifyResultFile(dfos.getFile()); // Delete the temporary file. dfos.getFile().delete(); }
/** * Test specifying a temporary file and the threshold not reached. */ @Test public void testTempFileBelowThreshold() { final String prefix = "commons-io-test"; final String suffix = ".out"; final File tempDir = new File("."); final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDir); assertNull("Check file is null-A", dfos.getFile()); try { dfos.write(testBytes, 0, testBytes.length); dfos.close(); } catch (final IOException e) { fail("Unexpected IOException"); } assertTrue(dfos.isInMemory()); assertNull("Check file is null-B", dfos.getFile()); }
/** * Returns an {@link java.io.OutputStream OutputStream} that can be used for * storing the contents of the file. * * @return An {@link java.io.OutputStream OutputStream} that can be used for * storing the contensts of the file. * @throws IOException if an error occurs. */ public OutputStream getOutputStream() throws IOException { if (dfos == null) { int sizeThreshold; if (keepFormFieldInMemory && isFormField()) { sizeThreshold = Integer.MAX_VALUE; } else { sizeThreshold = this.sizeThreshold; } dfos = new DeferredFileOutputStream(sizeThreshold, "upload_" + UID, ".tmp", repository); } return dfos; }
/** * Returns an {@link java.io.OutputStream OutputStream} that can be used for * storing the contents of the file. * * @return An {@link java.io.OutputStream OutputStream} that can be used for * storing the contensts of the file. * @throws IOException if an error occurs. */ public OutputStream getOutputStream() throws IOException { if (dfos == null) { int sizeThreshold; if (keepFormFieldInMemory && isFormField()) { sizeThreshold = Integer.MAX_VALUE; } else { sizeThreshold = this.sizeThreshold; } dfos = new DeferredFileOutputStream(sizeThreshold, "upload_" + UID, ".tmp", repository); } return dfos; }
/** * Creates the request OutputStream, to be sent to the end Service invoked, as a * <a href="http://commons.apache.org/io/api-release/org/apache/commons/io/output/DeferredFileOutputStream.html" * >DeferredFileOutputStream</a>. * * * @param request - * @return - DeferredFileOutputStream with the ClientRequest written out per HTTP specification. * @throws IOException - */ private DeferredFileOutputStream writeRequestBodyToOutputStream(final ClientInvocation request) throws IOException { DeferredFileOutputStream memoryManagedOutStream = new DeferredFileOutputStream( this.fileUploadInMemoryThresholdLimit * getMemoryUnitMultiplier(), getTempfilePrefix(), ".tmp", this.fileUploadTempFileDir); request.getDelegatingOutputStream().setDelegate(memoryManagedOutStream); request.writeRequestBody(request.getEntityStream()); memoryManagedOutStream.close(); return memoryManagedOutStream; }
private DeferredFileOutputStream newLog() { return new DeferredFileOutputStream(10*1024,"maven-build","log",null); }
public Deferred( final PlexusIoResource resource, PlexusIoResourceCollection owner, boolean hasTransformer ) throws IOException { this.resource = resource; this.owner = owner; dfos = hasTransformer ? new DeferredFileOutputStream( 5000000, "p-archiver", null, null ) : null; if ( dfos != null ) { InputStream inputStream = owner.getInputStream( resource ); IOUtils.copy( inputStream, dfos ); IOUtils.closeQuietly( inputStream ); } }
private static DeferredFileOutputStream asDeferredStream( @Nonnull ContentSupplier supplier, @Nonnull InputStreamTransformer transToUse, PlexusIoResource resource ) throws IOException { DeferredFileOutputStream dfos = new DeferredFileOutputStream( 5000000, "p-archiver", null, null ); InputStream inputStream = supplier.getContents(); InputStream transformed = transToUse.transform( resource, inputStream ); IOUtils.copy( transformed, dfos ); IOUtils.closeQuietly( inputStream ); IOUtils.closeQuietly( transformed ); return dfos; }
/** * {@inheritDoc} */ public InputStream getInputStream() throws IOException, RepositoryException { DeferredFileOutputStream out = new DeferredFileOutputStream(8192, "vlttmp", ".tmp", null); spool(out); out.close(); if (out.isInMemory()) { return new ByteArrayInputStream(out.getData()); } else { return new TempFileInputStream(out.getFile()); } }