Search code examples
javac#protocol-bufferslanguage-translation

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


I'm trying to render the following method into java from c#.

some components are easily recognizable, for instance (please correct me if I'm wrong but) it seems that:

C#:                                                        Java

Console.WriteLine         =         System.out.println

Some components are more opake. Such as using, I guess that has no equivalent in java, isn't it? So I'm thinking I'll just ignore it, is that prudent?

A little background before we go on, I'm trying to decode a google protocol buffer .pb file.

Serializer.TryReadLengthPrefix(file, PrefixStyle.Base128, out len) is doubtless tricky as well, but it's the whole crux of the program, so it's important.

I'm reasonably certain that in place of that I should use something like this:

    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);
      }
    }

But that's an unwieldy beast and I'm not sure exactly what to do with it.

I've been at this too long and my capacity to think clearly is totally disipated but if one among you who is expert in c# and java feels up to this momentus undertaking, far be it from me to stop you.

    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();
        }
    }

if you're feeling particularly ambitious please do dig the who code here.


Solution

  • Can you pls. elaborate more on

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

    Usually you have protobuf definition file.. Google protobuff, auto-generates "code" file based on this "def" file. Generally there is no point in decoding that file...

    1. you generate the C# code file using definition.pb/proto by calling protoc.exe
    2. again you generate the code file in Java using same definition.pb/proto and by same calling protoc.exe ( same command switch will be different)

    and you can communicate across both language.. you dont have to map/find equivalent.. hopefully I answered your q..

    if you have further q. post your .pb/proto file (definition not data)

    if you don't have the definition file.. let us know.. we can than may be able to take further steps.