protobuf java проблемы при отправке / получении байтов типа - PullRequest
1 голос
/ 05 апреля 2020

Здравствуйте. Я пытаюсь десериализовать этот объект в шестнадцатеричном формате, когда я использую другой тип, с которым он работает (int, float, string ...). Только это происходит с байтами.

Мой код такой

 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;
    }
...