Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Buffered Streams transferrable?

Thread view: 
SD WinGirl - 13 May 2004 01:58 GMT
I was wondering if the buffering of a stream is transferable.

Example:

//1.The file reader is buffered
BufferedReader fileReader = new BufferedReader(new
FileInputStream("foo.txt"));

//2.Now I wrap this buffered file reader in a InputStreamReader
InputStreamReader newFileReader = new InputStreamReader(fileReader);

In step two, is the InputStreamReader inherently buffered, because I
had originally buffered the FileInputStream?

Or do I need to buffer it explicitly in this order:
BufferedReader fileReader = BufferedReader(new InputStreamReader(new
FileInputStream("foo.txt")));

Thanks for you help.
Chris Smith - 13 May 2004 18:36 GMT
> I was wondering if the buffering of a stream is transferable.

That's a strange way to put it, but yes.

> //1.The file reader is buffered
> BufferedReader fileReader = new BufferedReader(new
[quoted text clipped - 5 lines]
> In step two, is the InputStreamReader inherently buffered, because I
> had originally buffered the FileInputStream?

Right.  The reading from the file will be buffered.  Incidentally,
InputStreamReader will do a bit of redundant buffering to help it decode
the character stream, but that probably doesn't matter to you, and it's
unlikely to have a significant effect on performance.

> Or do I need to buffer it explicitly in this order:
> BufferedReader fileReader = BufferedReader(new InputStreamReader(new
> FileInputStream("foo.txt")));

That also does buffering, but of a slightly different kind.  The
previous version buffered bytes from the file, before decoding them into
characters.  This version decodes into characters, and then buffers
those decoded characters (again, there's some redundant byte-buffering
done by InputStreamReader as well).

You probably don't have a good reason to do the latter kind of
buffering.  The major reason for buffer is to avoid the costs of two
things: crossing the user/kernel boundary too often, and asking bulk-
input devices like disks to do many small I/O operations.  To best gain
that benefit, you'd want to buffer close to the FileInputStream (and
hence choose the first option).

However, the BufferedReader class also provides a convenience method,
called readLine, which returns data only up to the next EOL sequence.  
That's a feature specifically of the BufferedReader class, so if you
intend to use it, you'd need have a reference to a BufferedReader, and
hence do the latter type of buffering.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

perry - 17 May 2004 14:24 GMT
Java makes great use of the GoF "Decorator" pattern where possible, the
streaming mechanism is a great example of that... you were correct the
first time. Buffering is being provided the BufferedReader object and
does not need to be replicated in the InputStreamReader object

- perry

> I was wondering if the buffering of a stream is transferable.
>
[quoted text clipped - 15 lines]
>
> Thanks for you help.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.