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