Search code examples
c#javaandroidprimitivebinary-serialization

C# and Android/Java - cross-language binary stream writers/readers? (for primitives and UTF-8 strings)


What is the easiest way to do binary serialization/deserialization of some custom data between C# and Android's Java? I'd like to find for Java something similar to C# BinaryWriter and BinaryReader - which supports writing primitives (like uint16) and UTF-8 strings.

Or maybe there is a better way?

edit: structure of the data is not know at compilation time

Sample write:

        BinaryWriter w = new BinaryWriter(File.OpenWrite(@"D:\data"));
        w.Write((UInt16)1234);
        w.Write("To jest żółwiątko");
        w.Write((UInt16)4567);

Solution

  • In Java all primitive types are signed (oddly even byte!). So you will need to write out signed integers if you want to read them in Java using DataInputStream.readInt(). Also note that readInt() uses big-endian. You can use something like the EndianBinaryReader from Jon Skeets MiscUtils to write these so the can be read on Android.

    UTF-8 is a little trickier as DataInputStream uses something called MUTF-8 (Modified UTF-8) Encoding for strings. In code that we use to share data between android and .net we use a simple run-length encoded UTF-8 bytes to represent a String (-1 is null). Our reader method in Java looks something like this to read standard UTF-8 encoded strings from the C# BinaryWriter (after first writing out Int16 length):

    public String readUTF8String() throws ImageFileFormatException, IOException
     {
         short len = readInt16();
         if (len == -1)
             return null;
         if (len == 0)
             return "";
         if (len < -1)
             throw new ImageFileFormatException("Invalid UTF8 string");
         byte[] utf8Bytes = readBytes(len);
         return new String(utf8Bytes, "UTF-8");
     }