• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help! Byte from ByteArrayInputStream to ByteArrayOutputStream converted to -1!!  RSS feed

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an image saved into a ByteArrayInputStream. At a certain point in the code, the bytes in that inputstream are written to a ByteArrayOutputStream. Later, they are then read again from that outputstream. For some reason, when they're read out of the outputstream, the bytes are all wrong. They're converted into many weird numbers including many negative ones (such as -1 which stops the reading). Why is this?

(Just for checking) When I read from the ByteArrayInputStream and save it directly into a FileOutputStream, everything is perfect. The file can then be opened and it's the correct image. It's only when putting it into a ByteArrayOutputStream that I get a problem. Does anyone know what I'm doing wrong?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This almost certainly because of a case of converting between byte and int. Post your compilable, minimal code that demonstrates your unexpected observations and someone could then possibly make a more specific diagnosis of your problem.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have no idea why this is happening as both read() and write() return/use an int. And looking in the code for the classes, there's nothing about ceonversion or encoding. Any ideas?
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get an int that contains 255 when you read a certain byte from the array. Casting that int to byte gives you -1. This is perfectly normal because bytes contain numbers between -128 and +127. The bits are the same in any case.

Is the problem just that you see -1 where you expect to see 255, or is there a more practical problem?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
You get an int that contains 255 when you read a certain byte from the array. Casting that int to byte gives you -1. This is perfectly normal because bytes contain numbers between -128 and +127. The bits are the same in any case.

Is the problem just that you see -1 where you expect to see 255, or is there a more practical problem?


(actually it's the 216 that gets converted to -1)

I'm not sure what you mean, but the changing of the bytes causes two problems:

1. Reading the data from the outputstream in a loop, causes it to terminate at the wrong point (since I have the loop doing the usual read = in.read() != -1).

2. The data, which is then written to disk, not only terminates before the end of the data but writes the wrong bytes to disk.

How do I fix this? What's causing this?

I understand what you're saying about converting byte to int, but ByteArrayInputStream.read() returns an int and it's "216", and ByteArrayOutputStream.write() takes an int, the same thing! So why is there a conversion going on there? How do I stop it?
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't seen your code, but I'm willing to bet there are several bugs in it. What you are describing doesn't normally happen: byte streams can contain -1 without terminating correctly written programs, and they don't randomly change numbers.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
I haven't seen your code, but I'm willing to bet there are several bugs in it. What you are describing doesn't normally happen: byte streams can contain -1 without terminating correctly written programs, and they don't randomly change numbers.


While I'm sure that's possible, all the code is doing (I've now changed it to simply put the image in the BAIS and then transfer it to the BAOS - nothing else) is transferring from bais.read() to baos.write() and the byte, when then read from the BAOS' byte array is -1. That's IT. Nothing else. There's no other code now, and it still occurs. I am completely stumped.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan, the fact that you mention a 255 in close proximity makes me think there's an off-by-one error somewhere, and you really are looking at the 255 rather than the 216 with you see the -1.

The "...elided..." section is of course suspicious too, under these circumstances.

Note that Tony had requested a compilable example. Yours isn't for several reasons. I made some changes to get it to compile, and the results work as expected:

I suspect if you were to go through some similar changes to isolate your problem in a single compilable file, there's a good chance that in making one of those changes, you'd discover the true cause of your current problem.

Note also that in general, it's a good idea to religiously close your streams (particularly output streams) when you're done with them. For BAIS and BAOS here, it doesn't seem to matter (but it might, in some implementations) so I would certainly recommend putting in the close() calls.
[ August 16, 2006: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!