下面列出了怎么用com.google.protobuf.nano.CodedOutputByteBufferNano的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
@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);
}
@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;
}