0

I need to convert this short script posted below from c# to java, however I'm very unfamiliar with c#. I tried some of the automatic tools suggested in this question but some of them are proprietary and others of them generated nothing useful.

Perhaps someone with more advanced understanding of c# could just quickly help me understand the equivalencies of important components, i.e. it's pretty clear that using from c# equates to import in java, but what else? I've been looking for a kind of table explaining this but to no avail. What is namespace for example?

I don't want to post my failed attempts because they're too miserable for serious contemplation.

using ProtoBuf;
using System;
using System.IO;

namespace SO29531899
{
    class Program
    {
        static void Main()
        {
            ProcessFile("test-multiple.pb");
            ProcessFile("testNegative.pb");
            ProcessFile("testPositive.pb");
            ProcessFile("trainNegative.pb");
            ProcessFile("trainPositive.pb");
        }
        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();
            }
        }
        private static void ProcessRelation(ProtoReader reader)
        {
            Console.WriteLine("> Relation");
            while (reader.ReadFieldHeader() > 0)
            {
                Console.Write("{0}: {1}", reader.FieldNumber, reader.WireType);
                switch (reader.FieldNumber)
                {
                    case 1:
                        Console.WriteLine(", sourceGuid, {0}", reader.ReadString());
                        break;
                    case 2:
                        Console.WriteLine(", destGuid, {0}", reader.ReadString());
                        break;
                    case 3:
                        Console.WriteLine(", relType, {0}", reader.ReadString());
                        break;
                    case 4:
                        Console.WriteLine(", mention");
                        var tok = ProtoReader.StartSubItem(reader);
                        ProcessRelationMentionRef(reader);
                        ProtoReader.EndSubItem(tok, reader);
                        break;
                    default:
                        Console.WriteLine(", Unexpected field");
                        reader.SkipField();
                        break;
                }
            }
            Console.WriteLine("< Relation");
        }

        private static void ProcessRelationMentionRef(ProtoReader reader)
        {
            Console.WriteLine("> RelationMentionRef");
            while (reader.ReadFieldHeader() > 0)
            {
                Console.Write("{0}: {1}", reader.FieldNumber, reader.WireType);
                switch (reader.FieldNumber)
                {
                    case 1:
                        Console.WriteLine(", filename, {0}", reader.ReadString());
                        break;
                    case 2:
                        Console.WriteLine(", sourceId, {0}", reader.ReadInt32());
                        break;
                    case 3:
                        Console.WriteLine(", destId, {0}", reader.ReadInt32());
                        break;
                    case 4:
                        Console.WriteLine(", feature, {0}", reader.ReadString());
                        break;
                    case 5:
                        Console.WriteLine(", sentence, {0}", reader.ReadString());
                        break;
                    default:
                        Console.WriteLine(", Unexpected field");
                        reader.SkipField();
                        break;
                }
            }
            Console.WriteLine("< RelationMentionRef");
        }
    }
}
Community
  • 1
  • 1
smatthewenglish
  • 2,831
  • 4
  • 36
  • 72
  • "it's pretty clear that using from c# equates to import in java" No, that is the **using directive**. `using (var file = File.OpenRead(path))` is a **using statement**, which is something completely different. It is similar to try-with-resources in Java. – Dennis_E Apr 09 '15 at 11:25
  • 1
    It is better to specify what exactly unclear to you in this code and ask specific question. Otherwise, some "comparision table" of c# classes and methods used here and their java equivalents will be at least some pages of text. I really doubt someone will bother to write such a table. – Andrey Korneyev Apr 09 '15 at 11:26
  • @Dennis_E I think, this part of question was about `using System;`, not `using (var file = File.OpenRead(path))` ;) – Andrey Korneyev Apr 09 '15 at 11:27
  • @AndyKorneyev Yes, I realized that too late. I edited the previous comment somewhat. – Dennis_E Apr 09 '15 at 11:28
  • what specific issues are you having? Is there something that jumps out as not being easily understood? The code posted looks pretty straightforward. You have functions, function calls, while loops, a switch statement, writing to console, try/catch/finally blocks - i think most of which are written in java in a very similar manner – Kritner Apr 09 '15 at 11:29
  • "I'm very unfamiliar with c#". So *become* familiar with C#. You have the whole Internet at your fingertips. Go through some tutorials, learn the basics, and then do this task yourself. Otherwise you're just expecting people on SO to teach you C# *and* convert this code to Java. – Taylan Aydinli Apr 09 '15 at 11:31
  • Concerned to your specific question: "*What is namespace for example?*" - well, it is somehow equivalent to "package" in java. – Andrey Korneyev Apr 09 '15 at 11:35
  • ahhh, good call. must be. – smatthewenglish Apr 09 '15 at 11:35

0 Answers0