public void testFinalizeDeletesFile() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(0, true); write(out, data, 0, 100, true); final File file = out.getFile(); assertEquals(100, file.length()); assertTrue(file.exists()); out.close(); // Make sure that finalize deletes the file out = null; // times out and throws RuntimeException on failure GcFinalization.awaitDone( new GcFinalization.FinalizationPredicate() { @Override public boolean isDone() { return !file.exists(); } }); }
public void testReset() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(Integer.MAX_VALUE); ByteSource source = out.asByteSource(); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.reset(); assertTrue(Arrays.equals(new byte[0], source.read())); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.close(); } }
public void testWriteErrorAfterClose() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(50); ByteSource source = out.asByteSource(); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.close(); try { out.write(42); fail("expected exception"); } catch (IOException expected) { } // Verify that write had no effect assertTrue(Arrays.equals(data, source.read())); out.reset(); }
throws IOException { byte[] data = newPreFilledByteArray(dataSize); FileBackedOutputStream out = new FileBackedOutputStream(fileThreshold, resetOnFinalize); ByteSource source = out.asByteSource(); int chunk1 = Math.min(dataSize, fileThreshold);
Runtime.getRuntime().maxMemory() / 10)); backingStore = new FileBackedOutputStream(threshold); try (Writer writer = new OutputStreamWriter(backingStore, UTF_8)) { CharStreams.copy(source, writer);
FileBackedOutputStream os = new FileBackedOutputStream(threshold); try {
/** * @param fileThreshold the number of bytes before the stream should switch to buffering to a file */ public TemporaryFileBackedOutputStream(int fileThreshold) { this.fileBackedOutputStream = new FileBackedOutputStream(fileThreshold); }
public ProgressiveLoggingProcessOutputHandler( @NonNull ILogger logger, boolean logStdioToInfo) { this.logger = logger; this.logStdioToInfo = logStdioToInfo; standardOutput = new FileBackedOutputStream(2048); combinedOutput = new FileBackedOutputStream(2048); loggingProcessOutput = new ProgressiveLoggingProcessOutput(); }
@Override public String store(InputStream inputStream, String partialReference) throws DataStoreException { try ( FileBackedOutputStream fileBackedOutputStream = new FileBackedOutputStream(FILE_THRESHOLD, true) ) { try { ByteStreams.copy(inputStream, fileBackedOutputStream); return store(fileBackedOutputStream.asByteSource(), partialReference); } finally { fileBackedOutputStream.reset(); } } catch (IOException ex) { errors.incrementAndGet(); throw new DataStoreException("Could not store input stream.", ex); } }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; FileBackedOutputStream out = null; try { out = new FileBackedOutputStream(limit); long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); wire(header, out.getSupplier().getInput()); return out.getSupplier().getInput(); } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; final FileBackedOutputStream out = new FileBackedOutputStream(limit); try { long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); wire(header, out.getSupplier().getInput()); // we must call FileBackedOutputStream.reset to remove temporary file return new FilterInputStream(out.getSupplier().getInput()) { @Override public void close() throws IOException { super.close(); out.reset(); } }; } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
Runtime.getRuntime().maxMemory() / 10)); backingStore = new FileBackedOutputStream(threshold); try (Writer writer = new OutputStreamWriter(backingStore, UTF_8)) { CharStreams.copy(source, writer);
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; final FileBackedOutputStream out = new FileBackedOutputStream(limit); try { long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); InputStream is = out.asByteSource().openStream(); try { wire(header, is); } finally { is.close(); } // we must call FileBackedOutputStream.reset to remove temporary file return new FilterInputStream(out.asByteSource().getInput()) { @Override public void close() throws IOException { super.close(); out.reset(); } }; } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; final FileBackedOutputStream out = new FileBackedOutputStream(limit); try { long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); InputStream is = out.asByteSource().openStream(); try { wire(header, is); } finally { is.close(); } // we must call FileBackedOutputStream.reset to remove temporary file return new FilterInputStream(out.asByteSource().openStream()) { @Override public void close() throws IOException { super.close(); out.reset(); } }; } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; final FileBackedOutputStream out = new FileBackedOutputStream(limit); try { long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); InputStream is = out.asByteSource().openStream(); try { wire(header, is); } finally { is.close(); } // we must call FileBackedOutputStream.reset to remove temporary file return new FilterInputStream(out.asByteSource().openStream()) { @Override public void close() throws IOException { super.close(); out.reset(); } }; } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; final FileBackedOutputStream out = new FileBackedOutputStream(limit); try { long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); InputStream is = out.asByteSource().openStream(); try { wire(header, is); } finally { is.close(); } // we must call FileBackedOutputStream.reset to remove temporary file return new FilterInputStream(out.asByteSource().openStream()) { @Override public void close() throws IOException { super.close(); out.reset(); } }; } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
public void testFinalizeDeletesFile() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(0, true); write(out, data, 0, 100, true); final File file = out.getFile(); assertEquals(100, file.length()); assertTrue(file.exists()); out.close(); // Make sure that finalize deletes the file out = null; // times out and throws RuntimeException on failure GcFinalization.awaitDone(new GcFinalization.FinalizationPredicate() { @Override public boolean isDone() { return !file.exists(); } }); }
public void testReset() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(Integer.MAX_VALUE); ByteSource source = out.asByteSource(); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.reset(); assertTrue(Arrays.equals(new byte[0], source.read())); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.close(); } }
public void testWriteErrorAfterClose() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(50); ByteSource source = out.asByteSource(); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.close(); try { out.write(42); fail("expected exception"); } catch (IOException expected) { } // Verify that write had no effect assertTrue(Arrays.equals(data, source.read())); out.reset(); }
@Override public BlobURI write(final UUID messageId, final Mailbox mailbox, final String profileName, final InputStream in, final Long size) throws IOException, GeneralSecurityException { // get blob name String blobName = new BlobNameBuilder().setMailbox(mailbox) .setMessageId(messageId).setMessageSize(size).build(); InputStream in1; Long updatedSize = size; // prepare URI BlobURI blobUri = new BlobURI() .setProfile(profileName) .setName(blobName); // encrypt stream if (encryptionHandler != null) { byte[] iv = getCipherIVFromBlobName(blobName); InputStream encryptedInputStream = this.encryptionHandler.encrypt(in, Configurator.getBlobStoreDefaultEncryptionKey(), iv); FileBackedOutputStream fbout = new FileBackedOutputStream(MAX_MEMORY_FILE_SIZE, true); updatedSize = ByteStreams.copy(encryptedInputStream, fbout); in1 = fbout.getSupplier().getInput(); blobUri.setEncryptionKey(Configurator.getBlobStoreDefaultEncryptionKeyAlias()); } else { in1 = in; } CloudStoreProxy.write(blobName, profileName, in1, updatedSize); return blobUri; }