use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method writer.
/**
* Example of writing a blob's content through a writer.
*/
// [TARGET writer(BlobInfo, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void writer(String bucketName, String blobName) throws IOException {
// [START writer]
BlobId blobId = BlobId.of(bucketName, blobName);
byte[] content = "Hello, World!".getBytes(UTF_8);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
try (WriteChannel writer = storage.writer(blobInfo)) {
try {
writer.write(ByteBuffer.wrap(content, 0, content.length));
} catch (Exception ex) {
// handle exception
}
}
// [END writer]
}
use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.
the class StorageImplTest method testWriterWithEncryptionKey.
@Test
public void testWriterWithEncryptionKey() {
BlobInfo info = BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build();
EasyMock.expect(storageRpcMock.open(info.toPb(), ENCRYPTION_KEY_OPTIONS)).andReturn("upload-id").times(2);
EasyMock.replay(storageRpcMock);
initializeService();
WriteChannel channel = storage.writer(info, BlobWriteOption.encryptionKey(KEY));
assertNotNull(channel);
assertTrue(channel.isOpen());
channel = storage.writer(info, BlobWriteOption.encryptionKey(BASE64_KEY));
assertNotNull(channel);
assertTrue(channel.isOpen());
}
use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.
the class TableDataWriteChannelTest method testSaveAndRestoreClosed.
@Test
public void testSaveAndRestoreClosed() throws IOException {
expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andReturn(UPLOAD_ID);
Capture<byte[]> capturedBuffer = Capture.newInstance();
expect(bigqueryRpcMock.write(eq(UPLOAD_ID), capture(capturedBuffer), eq(0), eq(0L), eq(0), eq(true))).andReturn(job.toPb());
replay(bigqueryRpcMock);
writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION);
writer.close();
assertEquals(job, writer.getJob());
RestorableState<WriteChannel> writerState = writer.capture();
RestorableState<WriteChannel> expectedWriterState = TableDataWriteChannel.StateImpl.builder(options, LOAD_CONFIGURATION, UPLOAD_ID, job).setBuffer(null).setChunkSize(DEFAULT_CHUNK_SIZE).setIsOpen(false).setPosition(0).build();
WriteChannel restoredWriter = writerState.restore();
assertArrayEquals(new byte[0], capturedBuffer.getValue());
assertEquals(expectedWriterState, restoredWriter.capture());
}
Aggregations