Java byte array contains negative numbers

I'm reading a file into a byte array in chunks and sending it over the network via a POST request to a webserver. It's not anything complicated, I've done it before using this exact same code. This time, I noticed that my images are looking really odd when they get to the server, so I decided to look at the byte array being sent and the one being received just to make sure it was the same. It's not. On the java sending side the byte array contains negative numbers. On the C# receiving side, there are no negative numbers.

The first 15 bytes on the receiving side (C#)

137
80
78
71
13
10
26
10
0
0
0
13
73
72
68

Those same bytes but on the sending side (java)

-119
80
78
71
13
10
26
10
0
0
0
13
73
72
68

All of the non-negative numbers are the same, and the -119 isn't the only negative number, they are all over. I did notice that -119 and 137 are 256 apart and wondered if that has something to do with it.

The code I'm using to read the image (java)

public static byte[] readPart(String fileName, long offset, int length) throws FileNotFoundException, Exception
{
    byte[] data = new byte[length];
    File file = new File(fileName);
    InputStream is = new FileInputStream(file);
    is.skip(offset);
    is.read(data,0,data.length);
    is.close();
    return data;
}

The code I'm using to write the data (c#)

    private void writeFile(string fileName, Stream contents)
    {
        using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
        {
            int bufferLen = 65000;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = contents.Read(buffer, 0, bufferLen)) > 0)
            {
                fs.Write(buffer, 0, count);
            }
            fs.Close();
        }
        contents.Close();
    }

I don't know if that is something that always happens and I just never noticed it before or if it is something that decided to go horribly wrong. What I do know is that this code worked before for something very similar and that it's not working now.

If anyone has any suggestions or an explanation I would really appreciate it.

EDIT: The reason my images were looking odd is how I was calling the readPart method.

byte[] data = FileUtilities.readPart(fileName,counter,maxFileSize);//counter is the current chunk number

How I should have been calling it

byte[] data = FileUtilities.readPart(fileName,counter*maxFileSize,maxFileSize);//the current chunk * cuhnksize for the offset...

Thanks everyone, I'm significantly less confused now :)


In Java, byte is a signed value (using two's complement to encode negative values), so what you see it correct if unexpected by most people.

To convert a byte to an unsigned int value, use b & 0xff


Java doesn't have unsigned bytes; all bytes are treated as signed. That's all.

All that really matters is how you think of the bytes, since you rarely ever actually need to do comparisons on bytes. The only significant difference is that they print out as signed, as you've discovered.

If you like, you can use e.g. Guava's UnsignedBytes utilities to view Java bytes as unsigned, but there's really not much practical difference.


As a further explanation, assume you have 137 as an unsigned byte. That is represented as:

1000 1001

This binary value, when expressed as a signed two's complement number, turns out to be -119. (-128 + 9)

Any unsigned byte values over 128 will be affected by the difference since the left-most bit is used in this way by the two's complement scheme.


Maybe it has something to do with the fact that Java's byte is signed (range -128 to 127) while C#'s is unsigned (0 to 255) :). The information is the same in binary, it's just interpreted differently.