Java Code Examples for com.google.protobuf.MessageLite#toByteArray()

The following examples show how to use com.google.protobuf.MessageLite#toByteArray() . 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: SocketChannel.java    From xraft with MIT License 5 votes vote down vote up
private void write(OutputStream output, int messageType, MessageLite message) throws IOException {
    DataOutputStream dataOutput = new DataOutputStream(output);
    byte[] messageBytes = message.toByteArray();
    dataOutput.writeInt(messageType);
    dataOutput.writeInt(messageBytes.length);
    dataOutput.write(messageBytes);
    dataOutput.flush();
}
 
Example 2
Source File: ProtobufEncoder.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ByteBuffer encode(MessageLite msg){

	if (msg == null)
		return null;

	byte[] msgBuffer = msg.toByteArray();
	if (msgBuffer == null || msgBuffer.length == 0) {
		return null;
	}
	
	if ( !isCustomPkg ) {
		return ByteBuffer.wrap(msgBuffer);
	}
	
	//
	int totalSize =  4 + msgBuffer.length;
	ByteBuffer buffer = ByteBuffer.allocate(totalSize);
	buffer.put( (byte) ((totalSize >> 24) & 0xFF) );
	buffer.put( (byte) ((totalSize >> 16) & 0xFF) );
	buffer.put( (byte) ((totalSize >> 8) & 0xFF) );
	buffer.put( (byte) (totalSize & 0xFF) );
	//
	buffer.put( msgBuffer );
	buffer.flip();
	return buffer;
}
 
Example 3
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testGetByteArray() throws Exception {
  // Make sure it compiles with the MessageLite type.
  MessageLite data = TypicalData.newBuilder().setMyInt(42).build();
  byte[] bytes = data.toByteArray();
  byte[] expected = new byte[]{ 0x08, 0x2A };
  checkBytes(expected, bytes);
}
 
Example 4
Source File: RpcResponseEncoder.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(IoSession session, Object message,
		ProtocolEncoderOutput out) throws Exception {

	MessageLite resp = (MessageLite) message;
	byte[] respBytes = resp.toByteArray();
	IoBuffer buf = IoBuffer.allocate(HEADER_LEN+respBytes.length);
	buf.putInt(respBytes.length);
	buf.put(respBytes);
	buf.flip();
	out.write(buf);

}
 
Example 5
Source File: Encoder.java    From xraft with MIT License 4 votes vote down vote up
private void writeMessage(int messageType, MessageLite message, ByteBuf out) throws IOException {
    out.writeInt(messageType);
    byte[] bytes = message.toByteArray();
    out.writeInt(bytes.length);
    out.writeBytes(bytes);
}
 
Example 6
Source File: StandardProtocol.java    From rpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public Object processRequest(Object request) {
    long startTime = System.currentTimeMillis();
    RPCMessage<RPCHeader.RequestHeader> fullRequest = (RPCMessage<RPCHeader.RequestHeader>) request;
    RPCHeader.RequestHeader requestHeader = fullRequest.getHeader();
    // default response
    RPCHeader.ResponseHeader.Builder responseHeader = RPCHeader.ResponseHeader.newBuilder()
            .setCallId(fullRequest.getHeader().getCallId())
            .setResCode(RPCHeader.ResCode.RES_FAIL);
    RPCMessage<RPCHeader.ResponseHeader> fullResponse = new RPCMessage<RPCHeader.ResponseHeader>();

    String serviceName = requestHeader.getServiceName();
    String methodName = requestHeader.getMethodName();
    ServiceManager serviceManager = ServiceManager.getInstance();
    ServiceInfo serviceInfo = serviceManager.getService(serviceName, methodName);
    if (serviceInfo == null) {
        LOG.error("can not find service info, serviceName={}, methodName={}", serviceName, methodName);
        responseHeader.setResCode(RPCHeader.ResCode.RES_FAIL);
        responseHeader.setResMsg("can not find service info");
        fullResponse.setHeader(responseHeader.build());
        return fullResponse;
    }
    try {
        Method parseMethod = serviceInfo.getParseFromForRequest();
        MessageLite requestMessage = (MessageLite) parseMethod.invoke(null, fullRequest.getBody());
        MessageLite responseMessage =
                (MessageLite) serviceInfo.getMethod().invoke(
                        serviceInfo.getService(), requestMessage);
        byte[] responseBody = responseMessage.toByteArray();

        responseHeader.setResCode(RPCHeader.ResCode.RES_SUCCESS).setResMsg("");
        fullResponse.setHeader(responseHeader.build());
        fullResponse.setBody(responseBody);
        long endTime = System.currentTimeMillis();
        LOG.debug("elapseMS={} service={} method={} callId={}",
                endTime - startTime, requestHeader.getServiceName(),
                requestHeader.getMethodName(), requestHeader.getCallId());
        return fullResponse;
    } catch (Exception ex) {
        LOG.error("invoke method failed, msg={}", ex.getMessage());
        responseHeader.setResCode(RPCHeader.ResCode.RES_FAIL);
        responseHeader.setResMsg("invoke method failed");
        fullResponse.setHeader(responseHeader.build());
        return fullResponse;
    }
}
 
Example 7
Source File: OutOfBandMessage.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public Payload(MessageLite item) {
  this.type = item.getClass().getName();
  this.bytes = item.toByteArray();
}
 
Example 8
Source File: JRedisSerializationUtils.java    From springJredisCache with Apache License 2.0 2 votes vote down vote up
/**
 * @param messageLite 序列化对应的类型
 * @return
 * @throws JRedisCacheException
 */
public static byte[] protoSerialize(MessageLite messageLite) throws JRedisCacheException {
    assert (messageLite != null);
    return messageLite.toByteArray();
}