Hunt
Hunt

Reputation: 8435

not able to read back protobuff from file

i am writing a protobuf into file and then reading again as follows

protobuff block

Person personOne =
                  Person.newBuilder()
                    .setId(1234)
                    .setName("John Doe")
                    .setEmail("[email protected]")
                    .addPhone(
                      Person.PhoneNumber.newBuilder()
                        .setNumber("555-4321")
                        .setType(Person.PhoneType.HOME))
                    .build();

writing to file , here data is byte[] and parameter is personOne.toByteArray()

        try {
             FileOutputStream output = new FileOutputStream(file);
             output.write(data);
             output.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

reading from file , but while reading i am getting error

          try {

             FileInputStream input = new FileInputStream(file);
             byte [] buffer = new byte[input.read()];

             input.read(buffer);
             input.close();
             return buffer;

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

error

    03-20 21:04:39.060: W/System.err(18074): com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
    03-20 21:04:39.090: W/System.err(18074):    at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
    03-20 21:04:39.110: W/System.err(18074):    at com.google.protobuf.GeneratedMessage.parseUnknownField(GeneratedMessage.java:193)
    03-20 21:04:39.110: W/System.err(18074):    at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:124)
    03-20 21:04:39.110: W/System.err(18074):    at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:107)
    03-20 21:04:39.110: W/System.err(18074):    at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:186)
    03-20 21:04:39.110: W/System.err(18074):    at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:1)
    03-20 21:04:39.110: W/System.err(18074):    at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:141)
    03-20 21:04:39.110: W/System.err(18074):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:176)
    03-20 21:04:39.110: W/System.err(18074):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:188)
    03-20 21:04:39.110: W/System.err(18074):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:193)
    03-20 21:04:39.110: W/System.err(18074):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
    03-20 21:04:39.120: W/System.err(18074):    at com.example.protodemo.AddressBookProtos$Person.parseFrom(AddressBookProtos.java:1088)
    03-20 21:04:39.120: W/System.err(18074):    at com.example.protodemo.MainActivity.onCreate(MainActivity.java:63)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.Activity.performCreate(Activity.java:4470)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
    03-20 21:04:39.120: W/System.err(18074):    at android.os.Handler.dispatchMessage(Handler.java:99)
    03-20 21:04:39.120: W/System.err(18074):    at android.os.Looper.loop(Looper.java:137)
    03-20 21:04:39.120: W/System.err(18074):    at android.app.ActivityThread.main(ActivityThread.java:4517)
    03-20 21:04:39.120: W/System.err(18074):    at java.lang.reflect.Method.invokeNative(Native Method)
    03-20 21:04:39.120: W/System.err(18074):    at java.lang.reflect.Method.invoke(Method.java:511)
    03-20 21:04:39.130: W/System.err(18074):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    03-20 21:04:39.130: W/System.err(18074):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    03-20 21:04:39.130: W/System.err(18074):    at dalvik.system.NativeStart.main(Native Method)

Update previously i was storing data into data.txt file but now i have removed the exception and getting new error

03-20 21:25:58.920: W/System.err(18470): com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
03-20 21:25:58.920: W/System.err(18470):    at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
03-20 21:25:58.920: W/System.err(18470):    at com.google.protobuf.GeneratedMessage.parseUnknownField(GeneratedMessage.java:193)
03-20 21:25:58.920: W/System.err(18470):    at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:124)
03-20 21:25:58.920: W/System.err(18470):    at com.example.protodemo.AddressBookProtos$Person.<init>(AddressBookProtos.java:107)
03-20 21:25:58.930: W/System.err(18470):    at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:186)
03-20 21:25:58.930: W/System.err(18470):    at com.example.protodemo.AddressBookProtos$Person$1.parsePartialFrom(AddressBookProtos.java:1)
03-20 21:25:58.930: W/System.err(18470):    at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:141)
03-20 21:25:58.930: W/System.err(18470):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:176)
03-20 21:25:58.930: W/System.err(18470):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:188)
03-20 21:25:58.930: W/System.err(18470):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:193)
03-20 21:25:58.930: W/System.err(18470):    at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
03-20 21:25:58.930: W/System.err(18470):    at com.example.protodemo.AddressBookProtos$Person.parseFrom(AddressBookProtos.java:1088)
03-20 21:25:58.930: W/System.err(18470):    at com.example.protodemo.MainActivity.onCreate(MainActivity.java:63)
03-20 21:25:58.930: W/System.err(18470):    at android.app.Activity.performCreate(Activity.java:4470)
03-20 21:25:58.930: W/System.err(18470):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
03-20 21:25:58.930: W/System.err(18470):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
03-20 21:25:58.930: W/System.err(18470):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-20 21:25:58.940: W/System.err(18470):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-20 21:25:58.940: W/System.err(18470):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-20 21:25:58.940: W/System.err(18470):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 21:25:58.940: W/System.err(18470):    at android.os.Looper.loop(Looper.java:137)
03-20 21:25:58.940: W/System.err(18470):    at android.app.ActivityThread.main(ActivityThread.java:4517)
03-20 21:25:58.940: W/System.err(18470):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 21:25:58.940: W/System.err(18470):    at java.lang.reflect.Method.invoke(Method.java:511)
03-20 21:25:58.940: W/System.err(18470):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-20 21:25:58.940: W/System.err(18470):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-20 21:25:58.940: W/System.err(18470):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 243

Answers (1)

Kenton Varda
Kenton Varda

Reputation: 45246

From your code:

byte [] buffer = new byte[input.read()];

Here you appear to be reading the first byte of the input and using it as the size of the buffer. However, when you wrote the file, you didn't actually write a size in the first byte; you only wrote the data:

FileOutputStream output = new FileOutputStream(file);
output.write(data);
output.close();

Instead of reading the file content to a byte[], just pass the input stream itself to parseFrom, like:

FileInputStream input = new FileInputStream(file);
Person person = Person.parseFrom(input);

If you must read into a byte array, then allocate the array according to the actual size of the file, e.g. using File#length(). Or, actually write the message size before writing the message. (Don't write it as just a single byte, though, because then you'll have problems if your message is larger than 255 bytes.)

Upvotes: 1

Related Questions