java - 无效 Protocol Buffer 异常 : Protocol message end-group tag did not match expected tag

标签 java protocol-buffers

我正在尝试写入一个文件,然后再次读取它。

我写的代码:

ObjectOutputStream oos = null;
FileOutputStream fout = null;
try
{
    Object myObject;
    fout = new FileOutputStream(new File("C:\\Foo","Bar.log"));

    oos = new ObjectOutputStream(fout);
    oos.writeObject(myObject);

} catch (Exception ex) {
    ex.printStackTrace();
} finally {
    try {
        oos.close();
        fout.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我的读者:

FileInputStream input;
try {
    input = new FileInputStream(new File("C:\\Foo\\Bar.log"));
    MyFile parsedObject = MyFileFormat.MyFile.parseFrom(input);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

但是我在解析器上遇到了这个异常:

com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
    at com.google.protobuf.InvalidProtocolBufferException.invalidEndTag(InvalidProtocolBufferException.java:94)

有人可以帮忙吗?

最佳答案

您使用java的ObjectOutputStream来序列化您的对象,但使用一些涉及 Guava 的自定义反序列化? 如果您使用 ObjectOutputStream 来编写内容,则应该使用 ObjectInputStream阅读它。另外,你的例子没有任何意义。您永远不会在序列化片段中初始化 myObject 变量,此代码将根本无法编译。

关于java - 无效 Protocol Buffer 异常 : Protocol message end-group tag did not match expected tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671458/

相关文章:

java - Visual Studio 2013 企业版 : Oracle Java SDK A more recent version of this software is required

java - 反射机制是否破坏了java中的安全性?请解释

java - 无论如何,要将构造函数中的泛型类型传递给父类构造函数?

Hadoop 基础知识 : What do I do with the output?

c# - 如何使用 protobuf-net 读取 fixed32 整数?

c# - 如何从 json 序列化 protobuf,并自动忽略 C# 中的未知字段?

scala - 如何在 akka http 中序列化/反序列化 protobuf?

java - 如何使用 Apache POI 检查单元格是否包含图像?

java - 在 JSP 中使用 Ajax 响应

c - Protobuf-C 编译问题