Re: DataInputStream Read Method -- Is Offset From Start of File or Last Read?
From: Chris Smith (cdsmith_at_twu.net)
Date: 11/27/04
- Next message: Thanasis \(sch\): "MenuBar in AWT Java Applets"
- Previous message: Andrew Thompson: "Re: DataInputStream Read Method -- Is Offset From Start of File or Last Read?"
- In reply to: Hal Vaughan: "DataInputStream Read Method -- Is Offset From Start of File or Last Read?"
- Next in thread: Oscar kind: "Re: DataInputStream Read Method -- Is Offset From Start of File or Last Read?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Nov 2004 09:00:50 -0700
Hal Vaughan <hal@thresholddigital.com> wrote:
> 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.
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.
> 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
- Next message: Thanasis \(sch\): "MenuBar in AWT Java Applets"
- Previous message: Andrew Thompson: "Re: DataInputStream Read Method -- Is Offset From Start of File or Last Read?"
- In reply to: Hal Vaughan: "DataInputStream Read Method -- Is Offset From Start of File or Last Read?"
- Next in thread: Oscar kind: "Re: DataInputStream Read Method -- Is Offset From Start of File or Last Read?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|