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