1 /* 2 * Copyright 2020 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import java.nio.ByteBuffer; 20 import javax.annotation.Nullable; 21 22 /** 23 * Extension to an {@link java.io.InputStream} whose content can be accessed as {@link 24 * ByteBuffer}s. 25 * 26 * <p>This can be used for optimizing the case for the consumer of a {@link ByteBuffer}-backed 27 * input stream supports efficient reading from {@link ByteBuffer}s directly. This turns the reader 28 * interface from an {@link java.io.InputStream} to {@link ByteBuffer}s, without copying the 29 * content to a byte array and read from it. 30 */ 31 public interface HasByteBuffer { 32 33 /** 34 * Indicates whether or not {@link #getByteBuffer} operation is supported. 35 */ byteBufferSupported()36 boolean byteBufferSupported(); 37 38 /** 39 * Gets a {@link ByteBuffer} containing some bytes of the content next to be read, or {@code 40 * null} if has reached end of the content. The number of bytes contained in the returned buffer 41 * is implementation specific. Calling this method does not change the position of the input 42 * stream. The returned buffer's content should not be modified, but the position, limit, and 43 * mark may be changed. Operations for changing the position, limit, and mark of the returned 44 * buffer does not affect the position, limit, and mark of this input stream. This is an optional 45 * method, so callers should first check {@link #byteBufferSupported}. 46 * 47 * @throws UnsupportedOperationException if this operation is not supported. 48 */ 49 @Nullable getByteBuffer()50 ByteBuffer getByteBuffer(); 51 } 52