Search in sources :

Example 6 with ReadChannel

use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testReadAndWriteChannels.

@Test
public void testReadAndWriteChannels() throws IOException {
    String blobName = "test-read-and-write-channels-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    byte[] stringBytes;
    try (WriteChannel writer = storage.writer(blob)) {
        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())) {
        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));
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 7 with ReadChannel

use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testReaderWithOptions.

@Test
public void testReaderWithOptions() throws IOException {
    byte[] result = new byte[DEFAULT_CHUNK_SIZE];
    EasyMock.expect(storageRpcMock.read(BLOB_INFO2.toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)).andReturn(Tuple.of("etag", result));
    EasyMock.replay(storageRpcMock);
    initializeService();
    ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION);
    assertNotNull(channel);
    assertTrue(channel.isOpen());
    channel.read(ByteBuffer.allocate(42));
}
Also used : ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 8 with ReadChannel

use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testReadChannelFailUpdatedGeneration.

@Test
public void testReadChannelFailUpdatedGeneration() throws IOException {
    String blobName = "test-read-blob-fail-updated-generation";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Random random = new Random();
    int chunkSize = 1024;
    int blobSize = 2 * chunkSize;
    byte[] content = new byte[blobSize];
    random.nextBytes(content);
    Blob remoteBlob = storage.create(blob, content);
    assertNotNull(remoteBlob);
    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);
        assertEquals(chunkSize, numReadBytes);
        assertArrayEquals(Arrays.copyOf(content, chunkSize), readBytes.array());
        try (WriteChannel writer = storage.writer(blob)) {
            byte[] newContent = new byte[blobSize];
            random.nextBytes(newContent);
            int numWrittenBytes = writer.write(ByteBuffer.wrap(newContent));
            assertEquals(blobSize, numWrittenBytes);
        }
        readBytes = ByteBuffer.allocate(chunkSize);
        reader.read(readBytes);
        fail("StorageException was expected");
    } catch (StorageException ex) {
        StringBuilder messageBuilder = new StringBuilder();
        messageBuilder.append("Blob ").append(blob.getBlobId()).append(" was updated while reading");
        assertEquals(messageBuilder.toString(), ex.getMessage());
    }
    assertTrue(storage.delete(BUCKET, blobName));
}
Also used : Blob(com.google.cloud.storage.Blob) Random(java.util.Random) WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) ByteBuffer(java.nio.ByteBuffer) StorageException(com.google.cloud.storage.StorageException) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 9 with ReadChannel

use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testReadChannelFail.

@Test
public void testReadChannelFail() throws IOException {
    String blobName = "test-read-channel-blob-fail";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.metagenerationMatch(-1L))) {
        reader.read(ByteBuffer.allocate(42));
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.generationMatch(-1L))) {
        reader.read(ByteBuffer.allocate(42));
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    BlobId blobIdWrongGeneration = BlobId.of(BUCKET, blobName, -1L);
    try (ReadChannel reader = storage.reader(blobIdWrongGeneration, Storage.BlobSourceOption.generationMatch())) {
        reader.read(ByteBuffer.allocate(42));
        fail("StorageException was expected");
    } catch (StorageException ex) {
    // expected
    }
    assertTrue(remoteBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) StorageException(com.google.cloud.storage.StorageException) BlobId(com.google.cloud.storage.BlobId) ReadChannel(com.google.cloud.ReadChannel) Test(org.junit.Test)

Example 10 with ReadChannel

use of com.google.cloud.ReadChannel in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method readerFromStrings.

/**
   * Example of reading a blob's content through a reader.
   */
// [TARGET reader(String, String, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void readerFromStrings(String bucketName, String blobName) throws IOException {
    // [START readerFromStrings]
    try (ReadChannel reader = storage.reader(bucketName, blobName)) {
        ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
        while (reader.read(bytes) > 0) {
            bytes.flip();
            // do something with bytes
            bytes.clear();
        }
    }
// [END readerFromStrings]
}
Also used : ByteBuffer(java.nio.ByteBuffer) ReadChannel(com.google.cloud.ReadChannel)

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