从 c# 到 java,如何交换 'Serializer.TryReadLengthPrefix' 的方法

from c# to java, how to swap method of 'Serializer.TryReadLengthPrefix'

我正在尝试将以下方法从 c# 渲染到 java。

一些组件很容易识别,例如(如果我错了请纠正我但是)似乎:

C#: Java

Console.WriteLine = System.out.println

有些组件更不透明。例如 using,我猜在 java 中没有等价物,不是吗?所以我想我会忽略它,这样谨慎吗?

在我们继续之前先了解一下背景知识,我正在尝试解码一个 google 协议缓冲区 .pb 文件。

Serializer.TryReadLengthPrefix(file, PrefixStyle.Base128, out len) 无疑也很棘手,但它是整个程序的关键,所以它很重要。

我相当确定我应该使用这样的东西来代替它:

    while ((r = Relation.parseDelimitedFrom(is)) != null) {
      RelationAndMentions relation = new RelationAndMentions(
          r.getRelType(), r.getSourceGuid(), r.getDestGuid());
      labelCountHisto.incrementCount(relation.posLabels.size());
      relTypes.addAll(relation.posLabels);
      relations.add(relation);

      for(int i = 0; i < r.getMentionCount(); i ++) {
        DocumentProtos.Relation.RelationMentionRef mention = r.getMention(i);
        // String s = mention.getSentence();
        relation.mentions.add(new Mention(mention.getFeatureList()));
      }

      for(String l: relation.posLabels) {
        addKnownRelation(relation.arg1, relation.arg2, l, knownRelationsPerEntity);
      }
    }

但那是一只笨重的野兽,我不确定该如何处理它。

我已经在这太久了,我的清晰思考能力已经完全消失了,但是如果你们中有一个精通 c# 和 java 的人能承受这一巨大的挑战,那我就离不开它了阻止你。

    static void ProcessFile(string path)
    {
        try
        {
            Console.WriteLine("Processing: {0}", path);
            using (var file = File.OpenRead(path))
            {
                int len, count = 0;
                while(Serializer.TryReadLengthPrefix(file, PrefixStyle.Base128, out len))
                {
                    Console.WriteLine("Fragment: {0} bytes", len);
                    using (var reader = new ProtoReader(file, null, null, len))
                    {
                        ProcessRelation(reader);
                        count++;
                    }
                }
                Console.WriteLine("{0}, {1} Relation objects parsed", path, count);
                Console.Error.WriteLine("{0}, {1} Relation objects parsed", path, count);
            }
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.Message);
        }
        finally
        {
            Console.WriteLine();
        }
    }

如果您感觉特别有野心,请挖掘谁的代码 here

你能不能。详细说明

I'm trying to decode a google protocol buffer .pb file.

通常你有 protobuf 定义文件.. Google protobuff,根据这个 "def" 文件自动生成 "code" 文件。通常没有必要解码该文件...

  1. 您使用定义生成 C# 代码文件。pb/proto 通过调用 protoc.exe
  2. 再次使用相同的定义在 Java 中生成代码文件。pb/proto 并通过相同的调用 protoc.exe(相同的命令开关将不同)

而且您可以使用两种语言进行交流。您不必 map/find 等同..希望我回答了您的问题..

如果你有更多的问题。 post 你的 .pb/proto 文件(定义不是数据)

如果您没有定义文件..请告诉我们..我们可以采取进一步的措施。