Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java byte array contains negative numbers

Tags:

java

file-io

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 :)

like image 981
nick Avatar asked Mar 07 '12 21:03

nick


People also ask

Can byte hold negative values?

In C#, a byte represents an unsigned 8-bit integer, and can therefore not hold a negative value (valid values range from 0 to 255 ).

Can a byte be negative Java?

In Java, byte is an 8-bit signed (positive and negative) data type, values from -128 (-2^7) to 127 (2^7-1) . For unsigned byte , the allowed values are from 0 to 255 .

Can you have negative numbers in Java?

Java Integer class provides an inbuilt function signum() to check if a number is positive or negative. It is a static method that accepts a parameter of integer type. It returns 0, if the argument is 0. It returns 1, if the argument>0.

How do you stop a negative number in Java?

To convert negative number to positive number (this is called absolute value), uses Math. abs(). This Math. abs() method is work like this “ number = (number < 0 ? -number : number); ".


1 Answers

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

like image 76
Aaron Digulla Avatar answered Sep 25 '22 08:09

Aaron Digulla