I'm trying to read a stream into a byte array using FileInputStream reads(byte ) etc
This works about 50% of the time, but when it fails my test is a System.out.println testing if the data1D is not "null" in an inner class used to display, and this seems to confirm the array is empty.
thank you Jim - there's more to IO than meets the eye
posted 13 years ago
Hi again y'all
Thanks for the pointers.
Jim - using a while loop and forcing a read from the link you posted me is tricky.
I've got round the problem temporarily by testing for a null array - i.e. a fail condition, and forcing my file read method to re-run until it works, and I now get a display even if it has to loop a couple of times. but I'm not sure why the readFully() doesn't always correctly fill the array?
I'd be much happier if I could understand this?
Can anyone spot logic or code problems for me please? kelly
posted 13 years ago
[kelly]: Jim - using a while loop and forcing a read from the link you posted me is tricky.
How so? What problem(s) do you observe?
[kelly]: I've got round the problem temporarily by testing for a null array - i.e. a fail condition,
Well, that sounds very suspicious. Where does this array you're using come from? Can you show some code that demonstrates how it's created or obtained, and how it's used?
[kelly]: and forcing my file read method to re-run until it works,
You shouldn't have to do this, but I suppose as a temporary workaround it's better than nothing. Need to find the real problem though...
[kelly]: and I now get a display even if it has to loop a couple of times. but I'm not sure why the readFully() doesn't always correctly fill the array?
How do you know it doesn't? And failing to correctly fill an array sounds different than returning a null array, so this sounds like a different problem than the one you mentioned earlier.
When using readFully(), it should fill the array unless the input stream has no more bytes - i.e., unless you've reached the end of the file. Do you have any way of knowing if that's the case?
[kelly]: Can anyone spot logic or code problems for me please?
If you read the API for skip(), you'll find there's no guarantee that this will skip all the requested bytes. It's much like read() in that respect. Too bad there's no skipFully() in the DataInput AAPI. Got to do it yourself I guess:
this is suspicious since the variable "read" is never used here...
And where did data1D come from? How big is it? Do you know if it's bigger or smaller than the file size?
Usually close() should be in a finally block, and the preceding code should be in a try block. But that's unlikely to be related to the problem's you're describing.
Hope that helps... [ October 08, 2006: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad: