java 协议消息包含无效标签(零)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26598419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protocol message contained an invalid tag (zero)
提问by Moe
I'm working with pbf files from the open street maps
我正在处理来自开放街道地图的 pbf 文件
I want to parse node, relations, and ways.
我想解析节点、关系和方式。
when I try to parse nodes I get that message.
当我尝试解析节点时,我收到了该消息。
The code looks like
代码看起来像
package myCode;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import protpbufCode.OsmPbf;
import protpbufCode.OsmPbf.Node;
import protpbufCode.OsmPbf.PrimitiveGroup;
public class ReadingPBF
{
public static void print(PrimitiveGroup node)
{
for (Node m: node.getNodesList())
{
System.out.print("Person ID: " + m.getId() + " ");
System.out.print(" Lat: " + m.getLat()+ " ");
System.out.print(" Long: "+ m.getLon()+ " ");
System.out.println("");
}
}
public static void main (String args[])
{
try
{
PrimitiveGroup newNode = PrimitiveGroup.parseFrom(new FileInputStream(new File("isle.pbf")));
print(newNode);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.getCause());
}
}
}
the OsmPbf is java class that created using the protobuf compiler.
OsmPbf 是使用 protobuf 编译器创建的 java 类。
and that what gets printed.
打印出来的东西。
com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
at com.google.protobuf.InvalidProtocolBufferException.invalidTag(InvalidProtocolBufferException.java:89)
at com.google.protobuf.CodedInputStream.readTag(CodedInputStream.java:158)
at protpbufCode.OsmPbf$PrimitiveGroup.<init>(OsmPbf.java:5230)
at protpbufCode.OsmPbf$PrimitiveGroup.<init>(OsmPbf.java:5219)
at protpbufCode.OsmPbf$PrimitiveGroup.parsePartialFrom(OsmPbf.java:5329)
at protpbufCode.OsmPbf$PrimitiveGroup.parsePartialFrom(OsmPbf.java:1)
at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:192)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:209)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:215)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
at protpbufCode.OsmPbf$PrimitiveGroup.parseFrom(OsmPbf.java:5627)
at myCode.ReadingPBF.main(ReadingPBF.java:33)
Protocol message contained an invalid tag (zero).
null
回答by Kenton Varda
OpenStreetMap .pbf files are not simple protobufs. See the documentation here:
OpenStreetMap .pbf 文件不是简单的 protobuf。请参阅此处的文档:
http://wiki.openstreetmap.org/wiki/PBF_Format
http://wiki.openstreetmap.org/wiki/PBF_Format
Under the section "File format", you'll see this:
在“文件格式”部分下,您将看到:
The format is a repeating sequence of:
- int4: length of the BlobHeader message in network byte order
- serialized BlobHeader message
- serialized Blob message (size is given in the header)
格式是以下重复序列:
- int4:BlobHeader 消息的网络字节序长度
- 序列化的 BlobHeader 消息
- 序列化的 Blob 消息(大小在标头中给出)
That is, the file starts out with a 4-byte integer before the first protobuf message. Since this integer is probably smaller than 2^24, the first byte will of course be zero, which explains the exact exception you are seeing.
也就是说,文件以第一个 protobuf 消息之前的 4 字节整数开始。由于此整数可能小于 2^24,因此第一个字节当然为零,这解释了您所看到的确切异常。
You will need to read this 4-byte value manually, then make sure to read only that many bytes and parse them as a BlobHeader
, and so on.
您需要手动读取这个 4 字节的值,然后确保只读取那么多字节并将它们解析为BlobHeader
,依此类推。
Personally I'd recommend looking for a PBF decoder library that already handles this for you. There must be a few out there.
我个人建议寻找一个已经为您处理这个问题的 PBF 解码器库。那里应该有几个。