英文:
protobuf java problems when sending/receving bytes type
问题
InputStream is = new ByteArrayInputStream(hexStringToByteArray(s1));
fi.kapsi.koti.jpa.nanopb.TestProtocol.MessageFinale.parseFrom(is);
System.out.println("hello");
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
Exception in thread "Thread-1" java.lang.ExceptionInInitializerError
at fi.kapsi.koti.jpa.nanopb.TestProtocol.<clinit>(TestProtocol.java:4650)
at fi.kapsi.koti.jpa.nanopb.TestProtocol$MessageFinale.internalGetFieldAccessorTable(TestProtocol.java:213)
at com.google.protobuf.GeneratedMessage.getDescriptorForType(GeneratedMessage.java:98)
at com.google.protobuf.AbstractMessage$Builder.findMissingFields(AbstractMessage.java:789)
at com.google.protobuf.AbstractMessage$Builder.findMissingFields(AbstractMessage.java:780)
at com.google.protobuf.AbstractMessage$Builder.newUninitializedMessageException(AbstractMessage.java:770)
at com.google.protobuf.AbstractMessage.newUninitializedMessageException(AbstractMessage.java:237)
at com.google.protobuf.AbstractParser.newUninitializedMessageException(AbstractParser.java:57)
at com.google.protobuf.AbstractParser.checkMessageInitialized(AbstractParser.java:71)
at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:253)
at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:259)
at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:49)
at fi.kapsi.koti.jpa.nanopb.TestProtocol$MessageFinale.parseDelimitedFrom(TestProtocol.java:574)
at com.mkyong.hashing.server.RequestHandler.run(RequestHandler.java:169)
Caused by: java.lang.IllegalArgumentException: Invalid embedded
descriptor for "nanopb.proto".
at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:301)
at fi.kapsi.koti.jpa.nanopb.Nanopb.<clinit>(Nanopb.java:1765)
... 14 more Caused by: com.google.protobuf.Descriptors$DescriptorValidationException:
nanopb.proto: Dependencies passed to FileDescriptor.buildFrom() don't
match those listed in the FileDescriptorProto.
at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:246)
at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:299)
... 15 more
message MessageFinale {
enum Type {
AUTH = 0;
FILE = 1;
ERRORCODE = 2;
GPS = 3;
};
required Type type = 1;
optional Auth auth = 2 [(nanopb).type = FT_CALLBACK];
optional File file = 3 [(nanopb).type = FT_CALLBACK];
optional ErrorCode errorCode = 4 [(nanopb).type = FT_CALLBACK];
optional GPS gps = 5 [(nanopb).type = FT_CALLBACK];
}
message Auth {
enum Type {
REQ = 0;
RES = 1;
}
required Type type = 1;
message Req {
required uint64 imei = 1;
required uint32 password = 2 [default = 1234];
}
optional Req req = 2 [(nanopb).type = FT_CALLBACK];
message Res {
}
optional Res res = 3 [(nanopb).type = FT_CALLBACK];
}
message File {
required bytes chunk = 1;
}
message ErrorCode {
required int32 errorId = 1;
enum Status {
ACTIVE = 1;
INACTIVE = 2;
DELETED = 3;
}
required Status status = 2;
}
message GPS {
required int32 lat = 1;
required int32 lng = 2;
}
Please let me know if there is anything else I can assist you with.
英文:
Hello I am trying to deserialise this object in hex when i use other type it is working (int , float, string ...) Only it happens with bytes.
> 8D0408011A880454696D657374616D703B4B4F434F203E2043414E43796C6963203E203130303031205B44435D3B4B4F434F203E2043414E43796C6963203E203130303032205B44435D3B4B4F434F203E2043414E43796C6963203E203130303033205B44435D3B4B4F434F203E2043414E43796C6963203E203130303034205B44435D3B4B4F434F203E2043414E43796C6963203E203130303035205B44435D3B4B4F434F203E2043414E43796C6963203E203130313031205B44435D3B4B4F434F203E2043414E43796C6963203E203130313032205B44435D3B4B4F434F203E2043414E43796C6963203E203130313033205B44435D3B4B4F434F203E2043414E43796C6963203E203130313034205B44435D3B4B4F434F203E2043414E43796C6963203E203130363031205B44435D3B4B4F434F203E2043414E43796C6963203E203130363032205B44435D3B4B4F434F203E2043414E43796C6963203E203130363033205B44435D3B4B4F434F203E2043414E43796C6963203E203130363034205B44435D3B4B4F434F203E2043414E43796C6963203E203330313031205B44435D3B0D0A30312E30342E32302031353A30393A34393A3331343B303B303B303B303B303B303B303B303B303B303B303B303B303B303B0D0A30312E30342E32302031353A30393A35303A3336373B303B303B303B303B303B303B303B303B303B303B303B303B303B303B
My code is this
InputStream is = new ByteArrayInputStream(hexStringToByteArray(s1));
fi.kapsi.koti.jpa.nanopb.TestProtocol.MessageFinale.parseFrom(is);
System.out.println("hello");
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
But i got this error :
>
Exception in thread "Thread-1" java.lang.ExceptionInInitializerError
> at fi.kapsi.koti.jpa.nanopb.TestProtocol.<clinit>(TestProtocol.java:4650)
> at fi.kapsi.koti.jpa.nanopb.TestProtocol$MessageFinale.internalGetFieldAccessorTable(TestProtocol.java:213)
> at com.google.protobuf.GeneratedMessage.getDescriptorForType(GeneratedMessage.java:98)
> at com.google.protobuf.AbstractMessage$Builder.findMissingFields(AbstractMessage.java:789)
> at com.google.protobuf.AbstractMessage$Builder.findMissingFields(AbstractMessage.java:780)
> at com.google.protobuf.AbstractMessage$Builder.newUninitializedMessageException(AbstractMessage.java:770)
> at com.google.protobuf.AbstractMessage.newUninitializedMessageException(AbstractMessage.java:237)
> at com.google.protobuf.AbstractParser.newUninitializedMessageException(AbstractParser.java:57)
> at com.google.protobuf.AbstractParser.checkMessageInitialized(AbstractParser.java:71)
> at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:253)
> at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:259)
> at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:49)
> at fi.kapsi.koti.jpa.nanopb.TestProtocol$MessageFinale.parseDelimitedFrom(TestProtocol.java:574)
> at com.mkyong.hashing.server.RequestHandler.run(RequestHandler.java:169)
> Caused by: java.lang.IllegalArgumentException: Invalid embedded
> descriptor for "nanopb.proto".
> at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:301)
> at fi.kapsi.koti.jpa.nanopb.Nanopb.<clinit>(Nanopb.java:1765)
> ... 14 more Caused by: com.google.protobuf.Descriptors$DescriptorValidationException:
> nanopb.proto: Dependencies passed to FileDescriptor.buildFrom() don't
> match those listed in the FileDescriptorProto.
> at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:246)
> at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:299)
> ... 15 more
`
Here is my proto file :
message MessageFinale {
enum Type {
AUTH = 0;
FILE = 1;
ERRORCODE = 2;
GPS = 3;
};
required Type type = 1;
optional Auth auth = 2 [(nanopb).type = FT_CALLBACK];
optional File file = 3 [(nanopb).type = FT_CALLBACK];
optional ErrorCode errorCode = 4 [(nanopb).type = FT_CALLBACK];
optional GPS gps = 5 [(nanopb).type = FT_CALLBACK];
}
message Auth {
enum Type {
REQ = 0;
RES = 1;
}
required Type type = 1;
message Req {
required uint64 imei = 1;
required uint32 password = 2 [default = 1234];
}
optional Req req = 2 [(nanopb).type = FT_CALLBACK];
message Res {
}
optional Res res = 3 [(nanopb).type = FT_CALLBACK];
}
message File {
required bytes chunk = 1;
}
message ErrorCode {
required int32 errorId = 1;
enum Status {
ACTIVE = 1;
INACTIVE = 2;
DELETED = 3;
}
required Status status = 2;
}
message GPS {
required int32 lat = 1;
required int32 lng = 2;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论