@Test public void testReadCompressedBlob() throws IOException { String blobName = "test-read-compressed-blob"; BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(BUCKET, blobName)) .setContentType("text/plain") .setContentEncoding("gzip") .build(); Blob blob = storage.create(blobInfo, COMPRESSED_CONTENT); try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { try (ReadChannel reader = storage.reader(BlobId.of(BUCKET, blobName))) { reader.setChunkSize(8); ByteBuffer buffer = ByteBuffer.allocate(8); while (reader.read(buffer) != -1) { buffer.flip(); output.write(buffer.array(), 0, buffer.limit()); buffer.clear(); } } assertArrayEquals( BLOB_STRING_CONTENT.getBytes(UTF_8), storage.readAllBytes(BUCKET, blobName)); assertArrayEquals(COMPRESSED_CONTENT, output.toByteArray()); try (GZIPInputStream zipInput = new GZIPInputStream(new ByteArrayInputStream(output.toByteArray()))) { assertArrayEquals(BLOB_STRING_CONTENT.getBytes(UTF_8), ByteStreams.toByteArray(zipInput)); } } }
assertEquals(blobSize, (long) remoteBlob.getSize()); try (ReadChannel reader = storage.reader(blob.getBlobId())) { reader.setChunkSize(chunkSize); ByteBuffer readBytes = ByteBuffer.allocate(chunkSize); int numReadBytes = reader.read(readBytes);
@Test public void testReadAndWriteCaptureChannels() throws IOException { String blobName = "test-read-and-write-capture-channels-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); byte[] stringBytes; WriteChannel writer = storage.writer(blob); stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); RestorableState<WriteChannel> writerState = writer.capture(); WriteChannel secondWriter = writerState.restore(); secondWriter.write(ByteBuffer.wrap(stringBytes)); secondWriter.close(); ByteBuffer readBytes; ByteBuffer readStringBytes; ReadChannel reader = storage.reader(blob.getBlobId()); reader.setChunkSize(BLOB_BYTE_CONTENT.length); readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length); reader.read(readBytes); RestorableState<ReadChannel> readerState = reader.capture(); ReadChannel secondReader = readerState.restore(); readStringBytes = ByteBuffer.allocate(stringBytes.length); secondReader.read(readStringBytes); reader.close(); secondReader.close(); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array()); assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8)); assertTrue(storage.delete(BUCKET, blobName)); }