private void innerOpen() throws IOException { this.channel = gcsStorage.reader(file, blobSourceOptions); if (position > 0) { channel.seek(position); } }
@Override public SeekableByteChannel position(long newPosition) throws IOException { checkArgument(newPosition >= 0); synchronized (this) { checkOpen(); if (newPosition == position) { return this; } channel.seek(newPosition); position = newPosition; return this; } }
/** Example of reading just a portion of the blob's content. */ // [TARGET reader(BlobSourceOption...)] // [VARIABLE 1] // [VARIABLE 8] public byte[] readContentRange(int start, int end) throws IOException { // [START readContentRange] try (ReadChannel reader = blob.reader()) { reader.seek(start); ByteBuffer bytes = ByteBuffer.allocate(end - start); reader.read(bytes); return bytes.array(); } // [END readContentRange] }
@Test public void testChannelPositionDoesNotGetTruncatedToInt() throws IOException { // This position value will overflow to a negative value if a long -> int cast is attempted long startPosition = 11918483280L; ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); // Invoke CloudStorageReadChannel.create() to trigger a call to the private // CloudStorageReadChannel.innerOpen() method, which does a seek on our gcsChannel. CloudStorageReadChannel.create( gcsStorage, file, startPosition, 1, CloudStorageConfiguration.DEFAULT, ""); // Confirm that our position did not overflow during the seek in // CloudStorageReadChannel.innerOpen() verify(gcsChannel).seek(captor.capture()); assertThat(captor.getValue()).isEqualTo(startPosition); } }
@Test public void testSetPosition() throws IOException { assertThat(chan.position()).isEqualTo(0L); assertThat(chan.size()).isEqualTo(42L); chan.position(1L); assertThat(chan.position()).isEqualTo(1L); assertThat(chan.size()).isEqualTo(42L); verify(gcsChannel).seek(1); verify(gcsChannel, times(5)).isOpen(); }
@Override public SeekableByteChannel position(long newPosition) throws IOException { checkArgument(newPosition >= 0); synchronized (this) { checkOpen(); if (newPosition == position) { return this; } channel.seek(newPosition); position = newPosition; return this; } }
private void innerOpen() throws IOException { this.channel = gcsStorage.reader(file); if (position > 0) { channel.seek(position); } }
@Override public byte[] load(final Value v) throws IOException { final BlobId blobId = GcsBlob.of(v._key).getBlobId(); final byte[] contentBytes = MemoryManager.malloc1(v._max); final ByteBuffer wrappingBuffer = ByteBuffer.wrap(contentBytes); final Key k = v._key; long offset = 0; // Skip offset based on chunk number if (k._kb[0] == Key.CHK) { offset = FileVec.chunkOffset(k); // The offset } final ReadChannel reader = storageProvider.getStorage().reader(blobId); reader.seek(offset); reader.read(wrappingBuffer); return contentBytes; }