2012-07-03 216 views
2

是由Protostuff代碼生成器生成的類與Protobuf創建的類兼容嗎?Protobuf和Protostuff之間的兼容性

我試圖(德)序列化一些簡單的消息,並得到了一些例外:

始祖文件(WrapperClass.proto)

package tutorial; 
option java_package = "com.example.tutorial"; 
message ProjectId { 
    required int32 id = 1; 
} 
message UserId { 
    required ProjectId project = 1; 
    required int32 projectUserId = 2; 
} 
message ChannelId { 
    required ProjectId project = 1; 
    required string name = 2; 
} 

Protostuff到的Protobuf測試(例如)

ProjectId projectId = new ProjectId(1); 
byte[] projectarray = ProtostuffIOUtil.toByteArray(projectId, ProjectId.getSchema(), buffer); 
com.example.tutorial.WrapperClass.ProjectId returnBufProject = com.example.tutorial.WrapperClass.ProjectId.parseFrom(projectarray); 

問題:

一切工作的專案編號,但對於用戶ID和渠道ID(一切稍微複雜一點),我得到:

com.google.protobuf.InvalidProtocolBufferException: Message missing required fields: project 
    at com.google.protobuf.UninitializedMessageException.asInvalidProtocolBufferException(UninitializedMessageException.java:81) 
    at com.example.tutorial.WrapperClass$ChannelId$Builder.buildParsed(Test.java:1278) 
    at com.example.tutorial.WrapperClass$ChannelId$Builder.access$17(Test.java:1273) 
    at com.example.tutorial.WrapperClass$ChannelId.parseFrom(Test.java:1142) 
    ... 

和周圍的其他方式:

的Protobuf到Protostuff測試(例如)

com.example.tutorial.WrapperClass.ProjectId projectId2 = com.example.tutorial.WrapperClass.ProjectId.newBuilder().setId(1).build(); 
byte[] project2array = projectId2.toByteArray(); 
ProjectId returnStufProject = new ProjectId(); 
ProtostuffIOUtil.mergeFrom(project2array, returnStufProject, ProjectId.getSchema()); 

問題

再次,對於比其他專案編號一切,有一個例外:

java.lang.RuntimeException: Reading from a byte array threw an IOException (should never happen). 
    at com.dyuproject.protostuff.IOUtil.mergeFrom(IOUtil.java:53) 
    at com.dyuproject.protostuff.ProtostuffIOUtil.mergeFrom(ProtostuffIOUtil.java:96) 
    at JacksonTest.main(JacksonTest.java:92) 
Caused by: com.dyuproject.protostuff.ProtobufException: Protocol message contained an invalid tag (zero). 
    at com.dyuproject.protostuff.ProtobufException.invalidTag(ProtobufException.java:98) 
    at com.dyuproject.protostuff.ByteArrayInput.readFieldNumber(ByteArrayInput.java:220) 
    at com.example.tutorial.ProjectId$1.mergeFrom(ProjectId.java:115) 
    at com.example.tutorial.ProjectId$1.mergeFrom(ProjectId.java:1) 
    at com.dyuproject.protostuff.ByteArrayInput.mergeObjectEncodedAsGroup(ByteArrayInput.java:390) 
    at com.dyuproject.protostuff.ByteArrayInput.mergeObject(ByteArrayInput.java:362) 
    at com.example.tutorial.UserId$1.mergeFrom(UserId.java:138) 
    at com.example.tutorial.UserId$1.mergeFrom(UserId.java:1) 
    at com.dyuproject.protostuff.IOUtil.mergeFrom(IOUtil.java:43) 
    ... 2 more 

我豈是不可能辦到的事還是我唯一做錯了什麼?

回答

4

的問題很簡單: 而不是使用ProtostuffIOUtil到的(反)序列化我的消息我需要使用ProtobufIOUtil

+0

我只是在類似情況下遇到了類似的例外。在我的情況下,我傳遞給ProtobufIOUtil.mergeFrom()一個指定了不正確長度的字節數組。 – RenniePet

相關問題