Search in sources :

Example 16 with ReadChannel

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));
    }
}
Also used : Random(java.util.Random) WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 17 with ReadChannel

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]
}
Also used : BlobId(com.google.cloud.storage.BlobId) ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel)

Example 18 with ReadChannel

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]
}
Also used : ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel)

Example 19 with ReadChannel

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)));
}
Also used : ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 20 with ReadChannel

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());
}
Also used : ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Aggregations

ReadChannel (com.google.cloud.ReadChannel)20 Test (org.junit.Test)15 ByteBuffer (java.nio.ByteBuffer)11 BlobInfo (com.google.cloud.storage.BlobInfo)7 WriteChannel (com.google.cloud.WriteChannel)5 Blob (com.google.cloud.storage.Blob)3 BlobId (com.google.cloud.storage.BlobId)2 StorageException (com.google.cloud.storage.StorageException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Random (java.util.Random)2 Restorable (com.google.cloud.Restorable)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1