来自C ++的Java中的ParseFrom消息失败

时间:2019-05-02 14:24:01

标签: java c++ rabbitmq base64 protocol-buffers

我有一个问题/问题。

我正在使用RabbitMq从C ++向Java(播放框架)发送消息。因此,在C ++方面,我使用了SerializeToString函数(也尝试将SerializeToArraychar*一起使用)。使用Stringbyte []的Java无法使用ParseFrom。

详细信息:在我的邮件中,我以String的形式发送了超过50万个字符的base64图像。错误是:

CodedInputStream encountered an embedded string or message which claimed to have negative size

没有base64字符串和其他属性的邮件,ParseFrom正常运行。

这是完整的错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[InvalidProtocolBufferException: CodedInputStream encountered an embedded string or message which claimed to have negative size.]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:323)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:243)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:382)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:380)
    at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:417)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:41)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:64)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
Caused by: com.google.protobuf.InvalidProtocolBufferException: CodedInputStream encountered an embedded string or message which claimed to have negative size.
    at com.google.protobuf.InvalidProtocolBufferException.negativeSize(InvalidProtocolBufferException.java:92)
    at com.google.protobuf.CodedInputStream$ArrayDecoder.pushLimit(CodedInputStream.java:1179)
    at com.google.protobuf.CodedInputStream$ArrayDecoder.readMessage(CodedInputStream.java:881)
    at model.RequestOrResponse$Response.dynamicMethod(RequestOrResponse.java:1542)
    at com.google.protobuf.GeneratedMessageLite.parsePartialFrom(GeneratedMessageLite.java:1597)
    at com.google.protobuf.GeneratedMessageLite.parsePartialFrom(GeneratedMessageLite.java:1630)
    at com.google.protobuf.GeneratedMessageLite.parseFrom(GeneratedMessageLite.java:1746)
    at model.RequestOrResponse$Response.parseFrom(RequestOrResponse.java:1232)
    at controllers.SubjectController.get(SubjectController.java:195)
    at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$14(Routes.scala:187)

3 个答案:

答案 0 :(得分:2)

尝试使用较小的图像(例如50k个字符)测试代码,然后查看其是否有效。如果图像超过100万个字符(超过200万个字节)将是一个问题,因为CodedInputStream $ ArrayDecoder.pushLimit将获取第一个字节作为总字节数,并且int在溢出前的正数限制为2147483647,因此该溢出可能设置大小否定异常。

答案 1 :(得分:2)

ParseFrom之前,尝试在C ++端对base64进行编码,并在Java端进行解码。
了解更多here

答案 2 :(得分:1)

我解决了这个错误。

我做的第一件事是使用一个较小的图像,如@ leonardo-goes所说的,但是我得到了错误:

[InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length.]

我尝试按照@joão-pedro-bernardino所说进行编码和解构后,就可以了。