aspose file tools*
The moose likes Beginning Java and the fly likes Buffers and streams, is this how they work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Buffers and streams, is this how they work?" Watch "Buffers and streams, is this how they work?" New topic
Author

Buffers and streams, is this how they work?

Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
Now any given stream has a buffer, correct?

Most buffers aren't dynamic, so they can only hold a certain amount of bytes by default.

The buffer is just what holds the data *temporarily*.

So for example, I can't read the entire contents of a stream all at once, I can only read whatever is small enough to pass through the buffer.

Same with writing, I can't just write huge amounts of data in a single "write()" call, I need to use a while loop, always writing as little as one byte, or as big as the buffer's size, no more.

Same with read, I can't read the whole stream and save it somewhere, I can only read as much as a buffer's size at a given read() call.

Is that correct?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Originally posted by Tarek Khojah:
Now any given stream has a buffer, correct?

No, not every stream absolutely needs to have a buffer. It's up to who implements the stream class, and it might not be necessary to have a buffer.
So for example, I can't read the entire contents of a stream all at once, I can only read whatever is small enough to pass through the buffer.

Same with writing, I can't just write huge amounts of data in a single "write()" call, I need to use a while loop, always writing as little as one byte, or as big as the buffer's size, no more.

Same with read, I can't read the whole stream and save it somewhere, I can only read as much as a buffer's size at a given read() call.

Is that correct?

Not really, the stream hides implementation details like how large its internal buffer is, if it has any. For example, the method OutputStream.write(byte[]) accepts a byte[] of any length. There's no restriction on the length of the byte[] you call it with because of the length of the buffer that the stream uses internally.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Buffers and streams, is this how they work?