@Override public ReadChannel restore() { BlobReadChannel channel = new BlobReadChannel(serviceOptions, blob, requestOptions); channel.lastEtag = lastEtag; channel.position = position; channel.isOpen = isOpen; channel.endOfStream = endOfStream; channel.chunkSize = chunkSize; return channel; }
@Override public ReadChannel reader(BlobId blob, BlobSourceOption... options) { Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options); return new BlobReadChannel(getOptions(), blob, optionsMap); }
@Override public ReadChannel reader(String bucket, String blob, BlobSourceOption... options) { Map<StorageRpc.Option, ?> optionsMap = optionMap(options); return new BlobReadChannel(getOptions(), BlobId.of(bucket, blob), optionsMap); }
@Test public void testStateEquals() { replay(storageRpcMock); reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); @SuppressWarnings("resource") // avoid closing when you don't want partial writes to GCS 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()); }
@Test public void testCreate() { replay(storageRpcMock); reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); assertTrue(reader.isOpen()); }
@Test public void testReadClosed() throws IOException { replay(storageRpcMock); reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); reader.close(); try { ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); reader.read(readBuffer); fail("Expected BlobReadChannel read to throw ClosedChannelException"); } catch (ClosedChannelException ex) { // expected } }
@Test public void testReadFinish() throws IOException { reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); byte[] result = {}; ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)); replay(storageRpcMock); assertEquals(-1, reader.read(readBuffer)); }
@Test public void testReadBuffered() throws IOException { reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE); ByteBuffer firstReadBuffer = ByteBuffer.allocate(42); ByteBuffer secondReadBuffer = ByteBuffer.allocate(42); expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)); replay(storageRpcMock); reader.read(firstReadBuffer); reader.read(secondReadBuffer); assertArrayEquals(Arrays.copyOf(result, firstReadBuffer.capacity()), firstReadBuffer.array()); assertArrayEquals( Arrays.copyOfRange( result, firstReadBuffer.capacity(), firstReadBuffer.capacity() + secondReadBuffer.capacity()), secondReadBuffer.array()); }
@Test public void testSeek() throws IOException { reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); reader.seek(42); byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE); ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)); replay(storageRpcMock); reader.read(readBuffer); assertArrayEquals(result, readBuffer.array()); }
@Test public void testSaveAndRestore() throws IOException { byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE); byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE); ByteBuffer firstReadBuffer = ByteBuffer.allocate(42); ByteBuffer secondReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", firstResult)); expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", secondResult)); replay(storageRpcMock); reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); reader.read(firstReadBuffer); RestorableState<ReadChannel> readerState = reader.capture(); ReadChannel restoredReader = readerState.restore(); restoredReader.read(secondReadBuffer); assertArrayEquals( Arrays.copyOf(firstResult, firstReadBuffer.capacity()), firstReadBuffer.array()); assertArrayEquals(secondResult, secondReadBuffer.array()); }
@Test public void testReadBig() throws IOException { reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); reader.setChunkSize(CUSTOM_CHUNK_SIZE); byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE); byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE); ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); ByteBuffer secondReadBuffer = ByteBuffer.allocate(42); expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", firstResult)); expect( storageRpcMock.read( BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, CUSTOM_CHUNK_SIZE)) .andReturn(Tuple.of("etag", secondResult)); replay(storageRpcMock); reader.read(firstReadBuffer); reader.read(secondReadBuffer); assertArrayEquals(firstResult, firstReadBuffer.array()); assertArrayEquals( Arrays.copyOf(secondResult, secondReadBuffer.capacity()), secondReadBuffer.array()); }
@Test public void testReadGenerationChanged() throws IOException { BlobId blobId = BlobId.of(BUCKET_NAME, BLOB_NAME); reader = new BlobReadChannel(options, blobId, EMPTY_RPC_OPTIONS); byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE); byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE); ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); ByteBuffer secondReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); expect(storageRpcMock.read(blobId.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag1", firstResult)); expect( storageRpcMock.read( blobId.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag2", secondResult)); replay(storageRpcMock); reader.read(firstReadBuffer); try { reader.read(secondReadBuffer); fail("Expected ReadChannel read to throw StorageException"); } catch (StorageException ex) { StringBuilder messageBuilder = new StringBuilder(); messageBuilder.append("Blob ").append(blobId).append(" was updated while reading"); assertEquals(messageBuilder.toString(), ex.getMessage()); } }
@Test public void testClose() { replay(storageRpcMock); reader = new BlobReadChannel(options, BLOB_ID, EMPTY_RPC_OPTIONS); assertTrue(reader.isOpen()); reader.close(); assertTrue(!reader.isOpen()); }
@Override protected Restorable<?>[] restorableObjects() { StorageOptions options = StorageOptions.newBuilder().setProjectId("p2").build(); ReadChannel reader = new BlobReadChannel(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS); // avoid closing when you don't want partial writes to GCS upon failure @SuppressWarnings("resource") BlobWriteChannel writer = new BlobWriteChannel( options, BlobInfo.newBuilder(BlobId.of("b", "n")).build(), "upload-id"); return new Restorable<?>[] {reader, writer}; } }
@Override public ReadChannel restore() { BlobReadChannel channel = new BlobReadChannel(serviceOptions, blob, requestOptions); channel.lastEtag = lastEtag; channel.position = position; channel.isOpen = isOpen; channel.endOfStream = endOfStream; channel.chunkSize = chunkSize; return channel; }
@Override public ReadChannel reader(BlobId blob, BlobSourceOption... options) { Map<StorageRpc.Option, ?> optionsMap = optionMap(blob, options); return new BlobReadChannel(getOptions(), blob, optionsMap); }
@Override public ReadChannel reader(String bucket, String blob, BlobSourceOption... options) { Map<StorageRpc.Option, ?> optionsMap = optionMap(options); return new BlobReadChannel(getOptions(), BlobId.of(bucket, blob), optionsMap); }