com.google.protobuf.nano.CodedOutputByteBufferNano Java Examples

The following examples show how to use com.google.protobuf.nano.CodedOutputByteBufferNano. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NanoProtoInputStream.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public int read(byte[] b, int off, int len) throws IOException {
  if (message != null) {
    int size = message.getSerializedSize();
    if (size == 0) {
      message = null;
      partial = null;
      return -1;
    }
    if (len >= size) {
      // This is the only case that is zero-copy.
      CodedOutputByteBufferNano output = CodedOutputByteBufferNano.newInstance(b, off, size);
      message.writeTo(output);
      output.checkNoSpaceLeft();

      message = null;
      partial = null;
      return size;
    }

    toPartial();
  }
  if (partial != null) {
    return partial.read(b, off, len);
  }
  return -1;
}
 
Example #2
Source File: ProtobufEncoderNano.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void encode(
        ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception {
    final int size = msg.getSerializedSize();
    final ByteBuf buffer = ctx.alloc().heapBuffer(size, size);
    final byte[] array = buffer.array();
    CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array,
            buffer.arrayOffset(), buffer.capacity());
    msg.writeTo(cobbn);
    buffer.writerIndex(size);
    out.add(buffer);
}
 
Example #3
Source File: ProtoUtils.java    From PainlessMusicPlayer with Apache License 2.0 4 votes vote down vote up
@NonNull
public static byte[] toByteArray(@NonNull final MessageNano messageNano) throws IOException {
    final byte[] output = new byte[messageNano.getSerializedSize()];
    messageNano.writeTo(CodedOutputByteBufferNano.newInstance(output));
    return output;
}