why is DataInputStream.readline adding strange bytes at the beginning of string

Sending XML from C# Server and receiving it it in Android Java client

This is what the received XML look likes:

<?xml version="1.0" encoding="utf-8"?>.....

This is the c# send code

// convert the class WorkItem to xml
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(WorkItem));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, p);

// send the xml version of WorkItem to client
byte[] data = memoryStream.ToArray();
clientStream.Write(data, 0, data.Length);
Console.WriteLine(" send.." + data);
clientStream.Close();

In Java i just do:

in = new DataInputStream(skt.getInputStream());
String XMlString = in.readLine();

Everything is working if i every time remove the 3 first characters from XMlString. I would really like to do this in a better way if it's possible

*UPDATE adding the Android java client

@Override
protected String doInBackground(Long... params) {

    textTopInfo.setText("Loading workitems..");

    DataOutputStream out = null;
    DataInputStream in = null;

    try {

        Socket skt = new Socket(Consts.SERVER_URL_1, Consts.SERVER_PORT_1);
        skt.setSoTimeout(10000); //10 sec timout
        out = new DataOutputStream(skt.getOutputStream());
        in = new DataInputStream(skt.getInputStream());

        // check valid user id
        String id = prefs.getString("id", "");

        if(id.equals(""))
            return "Open menu and enter User Id";

        String theString =  Consts.PUSH_GET_WORKITEM + ":" + id ;

        out.write(theString.getBytes());

        BufferedReader d = new BufferedReader
        (new InputStreamReader(skt.getInputStream()));
        String XMlString = d.readLine();

            // here I remove the BOM
        XMlString = XMlString.substring(3);

        Log.d(TAG, "GF");   

        XStream xstream = new XStream();
        xstream.alias("WorkItem", WorkItem.class);
        xstream.alias("OneItem", OneItem.class);
        pl = (WorkItem)xstream.fromXML(XMlString);

    } catch (Exception e) {

        return "cannot connect to server " + e.toString();

    }finally{
        //kill out/in
        try {
            if(out != null)
                out.close();
            if(in!=null)
                in.close(); 
        } catch (IOException e) {

        }
    }
    return "here is the list";
}

Answers


the method readLine is deprecated in Java 1.7; from the javadocs:

readLine() Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form: DataInputStream d = new DataInputStream(in);

with: BufferedReader d = new BufferedReader(new InputStreamReader(in));


The three initial bytes is the UTF8 BOM (Byte ordering mark). You will need to tell your Java code to use the same encoding.


Need Your Help

Implement Camera on Android Emulator | Need some knowledge

android android-emulator emulation qemu

I'm interested in doing some enhancements to android emulator (implement webcam on emulator). Therefore I'm following the android source and emulators source to get basic understanding &amp; the

using AndEngine in Android Studio 0.2.10

android andengine android-studio

I'm trying to develop application's Android platform, so far I have programmed in PHP / MySQL, VB.Net, C #, and I have extensive knowledge and experience in this area, I decided to go in developing