GoogleCloudStorageReadChannel implements SeekableByteChannel { @Override public long size() throws IOException { throwIfNotOpen(); if (!metadataInitialized) { initMetadata(fetchInitialMetadata()); } return size; } GoogleCloudStorageReadChannel(
      Storage gcs,
      String bucketName,
      String objectName,
      ApiErrorExtractor errorExtractor,
      ClientRequestHelper<StorageObject> requestHelper);  GoogleCloudStorageReadChannel(
      Storage gcs,
      String bucketName,
      String objectName,
      ApiErrorExtractor errorExtractor,
      ClientRequestHelper<StorageObject> requestHelper,
      @Nonnull GoogleCloudStorageReadOptions readOptions);  GoogleCloudStorageReadChannel(
      Storage gcs,
      StorageResourceId resourceId,
      ApiErrorExtractor errorExtractor,
      ClientRequestHelper<StorageObject> requestHelper,
      @Nonnull GoogleCloudStorageReadOptions readOptions); protected  GoogleCloudStorageReadChannel(
      String bucketName, String objectName, @Nonnull GoogleCloudStorageReadOptions readOptions); void setMaxRetries(int maxRetries); @Override int read(ByteBuffer buffer); @Override SeekableByteChannel truncate(long size); @Override int write(ByteBuffer src); @Override boolean isOpen(); @Override void close(); @Override long position(); @Override SeekableByteChannel position(long newPosition); @Override long size();  }
@Test public void metadataInitialization_eager() throws IOException { StorageObject object = newStorageObject(BUCKET_NAME, OBJECT_NAME); MockHttpTransport transport = mockTransport(jsonDataResponse(object)); List<HttpRequest> requests = new ArrayList<>(); Storage storage = new Storage(transport, JSON_FACTORY, requests::add); GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setFastFailOnNotFound(true).build(); GoogleCloudStorageReadChannel readChannel = createReadChannel(storage, options); assertThat(requests).hasSize(1); assertThat(readChannel.size()).isEqualTo(object.getSize().longValue()); assertThat(requests).hasSize(1); }
@Test public void metadataInitialization_lazy() throws IOException { StorageObject object = newStorageObject(BUCKET_NAME, OBJECT_NAME); MockHttpTransport transport = mockTransport(jsonDataResponse(object)); List<HttpRequest> requests = new ArrayList<>(); Storage storage = new Storage(transport, JSON_FACTORY, requests::add); GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setFastFailOnNotFound(false).build(); GoogleCloudStorageReadChannel readChannel = createReadChannel(storage, options); assertThat(requests).isEmpty(); assertThat(readChannel.size()).isEqualTo(object.getSize().longValue()); assertThat(requests).hasSize(1); }
@Test public void size_whenObjectIsGzipEncoded_shouldBeSetToMaxLongValue() throws IOException { MockHttpTransport transport = mockTransport( jsonDataResponse( newStorageObject(BUCKET_NAME, OBJECT_NAME).setContentEncoding("gzip"))); Storage storage = new Storage(transport, JSON_FACTORY, r -> {}); GoogleCloudStorageReadChannel readChannel = createReadChannel(storage, GoogleCloudStorageReadOptions.DEFAULT); assertThat(readChannel.size()).isEqualTo(Long.MAX_VALUE); }
@Test public void lazyReadFileAtSpecificGeneration_fails_whenReadConsistencyStrict() throws IOException { long generation = 5L; MockHttpTransport transport = mockTransport(jsonErrorResponse(ErrorResponses.NOT_FOUND)); List<HttpRequest> requests = new ArrayList<>(); Storage storage = new Storage(transport, JSON_FACTORY, requests::add); GoogleCloudStorageReadOptions options = GoogleCloudStorageReadOptions.builder().setFastFailOnNotFound(false).build(); GoogleCloudStorageReadChannel readChannel = createReadChannel(storage, options, generation); assertThrows(FileNotFoundException.class, () -> readChannel.size()); }