read() returns incorrect results for file data values in the range x80-x9F
Hello folks....
can i resolve this problem or bug?
In the range from x80 to x9f the instruction .read() return incorrect values.
I read in the bug database (Bug ID: 4421413 ) that this is a natural consequence of the default encoding on windows which is cp1252.
can i resolve this problem or bug?
Thank's in advantage!!
[357 byte] By [
andreadena] at [2007-11-26 16:48:30]

What is the relationship between you and 'KrimsonEagl'? http://forum.java.sun.com/thread.jspa?threadID=5130844&messageID=9470117#9470117
nothing relationship!!
I have this code:
File inputFile = new File("temp/filename.bin");
FileReader in = new FileReader(inputFile);
int byteread = 0;
while ( byteread!= -1){
byteread = in.read();
}
System.out.println(Byteread);
and filename.bin is sequence of hex value:
81 82 8f 90 00 d0 aa 00 00 00 fb 00 fd 00 ff....... .... ... ... ... .. ..
for example, when i read 81 in hex, the correct value in Decimal should be 129 but byteread return 65533(incorrect).
how should resolve this problem?
thanks.
Since bytes are signed you need to mask them to get the unsigned value. Use
int byteReadAsUnsignedInt = byteread & 0xff;
P.S. Its a pity but in your code you can't just mask byteread to itself because then your loop would never terminate.
Message was edited by:
sabre150
Sorry for my english, maybe i don't explain very well my problem.
When i read a single byte of file out of range x80 to x9f all works fine and byteread receive the correct value converted from hex to decimal value.
When i read a single byte into range x80 to x9f the value are incorrect.
Look this BUG signal in 03-MAR-2001 with ID:4421413 for major explanation.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4421413
Please,I would to know if exist a solution for this problem and in what way!!
I try this:
int byteReadAsUnsignedInt = byteread & 0xff;
but the result is incorrect.
Thank's
This is not a bug! I have just re-read your second post and not that you are using
FileReader in = new FileReader(inputFile);
which is converting your bytes to characters using your default character encoding.
Do you want to read bytes or characters? They are not the same.
If you are trying to read bytes then use a InputStream in partticular FileInputStream
FileInputStream in = new FileInputStream(inputFile);
If you are trying to read characters then, as in
http://forum.java.sun.com/thread.jspa?threadID=5130844&messageID=9470117#9470117
you will need to specify the encoding to use. It looks to be exactly the same problem so try
BufferedReader br = new BufferedReader(new InputStreamReader(urlconnection.getInputStream(), "iso-8859-1"));
If this does not work, try the 'popular encodings' in http://en.wikipedia.org/wiki/Character_encoding .
Very Very thanks....I change thisFileReader in = new FileReader(inputFile);with FileInputStream in = new FileInputStream(inputFile);and work fine!!Now I can read all bytes of my file....thanks you!!