use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadAndWriteChannelsWithDifferentFileSize.
@Test
public void testReadAndWriteChannelsWithDifferentFileSize() throws IOException {
String blobNamePrefix = "test-read-and-write-channels-blob-";
int[] blobSizes = { 0, 700, 1024 * 256, 2 * 1024 * 1024, 4 * 1024 * 1024, 4 * 1024 * 1024 + 1 };
Random rnd = new Random();
for (int blobSize : blobSizes) {
String blobName = blobNamePrefix + blobSize;
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
byte[] bytes = new byte[blobSize];
rnd.nextBytes(bytes);
try (WriteChannel writer = storage.writer(blob)) {
writer.write(ByteBuffer.wrap(bytes));
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (ReadChannel reader = storage.reader(blob.getBlobId())) {
ByteBuffer buffer = ByteBuffer.allocate(64 * 1024);
while (reader.read(buffer) > 0) {
buffer.flip();
output.write(buffer.array(), 0, buffer.limit());
buffer.clear();
}
}
assertArrayEquals(bytes, output.toByteArray());
assertTrue(storage.delete(BUCKET, blobName));
}
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method readerFromId.
/**
* Example of reading a blob's content through a reader.
*/
// [TARGET reader(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void readerFromId(String bucketName, String blobName) throws IOException {
// [START readerFromId]
BlobId blobId = BlobId.of(bucketName, blobName);
try (ReadChannel reader = storage.reader(blobId)) {
ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
while (reader.read(bytes) > 0) {
bytes.flip();
// do something with bytes
bytes.clear();
}
}
// [END readerFromId]
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class BlobSnippets method reader.
/**
* 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]
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class BlobTest method testReaderWithDecryptionKey.
@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)));
}
use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.
the class BlobReadChannelTest method testStateEquals.
@Test
public void testStateEquals() {
replay(storageRpcMock);
reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS);
// avoid closing when you don't want partial writes to GCS
@SuppressWarnings("resource") ReadChannel secondReader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS);
RestorableState<ReadChannel> state = reader.capture();
RestorableState<ReadChannel> secondState = secondReader.capture();
assertEquals(state, secondState);
assertEquals(state.hashCode(), secondState.hashCode());
assertEquals(state.toString(), secondState.toString());
}
Aggregations