Search in sources :

Example 11 with WriteChannel

use of com.google.cloud.WriteChannel 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 12 with WriteChannel

use of com.google.cloud.WriteChannel 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 13 with WriteChannel

use of com.google.cloud.WriteChannel 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 14 with WriteChannel

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

the class ITStorageTest method testWriteChannelExistingBlob.

@Test
public void testWriteChannelExistingBlob() throws IOException {
    String blobName = "test-write-channel-existing-blob";
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
    storage.create(blob);
    byte[] stringBytes;
    try (WriteChannel writer = storage.writer(blob)) {
        stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
        writer.write(ByteBuffer.wrap(stringBytes));
    }
    assertArrayEquals(stringBytes, storage.readAllBytes(blob.getBlobId()));
    assertTrue(storage.delete(BUCKET, blobName));
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 15 with WriteChannel

use of com.google.cloud.WriteChannel 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)

Aggregations

WriteChannel (com.google.cloud.WriteChannel)18 Test (org.junit.Test)17 BlobInfo (com.google.cloud.storage.BlobInfo)8 ByteBuffer (java.nio.ByteBuffer)7 ReadChannel (com.google.cloud.ReadChannel)5 StorageException (com.google.cloud.storage.StorageException)3 Random (java.util.Random)2 EasyMock.captureLong (org.easymock.EasyMock.captureLong)2 Blob (com.google.cloud.storage.Blob)1 BlobId (com.google.cloud.storage.BlobId)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1