issue with reading byte array
807591Jun 10 2008 — edited Jun 11 2008I have a binary file to be read.I want to read the whole file into a byte array. But while doing so the values which are greater than 128 are read as negative values as we have Signedbyte in java(-127 to 128).
So I tried reading the bytes into the int array as shown in the below code.
int i = 0;
int content[]= new int[(int)size];
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
while(i<size)
{
content= in.readUnsignedByte();
i++;
}
The above function 'readUnsignedByte();' reads byte and stores it into an int array.....But since the file contains around 34000 bytes ....the loop runs 340000 times .....which is a performance hazard.
After googling for hours ....I could still not find any alternative ....
I want to read byte and want to store in int array but instead of loop, I want to read the file at once.
Or is there any way to implement Unsigned Byte in java so that I can read the entire file in byte array without corrupting the values which are greater than 128..
I have to deliver the code asap .....Please help me out with an alternative solution for this...
Thanks in advance....