use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadAndWriteCaptureChannels.
@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));
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadAndWriteChannelWithEncryptionKey.
@Test
public void testReadAndWriteChannelWithEncryptionKey() throws IOException {
String blobName = "test-read-write-channel-with-customer-key-blob";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
byte[] stringBytes;
try (WriteChannel writer = storage.writer(blob, Storage.BlobWriteOption.encryptionKey(BASE64_KEY))) {
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
writer.write(ByteBuffer.wrap(stringBytes));
}
ByteBuffer readBytes;
ByteBuffer readStringBytes;
try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.decryptionKey(KEY))) {
readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
readStringBytes = ByteBuffer.allocate(stringBytes.length);
reader.read(readBytes);
reader.read(readStringBytes);
}
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
assertTrue(storage.delete(BUCKET, blobName));
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class StorageImplTest method testReader.
@Test
public void testReader() {
EasyMock.replay(storageRpcMock);
initializeService();
ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME1);
assertNotNull(channel);
assertTrue(channel.isOpen());
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadCompressedBlob.
@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));
}
}
blob.delete();
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class StorageImplTest method testReaderWithDecryptionKey.
@Test
public void testReaderWithDecryptionKey() throws IOException {
byte[] result = new byte[DEFAULT_CHUNK_SIZE];
EasyMock.expect(storageRpcMock.read(BLOB_INFO2.toPb(), ENCRYPTION_KEY_OPTIONS, 0, DEFAULT_CHUNK_SIZE)).andReturn(Tuple.of("etag", result)).times(2);
EasyMock.replay(storageRpcMock);
initializeService();
ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BlobSourceOption.decryptionKey(KEY));
assertNotNull(channel);
assertTrue(channel.isOpen());
channel.read(ByteBuffer.allocate(42));
channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BlobSourceOption.decryptionKey(BASE64_KEY));
assertNotNull(channel);
assertTrue(channel.isOpen());
channel.read(ByteBuffer.allocate(42));
}
Aggregations