Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

intermittent reading data into a byte[ ]  RSS feed

 
kelly bones
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

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.

Any advice gratefully recd

love

kelly
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API documentation for InputStream, you'll see the following entry:

public int read(byte[] b) throws IOException

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer.

Note that it does not say that it will fill the given array. If you must fill an array in one call, have a look at the readFully() method of java.io.DataInputStream
 
kelly bones
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks Joe - off to read the API - again!

kelly
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also: read doesn't do what you think it does.
 
kelly bones
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Jim - there's more to IO than meets the eye

kelly
 
kelly bones
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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?

Let's see:

    din.skip(count);

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:

Moving on:

    int read;

this is suspicious since the variable "read" is never used here...

    din.readFully(data1D);

And where did data1D come from? How big is it? Do you know if it's bigger or smaller than the file size?

    din.close();

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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!