Code example for ByteBuffer

Methods: get, hasRemaining

0
    public ByteBufferInputStream(final ByteBuffer byteBuffer) {
        this.byteBuffer = byteBuffer;
    } 
 
    public int read() throws IOException { 
        if (!byteBuffer.hasRemaining()) {
            return -1; 
        } 
        return byteBuffer.get();
    } 
 
    public static Message buildMessage(Message prototype, ByteBuffer byteBuffer) {
        try { 
            return prototype.toBuilder().mergeFrom(new ByteBufferInputStream(byteBuffer)).build();
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            //Never happen 
            throw new RuntimeException(e);
        } 
    }