下面列出了com.google.protobuf.MessageLite#toByteArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
@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;
}
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);
}
@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);
}
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);
}
@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;
}
}
public Payload(MessageLite item) {
this.type = item.getClass().getName();
this.bytes = item.toByteArray();
}
/**
* @param messageLite 序列化对应的类型
* @return
* @throws JRedisCacheException
*/
public static byte[] protoSerialize(MessageLite messageLite) throws JRedisCacheException {
assert (messageLite != null);
return messageLite.toByteArray();
}