test_class.declarator: BUCKET_NAME = "bucket-name"
test_class.modifier: private static final
test_class.original_string: private static final String BUCKET_NAME = "bucket-name";
test_class.type: String
test_class.var_name: BUCKET_NAME
test_class.declarator: OBJECT_NAME = "object-name"
test_class.modifier: private static final
test_class.original_string: private static final String OBJECT_NAME = "object-name";
test_class.type: String
test_class.var_name: OBJECT_NAME
test_class.declarator: OBJECT_GENERATION = 7
test_class.modifier: private static final
test_class.original_string: private static final long OBJECT_GENERATION = 7;
test_class.type: long
test_class.var_name: OBJECT_GENERATION
test_class.declarator: OBJECT_SIZE =
        GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE + 10
test_class.modifier: private static final
test_class.original_string: private static final int OBJECT_SIZE =
        GoogleCloudStorageReadOptions.DEFAULT_MIN_RANGE_REQUEST_SIZE + 10;
test_class.type: int
test_class.var_name: OBJECT_SIZE
test_class.declarator: DEFAULT_OBJECT_CRC32C = 185327488
test_class.modifier: private static final
test_class.original_string: private static final int DEFAULT_OBJECT_CRC32C = 185327488;
test_class.type: int
test_class.var_name: DEFAULT_OBJECT_CRC32C
test_class.declarator: DEFAULT_OBJECT =
        Object.newBuilder()
            .setBucket(BUCKET_NAME)
            .setName(OBJECT_NAME)
            .setSize(OBJECT_SIZE)
            .setCrc32C(UInt32Value.newBuilder().setValue(DEFAULT_OBJECT_CRC32C))
            .setGeneration(OBJECT_GENERATION)
            .build()
test_class.modifier: private static
test_class.original_string: private static Object DEFAULT_OBJECT =
        Object.newBuilder()
            .setBucket(BUCKET_NAME)
            .setName(OBJECT_NAME)
            .setSize(OBJECT_SIZE)
            .setCrc32C(UInt32Value.newBuilder().setValue(DEFAULT_OBJECT_CRC32C))
            .setGeneration(OBJECT_GENERATION)
            .build();
test_class.type: Object
test_class.var_name: DEFAULT_OBJECT
test_class.declarator: GET_OBJECT_REQUEST =
        GetObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).build()
test_class.modifier: private static
test_class.original_string: private static GetObjectRequest GET_OBJECT_REQUEST =
        GetObjectRequest.newBuilder().setBucket(BUCKET_NAME).setObject(OBJECT_NAME).build();
test_class.type: GetObjectRequest
test_class.var_name: GET_OBJECT_REQUEST
test_class.declarator: GET_OBJECT_MEDIA_REQUEST =
        GetObjectMediaRequest.newBuilder()
            .setBucket(BUCKET_NAME)
            .setObject(OBJECT_NAME)
            .setGeneration(OBJECT_GENERATION)
            .build()
test_class.modifier: private static
test_class.original_string: private static GetObjectMediaRequest GET_OBJECT_MEDIA_REQUEST =
        GetObjectMediaRequest.newBuilder()
            .setBucket(BUCKET_NAME)
            .setObject(OBJECT_NAME)
            .setGeneration(OBJECT_GENERATION)
            .build();
test_class.type: GetObjectMediaRequest
test_class.var_name: GET_OBJECT_MEDIA_REQUEST
test_class.declarator: grpcCleanup = new GrpcCleanupRule()
test_class.modifier: @Rule public final
test_class.original_string: @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
test_class.type: GrpcCleanupRule
test_class.var_name: grpcCleanup
test_class.declarator: stub
test_class.modifier: private
test_class.original_string: private StorageBlockingStub stub;
test_class.type: StorageBlockingStub
test_class.var_name: stub
test_class.declarator: fakeService
test_class.modifier: private
test_class.original_string: private FakeService fakeService;
test_class.type: FakeService
test_class.var_name: fakeService
test_class.declarator: executor = Executors.newCachedThreadPool()
test_class.modifier: private
test_class.original_string: private ExecutorService executor = Executors.newCachedThreadPool();
test_class.type: ExecutorService
test_class.var_name: executor
test_class.file: gcsio/src/test/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageGrpcReadChannelTest.java
test_class.identifier: GoogleCloudStorageGrpcReadChannelTest
test_case.body: @Test
    public void readToBufferWithArrayOffsetFailsWithInvalidMessageChecksum() throws Exception {
      fakeService.setReturnIncorrectMessageChecksum();
      GoogleCloudStorageReadOptions options =
          GoogleCloudStorageReadOptions.builder().setGrpcChecksumsEnabled(true).build();
      GoogleCloudStorageGrpcReadChannel readChannel = newReadChannel(options);
  
      byte[] array = new byte[OBJECT_SIZE + 100];
      // `ByteBuffer.slice` generates a ByteBuffer with a non-zero `arrayOffset`.
      ByteBuffer buffer = ByteBuffer.wrap(array, 50, OBJECT_SIZE).slice();
  
      IOException thrown = assertThrows(IOException.class, () -> readChannel.read(buffer));
      assertTrue(
          thrown.getMessage() + " should have contained 'checksum'",
          thrown.getMessage().contains("checksum"));
    }
test_case.class_method_signature: GoogleCloudStorageGrpcReadChannelTest.readToBufferWithArrayOffsetFailsWithInvalidMessageChecksum()
test_case.full_signature: @Test public void readToBufferWithArrayOffsetFailsWithInvalidMessageChecksum()
test_case.identifier: readToBufferWithArrayOffsetFailsWithInvalidMessageChecksum
test_case.invocations: setReturnIncorrectMessageChecksum build setGrpcChecksumsEnabled builder newReadChannel slice wrap assertThrows read assertTrue getMessage contains getMessage
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void readToBufferWithArrayOffsetFailsWithInvalidMessageChecksum()
focal_class.declarator: logger = GoogleLogger.forEnclosingClass()
focal_class.modifier: private static final
focal_class.original_string: private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
focal_class.type: GoogleLogger
focal_class.var_name: logger
focal_class.declarator: READ_STREAM_TIMEOUT = Duration.ofMinutes(20)
focal_class.modifier: private static final
focal_class.original_string: private static final Duration READ_STREAM_TIMEOUT = Duration.ofMinutes(20);
focal_class.type: Duration
focal_class.var_name: READ_STREAM_TIMEOUT
focal_class.declarator: requestContext
focal_class.modifier: @Nullable
focal_class.original_string: @Nullable CancellableContext requestContext;
focal_class.type: CancellableContext
focal_class.var_name: requestContext
focal_class.declarator: readStrategy
focal_class.original_string: Fadvise readStrategy;
focal_class.type: Fadvise
focal_class.var_name: readStrategy
focal_class.declarator: stub
focal_class.modifier: private
focal_class.original_string: private StorageBlockingStub stub;
focal_class.type: StorageBlockingStub
focal_class.var_name: stub
focal_class.declarator: bucketName
focal_class.modifier: private
focal_class.original_string: private String bucketName;
focal_class.type: String
focal_class.var_name: bucketName
focal_class.declarator: objectName
focal_class.modifier: private
focal_class.original_string: private String objectName;
focal_class.type: String
focal_class.var_name: objectName
focal_class.declarator: objectGeneration
focal_class.modifier: private
focal_class.original_string: private long objectGeneration;
focal_class.type: long
focal_class.var_name: objectGeneration
focal_class.declarator: objectSize
focal_class.modifier: private
focal_class.original_string: private long objectSize;
focal_class.type: long
focal_class.var_name: objectSize
focal_class.declarator: channelIsOpen = true
focal_class.modifier: private
focal_class.original_string: private boolean channelIsOpen = true;
focal_class.type: boolean
focal_class.var_name: channelIsOpen
focal_class.declarator: position = 0
focal_class.modifier: private
focal_class.original_string: private long position = 0;
focal_class.type: long
focal_class.var_name: position
focal_class.declarator: bytesToSkipBeforeReading = 0
focal_class.modifier: private
focal_class.original_string: private long bytesToSkipBeforeReading = 0;
focal_class.type: long
focal_class.var_name: bytesToSkipBeforeReading
focal_class.declarator: bufferedContent = null
focal_class.modifier: @Nullable private
focal_class.original_string: @Nullable private ByteString bufferedContent = null;
focal_class.type: ByteString
focal_class.var_name: bufferedContent
focal_class.declarator: bufferedContentReadOffset = 0
focal_class.modifier: private
focal_class.original_string: private int bufferedContentReadOffset = 0;
focal_class.type: int
focal_class.var_name: bufferedContentReadOffset
focal_class.declarator: resIterator = null
focal_class.modifier: @Nullable private
focal_class.original_string: @Nullable private Iterator<GetObjectMediaResponse> resIterator = null;
focal_class.type: Iterator<GetObjectMediaResponse>
focal_class.var_name: resIterator
focal_class.declarator: readOptions
focal_class.modifier: private
focal_class.original_string: private GoogleCloudStorageReadOptions readOptions;
focal_class.type: GoogleCloudStorageReadOptions
focal_class.var_name: readOptions
focal_class.file: gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageGrpcReadChannel.java
focal_class.identifier: GoogleCloudStorageGrpcReadChannel
focal_class.interfaces: implements SeekableByteChannel
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.GoogleCloudStorageGrpcReadChannel(
        StorageBlockingStub gcsGrpcBlockingStub,
        String bucketName,
        String objectName,
        long objectGeneration,
        long objectSize,
        GoogleCloudStorageReadOptions readOptions)
focal_class.full_signature: private  GoogleCloudStorageGrpcReadChannel(
        StorageBlockingStub gcsGrpcBlockingStub,
        String bucketName,
        String objectName,
        long objectGeneration,
        long objectSize,
        GoogleCloudStorageReadOptions readOptions)
focal_class.identifier: GoogleCloudStorageGrpcReadChannel
focal_class.modifiers: private
focal_class.parameters: (
        StorageBlockingStub gcsGrpcBlockingStub,
        String bucketName,
        String objectName,
        long objectGeneration,
        long objectSize,
        GoogleCloudStorageReadOptions readOptions)
focal_class.signature:  GoogleCloudStorageGrpcReadChannel(
        StorageBlockingStub gcsGrpcBlockingStub,
        String bucketName,
        String objectName,
        long objectGeneration,
        long objectSize,
        GoogleCloudStorageReadOptions readOptions)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.open(
        StorageBlockingStub stub,
        String bucketName,
        String objectName,
        GoogleCloudStorageReadOptions readOptions)
focal_class.full_signature: public static GoogleCloudStorageGrpcReadChannel open(
        StorageBlockingStub stub,
        String bucketName,
        String objectName,
        GoogleCloudStorageReadOptions readOptions)
focal_class.identifier: open
focal_class.modifiers: public static
focal_class.parameters: (
        StorageBlockingStub stub,
        String bucketName,
        String objectName,
        GoogleCloudStorageReadOptions readOptions)
focal_class.return: GoogleCloudStorageGrpcReadChannel
focal_class.signature: GoogleCloudStorageGrpcReadChannel open(
        StorageBlockingStub stub,
        String bucketName,
        String objectName,
        GoogleCloudStorageReadOptions readOptions)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.convertError(
        StatusRuntimeException error, String bucketName, String objectName)
focal_class.full_signature: private static IOException convertError(
        StatusRuntimeException error, String bucketName, String objectName)
focal_class.identifier: convertError
focal_class.modifiers: private static
focal_class.parameters: (
        StatusRuntimeException error, String bucketName, String objectName)
focal_class.return: IOException
focal_class.signature: IOException convertError(
        StatusRuntimeException error, String bucketName, String objectName)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.put(ByteString source, int offset, int size, ByteBuffer dest)
focal_class.full_signature: private static final void put(ByteString source, int offset, int size, ByteBuffer dest)
focal_class.identifier: put
focal_class.modifiers: private static final
focal_class.parameters: (ByteString source, int offset, int size, ByteBuffer dest)
focal_class.return: void
focal_class.signature: void put(ByteString source, int offset, int size, ByteBuffer dest)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.readBufferedContentInto(ByteBuffer byteBuffer)
focal_class.full_signature: private int readBufferedContentInto(ByteBuffer byteBuffer)
focal_class.identifier: readBufferedContentInto
focal_class.modifiers: private
focal_class.parameters: (ByteBuffer byteBuffer)
focal_class.return: int
focal_class.signature: int readBufferedContentInto(ByteBuffer byteBuffer)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.read(ByteBuffer byteBuffer)
focal_class.full_signature: @Override public int read(ByteBuffer byteBuffer)
focal_class.identifier: read
focal_class.modifiers: @Override public
focal_class.parameters: (ByteBuffer byteBuffer)
focal_class.return: int
focal_class.signature: int read(ByteBuffer byteBuffer)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.requestObjectMedia(OptionalLong bytesToRead)
focal_class.full_signature: private void requestObjectMedia(OptionalLong bytesToRead)
focal_class.identifier: requestObjectMedia
focal_class.modifiers: private
focal_class.parameters: (OptionalLong bytesToRead)
focal_class.return: void
focal_class.signature: void requestObjectMedia(OptionalLong bytesToRead)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.cancelCurrentRequest()
focal_class.full_signature: private void cancelCurrentRequest()
focal_class.identifier: cancelCurrentRequest
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void cancelCurrentRequest()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.moreServerContent()
focal_class.full_signature: private boolean moreServerContent()
focal_class.identifier: moreServerContent
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean moreServerContent()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.write(ByteBuffer byteBuffer)
focal_class.full_signature: @Override public int write(ByteBuffer byteBuffer)
focal_class.identifier: write
focal_class.modifiers: @Override public
focal_class.parameters: (ByteBuffer byteBuffer)
focal_class.return: int
focal_class.signature: int write(ByteBuffer byteBuffer)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.position()
focal_class.full_signature: @Override public long position()
focal_class.identifier: position
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: long
focal_class.signature: long position()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.resourceIdString()
focal_class.full_signature: private String resourceIdString()
focal_class.identifier: resourceIdString
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String resourceIdString()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.position(long newPosition)
focal_class.full_signature: @Override public SeekableByteChannel position(long newPosition)
focal_class.identifier: position
focal_class.modifiers: @Override public
focal_class.parameters: (long newPosition)
focal_class.return: SeekableByteChannel
focal_class.signature: SeekableByteChannel position(long newPosition)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.size()
focal_class.full_signature: @Override public long size()
focal_class.identifier: size
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: long
focal_class.signature: long size()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.truncate(long l)
focal_class.full_signature: @Override public SeekableByteChannel truncate(long l)
focal_class.identifier: truncate
focal_class.modifiers: @Override public
focal_class.parameters: (long l)
focal_class.return: SeekableByteChannel
focal_class.signature: SeekableByteChannel truncate(long l)
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.isOpen()
focal_class.full_signature: @Override public boolean isOpen()
focal_class.identifier: isOpen
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean isOpen()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.close()
focal_class.full_signature: @Override public void close()
focal_class.identifier: close
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void close()
focal_class.class_method_signature: GoogleCloudStorageGrpcReadChannel.toString()
focal_class.full_signature: @Override public String toString()
focal_class.identifier: toString
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String toString()
focal_method.body: @Override
    public int read(ByteBuffer byteBuffer) throws IOException {
      logger.atFine().log(
          "GCS gRPC read request for up to %d bytes at offset %d from object %s",
          byteBuffer.remaining(), position(), objectName);
  
      if (!isOpen()) {
        throw new ClosedChannelException();
      }
  
      int bytesRead = 0;
  
      // The server responds in 2MB chunks, but the client can ask for less than that. We store the
      // remainder in bufferedContent and return pieces of that on the next read call (and flush
      // that buffer if there is a seek).
      if (bufferedContent != null) {
        bytesRead += readBufferedContentInto(byteBuffer);
      }
      if (!byteBuffer.hasRemaining()) {
        return bytesRead;
      }
      if (position == objectSize) {
        return bytesRead > 0 ? bytesRead : -1;
      }
      if (resIterator == null) {
        OptionalLong bytesToRead;
        if (readStrategy == Fadvise.RANDOM) {
          long randomRangeSize =
              Math.max(byteBuffer.remaining(), readOptions.getMinRangeRequestSize());
          bytesToRead = OptionalLong.of(randomRangeSize);
        } else {
          bytesToRead = OptionalLong.empty();
        }
        requestObjectMedia(bytesToRead);
      }
      while (moreServerContent() && byteBuffer.hasRemaining()) {
        GetObjectMediaResponse res = resIterator.next();
  
        ByteString content = res.getChecksummedData().getContent();
        if (bytesToSkipBeforeReading >= 0 && bytesToSkipBeforeReading < content.size()) {
          content = res.getChecksummedData().getContent().substring((int) bytesToSkipBeforeReading);
          bytesToSkipBeforeReading = 0;
        } else if (bytesToSkipBeforeReading >= content.size()) {
          bytesToSkipBeforeReading -= content.size();
          continue;
        }
  
        if (readOptions.isGrpcChecksumsEnabled() && res.getChecksummedData().hasCrc32C()) {
          // TODO: Concatenate all these hashes together and compare the result at the end.
          Hasher messageHasher = Hashing.crc32c().newHasher();
          messageHasher.putBytes(res.getChecksummedData().getContent().toByteArray());
          int calculatedChecksum = messageHasher.hash().asInt();
          int expectedChecksum = res.getChecksummedData().getCrc32C().getValue();
          if (calculatedChecksum != expectedChecksum) {
            throw new IOException(
                String.format(
                    "For %s: Message checksum didn't match. Expected %s, got %s.",
                    this, expectedChecksum, calculatedChecksum));
          }
        }
  
        boolean responseSizeLargerThanRemainingBuffer = content.size() > byteBuffer.remaining();
        int bytesToWrite =
            responseSizeLargerThanRemainingBuffer ? byteBuffer.remaining() : content.size();
        put(content, 0, bytesToWrite, byteBuffer);
        bytesRead += bytesToWrite;
        position += bytesToWrite;
  
        if (responseSizeLargerThanRemainingBuffer) {
          bufferedContent = content;
          bufferedContentReadOffset = bytesToWrite;
        }
      }
      return bytesRead;
    }
focal_method.class_method_signature: GoogleCloudStorageGrpcReadChannel.read(ByteBuffer byteBuffer)
focal_method.full_signature: @Override public int read(ByteBuffer byteBuffer)
focal_method.identifier: read
focal_method.invocations: log atFine remaining position isOpen readBufferedContentInto hasRemaining max remaining getMinRangeRequestSize of empty requestObjectMedia moreServerContent hasRemaining next getContent getChecksummedData size substring getContent getChecksummedData size size isGrpcChecksumsEnabled hasCrc32C getChecksummedData newHasher crc32c putBytes toByteArray getContent getChecksummedData asInt hash getValue getCrc32C getChecksummedData format size remaining remaining size put
focal_method.modifiers: @Override public
focal_method.parameters: (ByteBuffer byteBuffer)
focal_method.return: int
focal_method.signature: int read(ByteBuffer byteBuffer)