當前位置: 首頁>>代碼示例>>Java>>正文


Java Message.toByteArray方法代碼示例

本文整理匯總了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));
}
 
開發者ID:CloudLandGame,項目名稱:CloudLand-Server,代碼行數:27,代碼來源:IdBasedProtobufEncoder.java

示例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);
}
 
開發者ID:ninelook,項目名稱:wecard-server,代碼行數:15,代碼來源:Client.java

示例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");
    }
}
 
開發者ID:Sixt,項目名稱:ja-micro,代碼行數:30,代碼來源:ProtobufHandler.java


注:本文中的com.google.protobuf.Message.toByteArray方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。