Discussion:
DataInputStream Read Method -- Is Offset From Start of File or Last Read?
(too old to reply)
Hal Vaughan
2004-11-27 15:20:04 UTC
Permalink
If I have a DataInputStream (called dis) wrapped around a FileInputStream,
when I do this:

dis.read(byteInput, 0, intLength);

if intLength is set to the entire length of the file, it reads the full
file. Now here's the question: If, instead of reading the file all at
once, I have a loop that reads more whenever more is written, I've noticed
that when I keep the offset at 0, each read picks up where the last one
left off. Is that correct behavior, or am I doing something wrong? I
didn't see mention of this in my books or in the Sun tutorials. I'm not
clear if the offset is bytes from the start of the file, or bytes from the
end of the last read (with the same open DataInputStream).

Could someone clarify whether the offset is always supposed to be from the
beginning of the file, or from the end of the last read operation?

Thanks!

Hal
Andrew Thompson
2004-11-27 15:56:32 UTC
Permalink
Post by Hal Vaughan
I
didn't see mention of this in my books or in the Sun tutorials.
Try the JavaDocs and see if that clears it up for you.
<http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInputStream.html#read(byte[],%20int,%20int)>

The JavaDocs* are an indispensable tool for Java coding,
bokmark the light entry point to the JavaDocs here..
<http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.html>
or follow the 'frames' link for the full index.

[ * Though most people download them for easy reference. ]
--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Hal Vaughan
2004-11-28 00:23:31 UTC
Permalink
Post by Andrew Thompson
Post by Hal Vaughan
I
didn't see mention of this in my books or in the Sun tutorials.
Try the JavaDocs and see if that clears it up for you.
<http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInputStream.html#read(byte[]
%20int,%20int)>

Actually, I had read that, but misunderstood.  I was thinking "off" referred
to a file offset, and kept looking for that.  Once I read your response
(and the others), and re-read it made sense.  I think since I expected it
to say one thing, I completely missed what it was actually saying.

Thanks!

Hal
 
Post by Andrew Thompson
The JavaDocs* are an indispensable tool for Java coding,
bokmark the light entry point to the JavaDocs here..
<http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.html>
or follow the 'frames' link for the full index.
[ * Though most people download them for easy reference. ]
Chris Smith
2004-11-27 16:00:50 UTC
Permalink
Post by Hal Vaughan
If I have a DataInputStream (called dis) wrapped around a FileInputStream,
dis.read(byteInput, 0, intLength);
if intLength is set to the entire length of the file, it reads the full
file.
No, it doesn't. You would be well advised to take a look at the
readFully method in DataInputStream instead.

The read method, as specified by the base class InputStream, will read
at least one byte and as many as are immediately available before
returning; but after reading one byte, it will not block for more. The
return value is the number of bytes read, or -1 for EOF. The read
method should generally always be used in a loop. For example, to
properly read an entire array's worth of data from read, you might do
this:

int count = 0;
int len = 0;
while ((len != -1) && (count < byteInput.length))
{
len = dis.read(byteInput, count, byteInput.length - count);
if (len != -1) count += len;
}

That's all beside the point, though.
Post by Hal Vaughan
Now here's the question: If, instead of reading the file all at
once, I have a loop that reads more whenever more is written, I've noticed
that when I keep the offset at 0, each read picks up where the last one
left off. Is that correct behavior, or am I doing something wrong?
The offset is an offset into the array; the index of the first byte that
you want to fill. So if you want the data that's returned to be at the
beginning of the array, then offset should indeed be zero. So in the
end, both of the possibilities you were considering are wrong.

Reads always happen from the current position in the input stream, and
nothing in the read(...) methods can change that.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Oscar kind
2004-11-27 20:06:48 UTC
Permalink
Post by Hal Vaughan
If I have a DataInputStream (called dis) wrapped around a FileInputStream,
dis.read(byteInput, 0, intLength);
if intLength is set to the entire length of the file, it reads the full
file. Now here's the question: If, instead of reading the file all at
once, I have a loop that reads more whenever more is written, I've noticed
that when I keep the offset at 0, each read picks up where the last one
left off. Is that correct behavior, or am I doing something wrong? I
didn't see mention of this in my books or in the Sun tutorials. I'm not
clear if the offset is bytes from the start of the file, or bytes from the
end of the last read (with the same open DataInputStream).
Could someone clarify whether the offset is always supposed to be from the
beginning of the file, or from the end of the last read operation?
Neither. The offset is for the array: if the offset is 10, the first 10
bytes in the array are left untouched.
--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
Loading...