Search in sources :

Example 11 with ReadChannel

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));
}
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 12 with ReadChannel

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));
}
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 13 with ReadChannel

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

Example 14 with ReadChannel

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

Example 15 with ReadChannel

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));
}
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