本文整理匯總了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");
}
}