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(); } }
@Override public synchronized void write(int b) throws IOException { update(1); out.write(b); }
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(); } }); }
throws IOException { byte[] data = newPreFilledByteArray(dataSize); FileBackedOutputStream out = new FileBackedOutputStream(fileThreshold, resetOnFinalize); ByteSource source = out.asByteSource(); int chunk1 = Math.min(dataSize, fileThreshold); int chunk2 = dataSize - chunk1; assertTrue(ByteSource.wrap(data).slice(0, chunk1).contentEquals(source)); File file = out.getFile(); assertNull(file); file = out.getFile(); assertEquals(dataSize, file.length()); assertTrue(file.exists()); out.close(); out.reset(); if (file != null) { assertFalse(file.exists());
@Override protected void finalize() { try { reset(); } catch (Throwable t) { t.printStackTrace(System.err); } } };
@Override public synchronized void write(byte[] b) throws IOException { write(b, 0, b.length); }
FileBackedOutputStream os = new FileBackedOutputStream(threshold); try { payload.writeTo(writer); try (InputStream rawIn = os.asByteSource().openBufferedStream(); BufferedInputStream contentStream = new BufferedInputStream(rawIn)) { os.reset();
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); } }
private @Nullable Map<String, Object> getOss() throws IOException { CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8); try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) { input.beginObject(); while (input.hasNext()) { String name = input.nextName(); if (DESIRED_CAPABILITIES.equals(name)) { return input.read(MAP_TYPE); } input.skipValue(); } } return null; }
/** * Calls {@link #close} if not already closed, and then resets this object back to its initial * state, for reuse. If data was buffered to a file, it will be deleted. * * @throws IOException if an I/O error occurred while deleting the file buffer */ public synchronized void reset() throws IOException { try { close(); } finally { if (memory == null) { memory = new MemoryOutput(); } else { memory.reset(); } out = memory; if (file != null) { File deleteMe = file; file = null; if (!deleteMe.delete()) { throw new IOException("Could not delete: " + deleteMe); } } } }
@Override public void close() throws IOException { if (this.subEntryOutputStream != null) { this.subEntryOutputStream.reset(); this.subEntryOutputStream.close(); this.subEntryOutputStream = null; } if (this.entryOutputStream != null) { this.entryOutputStream.reset(); this.entryOutputStream.close(); this.entryOutputStream = null; } } }
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); } }
Runtime.getRuntime().maxMemory() / 10)); backingStore = new FileBackedOutputStream(threshold); try (Writer writer = new OutputStreamWriter(backingStore, UTF_8)) { CharStreams.copy(source, writer);
private void testThreshold(int fileThreshold, int dataSize, boolean singleByte, boolean resetOnFinalize) throws IOException { byte[] data = newPreFilledByteArray(dataSize); FileBackedOutputStream out = new FileBackedOutputStream(fileThreshold, resetOnFinalize); ByteSource source = out.asByteSource(); int chunk1 = Math.min(dataSize, fileThreshold); int chunk2 = dataSize - chunk1; // Write just enough to not trip the threshold if (chunk1 > 0) { write(out, data, 0, chunk1, singleByte); assertTrue(ByteSource.wrap(data).slice(0, chunk1).contentEquals(source)); } File file = out.getFile(); assertNull(file); // Write data to go over the threshold if (chunk2 > 0) { write(out, data, chunk1, chunk2, singleByte); file = out.getFile(); assertEquals(dataSize, file.length()); assertTrue(file.exists()); } out.close(); // Check that source returns the right data assertTrue(Arrays.equals(data, source.read())); // Make sure that reset deleted the file out.reset(); if (file != null) { assertFalse(file.exists()); } }
@Override protected void finalize() { try { reset(); } catch (Throwable t) { t.printStackTrace(System.err); } } };
@Override public synchronized void write(byte[] b) throws IOException { write(b, 0, b.length); }
@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; 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); } }
private @Nullable Map<String, Object> getAlwaysMatch() throws IOException { CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8); try (Reader reader = charSource.openBufferedStream(); JsonInput input = json.newInput(reader)) { input.beginObject(); while (input.hasNext()) { String name = input.nextName(); if (CAPABILITIES.equals(name)) { input.beginObject(); while (input.hasNext()) { name = input.nextName(); if (ALWAYS_MATCH.equals(name)) { return input.read(MAP_TYPE); } input.skipValue(); } input.endObject(); } else { input.skipValue(); } } } return null; }
/** * Calls {@link #close} if not already closed, and then resets this object back to its initial * state, for reuse. If data was buffered to a file, it will be deleted. * * @throws IOException if an I/O error occurred while deleting the file buffer */ public synchronized void reset() throws IOException { try { close(); } finally { if (memory == null) { memory = new MemoryOutput(); } else { memory.reset(); } out = memory; if (file != null) { File deleteMe = file; file = null; if (!deleteMe.delete()) { throw new IOException("Could not delete: " + deleteMe); } } } }