本文整理汇总了Java中com.google.protobuf.Message.toByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java Message.toByteArray方法的具体用法?Java Message.toByteArray怎么用?Java Message.toByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Message
的用法示例。
在下文中一共展示了Message.toByteArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import com.google.protobuf.Message; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, MessageOrBuilder in, List<Object> out)
throws Exception {
Message msg = in instanceof Message ? (Message) in : ((Message.Builder) in).build();
int typeId = register.getTypeId(msg.getClass());
if (typeId == Integer.MIN_VALUE) {
throw new IllegalArgumentException("Unrecognisable message type, maybe not registered! ");
}
byte[] messageData = msg.toByteArray();
if (messageData.length <= 0) {
out.add(ByteBufAllocator.DEFAULT.heapBuffer().writeInt(typeId)
.writeInt(0));
return;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream def = new GZIPOutputStream(bos);
def.write(messageData);
def.flush();
def.close();
byte[] compressedData = bos.toByteArray();
out.add(ByteBufAllocator.DEFAULT.heapBuffer().writeInt(typeId).writeInt(compressedData.length)
.writeBytes(compressedData));
}
示例2: send
import com.google.protobuf.Message; //导入方法依赖的package包/类
/**
* 向当前客户端发送数据
* @param message
*/
public void send(Message message) {
byte[] bytes = message.toByteArray();
ByteBuf b = Unpooled.buffer(bytes.length);
b.writeBytes(bytes);
WebSocketFrame frame = new BinaryWebSocketFrame(b);
channel.writeAndFlush(frame);
}
示例3: sendSuccessfulResponse
import com.google.protobuf.Message; //导入方法依赖的package包/类
private void sendSuccessfulResponse(HttpServletResponse response,
RpcEnvelope.Request rpcRequest,
Message pbResponse) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
RpcEnvelope.Response rpcResponse = RpcEnvelope.Response.newBuilder().
setServiceMethod(rpcRequest.getServiceMethod()).
setSequenceNumber(rpcRequest.getSequenceNumber()).build();
byte responseHeader[] = rpcResponse.toByteArray();
byte responseBody[];
if (pbResponse == null) {
responseBody = new byte[0];
} else {
responseBody = pbResponse.toByteArray();
}
try {
ServletOutputStream out = response.getOutputStream();
out.write(Ints.toByteArray(responseHeader.length));
out.write(responseHeader);
out.write(Ints.toByteArray(responseBody.length));
out.write(responseBody);
} catch (IOException ioex) {
//there is nothing we can do, client probably went away
logger.debug("Caught IOException, assuming client disconnected");
}
}