我正在将protobuf
写入文件,然后再次阅读如下
protobuff block
Person personOne =
Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe@example.com")
.addPhone(
Person.PhoneNumber.newBuilder()
.setNumber("555-4321")
.setType(Person.PhoneType.HOME))
.build();
写入文件,此处data
为byte[]
,参数为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();
}
从文件中读取,但在阅读时我收到错误
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();
}
错误
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)
更新
以前我将数据存储到data.txt
文件中,但现在我已删除了异常并收到新错误
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)
答案 0 :(得分:1)
从你的代码:
byte [] buffer = new byte[input.read()];
在这里,您似乎正在读取输入的第一个字节,并将其用作缓冲区的大小。但是,当您编写文件时,您实际上并没有在第一个字节中写入大小;你只写了数据:
FileOutputStream output = new FileOutputStream(file);
output.write(data);
output.close();
不要将文件内容读取到byte[]
,只需将输入流本身传递给parseFrom
,如:
FileInputStream input = new FileInputStream(file);
Person person = Person.parseFrom(input);
如果必须读入字节数组,则根据文件的实际大小分配数组,例如:使用File#length()
。或者,实际上在写入消息之前写入消息大小。 (不要把它写成一个字节,因为如果你的消息大于255个字节,你就会遇到问题。)