/** Example of reading the blob's content through a reader. */ // [TARGET reader(BlobSourceOption...)] public void reader() throws IOException { // [START reader] try (ReadChannel reader = blob.reader()) { ByteBuffer bytes = ByteBuffer.allocate(64 * 1024); while (reader.read(bytes) > 0) { bytes.flip(); // do something with bytes bytes.clear(); } } // [END reader] }
/** Example of reading just a portion of the blob's content. */ // [TARGET reader(BlobSourceOption...)] // [VARIABLE 1] // [VARIABLE 8] public byte[] readContentRange(int start, int end) throws IOException { // [START readContentRange] try (ReadChannel reader = blob.reader()) { reader.seek(start); ByteBuffer bytes = ByteBuffer.allocate(end - start); reader.read(bytes); return bytes.array(); } // [END readContentRange] }
/** * Downloads this blob to the given file path using specified blob read options. * * @param path destination * @param options blob read options * @throws StorageException upon failure */ public void downloadTo(Path path, BlobSourceOption... options) { try (OutputStream outputStream = Files.newOutputStream(path); ReadChannel reader = reader(options)) { WritableByteChannel channel = Channels.newChannel(outputStream); ByteBuffer bytes = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); while (reader.read(bytes) > 0) { bytes.flip(); channel.write(bytes); bytes.clear(); } } catch (IOException e) { throw new StorageException(e); } }
InputStream getStream(UUID jobId, String keyName) { String blobName = getDataKeyName(jobId, keyName); Blob blob = bucket.get(blobName); ReadChannel channel = blob.reader(); return Channels.newInputStream(channel); }
@Test public void testReader() throws Exception { initializeExpectedBlob(2); ReadChannel channel = createMock(ReadChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.reader(BLOB_INFO.getBlobId())).andReturn(channel); replay(storage); initializeBlob(); assertSame(channel, blob.reader()); }
@Test public void testReaderWithDecryptionKey() throws Exception { initializeExpectedBlob(2); ReadChannel channel = createMock(ReadChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect( storage.reader( BLOB_INFO.getBlobId(), Storage.BlobSourceOption.decryptionKey(BASE64_KEY))) .andReturn(channel) .times(2); replay(storage); initializeBlob(); assertSame(channel, blob.reader(BlobSourceOption.decryptionKey(BASE64_KEY))); assertSame(channel, blob.reader(BlobSourceOption.decryptionKey(KEY))); }
/** * Downloads this blob to the given file path using specified blob read options. * * @param path destination * @param options blob read options * @throws StorageException upon failure */ public void downloadTo(Path path, BlobSourceOption... options) { try (OutputStream outputStream = Files.newOutputStream(path); ReadChannel reader = reader(options)) { WritableByteChannel channel = Channels.newChannel(outputStream); ByteBuffer bytes = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); while (reader.read(bytes) > 0) { bytes.flip(); channel.write(bytes); bytes.clear(); } } catch (IOException e) { throw new StorageException(e); } }
@Override public InputStream getInputStream() throws IOException { if (isBucket()) { throw new IllegalStateException("Cannot open an input stream to a bucket: '" + this.location + "'"); } else { return Channels.newInputStream(throwExceptionForNullBlob(getBlob()).reader()); } }
@Override public InputStream getInputStream() throws IOException { if (isBucket()) { throw new IllegalStateException("Cannot open an input stream to a bucket: '" + this.location + "'"); } else { return Channels.newInputStream(throwExceptionForNullBlob(getBlob()).reader()); } }
final Blob blob = storageProvider.getStorage().get(gcsBlob.getBlobId()); return new InputStream() { final ReadChannel reader = blob.reader();