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(); }
byte[] data = newPreFilledByteArray(dataSize); FileBackedOutputStream out = new FileBackedOutputStream(fileThreshold, resetOnFinalize); ByteSource source = out.asByteSource(); int chunk1 = Math.min(dataSize, fileThreshold); int chunk2 = dataSize - chunk1;
payload.writeTo(writer); try (InputStream rawIn = os.asByteSource().openBufferedStream(); BufferedInputStream contentStream = new BufferedInputStream(rawIn)) {
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; }
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; }
private @Nullable Collection<Map<String, Object>> getFirstMatch() 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 (FIRST_MATCH.equals(name)) { return input.read(LIST_OF_MAPS_TYPE); } input.skipValue(); } input.endObject(); } else { input.skipValue(); } } } return null; }
/** * Returns a supplier that may be used to retrieve the data buffered * by this stream. This method returns the same object as * {@link #asByteSource()}. * * @deprecated Use {@link #asByteSource()} instead. This method is scheduled * to be removed in Guava 16.0. */ @Deprecated public InputSupplier<InputStream> getSupplier() { return asByteSource(); }
/** * Returns a supplier that may be used to retrieve the data buffered * by this stream. This method returns the same object as * {@link #asByteSource()}. * * @deprecated Use {@link #asByteSource()} instead. This method is scheduled * to be removed in Guava 16.0. */ @Deprecated public InputSupplier<InputStream> getSupplier() { return asByteSource(); }
private void writeMetaData(JsonOutput out) 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(); switch (name) { case CAPABILITIES: case DESIRED_CAPABILITIES: case REQUIRED_CAPABILITIES: input.skipValue(); break; default: out.name(name); out.write(input.read(Object.class)); break; } } } }
/** * Returns a supplier that may be used to retrieve the data buffered * by this stream. This method returns the same object as * {@link #asByteSource()}. * * @deprecated Use {@link #asByteSource()} instead. This method is scheduled * to be removed in Guava 16.0. */ @Deprecated public InputSupplier<InputStream> getSupplier() { return asByteSource(); }
/** * Returns a supplier that may be used to retrieve the data buffered * by this stream. This method returns the same object as * {@link #asByteSource()}. * * @deprecated Use {@link #asByteSource()} instead. This method is scheduled * to be removed in Guava 16.0. */ @Deprecated public InputSupplier<InputStream> getSupplier() { return asByteSource(); }
/** * Returns a readable {@link ByteSource} view of the data that has been written to this stream. * Must not be called after {@link #close()} is called, otherwise an {@link IllegalStateException} * will be thrown. * * @return ByteSource of the data * @throws IOException throws an exception if the stream is closed */ public ByteSource asByteSource() throws IOException { checkIsClosed(); return fileBackedOutputStream.asByteSource(); }
@Override public InputStream getInputStream() throws IOException { if (!connected) { connect(); } if (subEntryName != null) { if (subEntryOutputStream != null) { return subEntryOutputStream.asByteSource().openBufferedStream(); } else { throw new IOException("Failed to load " + subEntryName); } } else if (entryName != null) { if (entryOutputStream != null) { return entryOutputStream.asByteSource().openBufferedStream(); } else { throw new IOException("Failed to load " + entryName); } } else { return new JarInputStream(new FileInputStream(file)); } }
@NonNull String getCombinedOutputString() throws IOException { return combinedOutput.asByteSource().asCharSource(Charsets.UTF_8).read(); }
@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); } }
@NonNull String getStandardOutputString() throws IOException { return standardOutput.asByteSource().asCharSource(Charsets.UTF_8).read(); }
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; }
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(); }