如何将协议缓冲区消息转换为byte []

时间:2014-07-23 05:43:22

标签: java protocol-buffers

这里是我的代码我仍然是这个协议缓冲区的新手如何将协议缓冲区转换为byte []以便我可以加密

for (ClientRequest req : check.getClientRequestGroup().getClientRequests()) {
            ClientRequestProtos.GetUnixDormantUsers.Builder guduBuilder =     ClientRequestProtos.GetUnixDormantUsers.newBuilder();
            guduBuilder.setCheckFiles(true);
            guduBuilder.setCheckProcesses(true);
            guduBuilder.setThreshold(30);
            guduBuilder.addExcludeUser("root");
            guduBuilder.addExcludeUser("bin");
            ClientRequestProtos.GetUnixDormantUsers guduMessage = guduBuilder.build();

            ClientRequestProtos.ClientRequest.Builder clientBuilder = ClientRequestProtos.ClientRequest.newBuilder();
            clientBuilder.setId(req.getId());
            clientBuilder.setSentAt(req.getSentAt().getTime());
            clientBuilder.setType(ClientRequestType.GET_UNIX_DORMANT_USERS);
            clientBuilder.setGetUnixDormantUsers(guduMessage);
            ClientRequestProtos.ClientRequest clientMessage = clientBuilder.build();



String ipAddress = mapHostIp.get(req.getHost());
                GenericUrl url = new GenericUrl("http://"+ ipAddress+":5736/request");
byte[] content; // convert protocol buffers to byte[]


            byte[] encrypted = Encrypt.encrypt(content);
            HttpRequest request = requestFactory.buildPostRequest(url, new ByteArrayContent(null, encrypted));
            request.execute();
        }

1 个答案:

答案 0 :(得分:3)

只需在邮件上调用toByteArray方法:

byte[] content = clientMessage.toByteArray();