Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Questions about buffered streams

Reply
Thread Tools

Questions about buffered streams

 
 
failure_to@yahoo.co.uk
Guest
Posts: n/a
 
      01-07-2008
hello

Sorry for so many questions, but I think I/O is one topic that will
give me troubles for quite some time

1)
What makes read and write operations time consuming ( or at least more
time consuming than calls to ordinary, non IO methods? )?

a) The fact that underlying stream classes make calls to system
libraries?

b)
* Buffer classes are supposed to help us with that, since they buffer
data and thus don¡¦t necessarelly call underlying system for each byte.
But non the less, even if buffered stream doesn¡¦t immediately call
underlying system in order to write a byte, it still has to call
underlying system ( say it buffers 5 bytes of data and at some point
we call flush() ) once for each byte „³ just as non buffered stream
classes have to¡K so in the end, same amount of time was spent to write
those five bytes to a file as if we¡¦d write those 5 bytes with non
buffered stream ¡K only difference being that those 5 bytes were
written at once and not every time write() was called?!

* Or, can buffered streams somehow call underlying system¡¦s method
just once and with just that one call write all five bytes of data?



c)
Even if buffered byte streams can somehow write all 5 bytes with one
system call, that shouldn¡¦t be true for BufferedWriter streams?!
BufferedWriter stream doesn¡¦t directly talk to underlying byte
stream, so I assume it would still take 5 system calls to write those
five bytes of data? So no time was saved!




2)
FileInputStream FS = new FileInputStream ( ¡§A.txt¡¨ );
BufferedInputStream BS = new BufferedInputStream ( FS );
DataInputStream DS = new DataInputStream ( BS );

Of the three objects above, I assume only the byte stream objects keep
some sort of internal pointer which keeps track of which bytes in a
stream were already written/read and thus advances this pointer with
each read or write operation? Wrapper objects ( BS and DS in the above
example ) don¡¦t have such internal pointers?!

3)
If you use say Fileoutputstream method write( int buf[]¡K), does it act
like kind of buffer and reads all those bytes with one system call, or
does it make one system call for each byte read?


4)
BufferedReader in = new
BufferedReader( new FileReader("foo.in") );

Does even simple in.read() without any parameters specified causes
wrapped FileReader object to read more than just one character from
underlying byte stream?


5)
Next questions are about PrintStream class. Here is what Java docs and
my book have to say about this class:

¡§All characters printed by a PrintStream are converted into bytes
using the platform's default character encoding. ¡§

I assume the text is referring to cases where we don¡¦t specify type of
encoding in a constructor, since if we do specify which encoding to
use, then PrintStream converts characters into bytes using specified
encoding and not platform¡¦s default character encoding?!



¡§For real-world programs, the recommended method of
writing to the console when using Java is through a PrintWriter
stream. PrintWriter is one of the character-based classes. Using a
character-based class for console output makes it easier to
internationalize your program.¡¨

¡§The PrintWriter class should be used in situations that require
writing characters rather than bytes.¡¨


* Why should PrintWriter be used instead in situations that require
writing characters instead of bytes?

* How does PrintWriter make it easier to internationalize a program?

* When dealing with characters, when and why ( or why not ) would you
choose PrintWriter over some other character based stream ( like
OutputStreamWriter )?


thank you

cheers
 
Reply With Quote
 
 
 
 
Silvio Bierman
Guest
Posts: n/a
 
      01-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hello
>
> Sorry for so many questions, but I think I/O is one topic that will
> give me troubles for quite some time
>
> 1)
> What makes read and write operations time consuming ( or at least more
> time consuming than calls to ordinary, non IO methods? )?
>
> a) The fact that underlying stream classes make calls to system
> libraries?
>
> b)
> * Buffer classes are supposed to help us with that, since they buffer
> data and thus don¡¦t necessarelly call underlying system for each byte.
> But non the less, even if buffered stream doesn¡¦t immediately call
> underlying system in order to write a byte, it still has to call
> underlying system ( say it buffers 5 bytes of data and at some point
> we call flush() ) once for each byte ? just as non buffered stream
> classes have to¡K so in the end, same amount of time was spent to write
> those five bytes to a file as if we¡¦d write those 5 bytes with non
> buffered stream ¡K only difference being that those 5 bytes were
> written at once and not every time write() was called?!
>
> * Or, can buffered streams somehow call underlying system¡¦s method
> just once and with just that one call write all five bytes of data?
>
>
>
> c)
> Even if buffered byte streams can somehow write all 5 bytes with one
> system call, that shouldn¡¦t be true for BufferedWriter streams?!
> BufferedWriter stream doesn¡¦t directly talk to underlying byte
> stream, so I assume it would still take 5 system calls to write those
> five bytes of data? So no time was saved!
>
>
>
>
> 2)
> FileInputStream FS = new FileInputStream ( ¡§A.txt¡¨ );
> BufferedInputStream BS = new BufferedInputStream ( FS );
> DataInputStream DS = new DataInputStream ( BS );
>
> Of the three objects above, I assume only the byte stream objects keep
> some sort of internal pointer which keeps track of which bytes in a
> stream were already written/read and thus advances this pointer with
> each read or write operation? Wrapper objects ( BS and DS in the above
> example ) don¡¦t have such internal pointers?!
>
> 3)
> If you use say Fileoutputstream method write( int buf[]¡K), does it act
> like kind of buffer and reads all those bytes with one system call, or
> does it make one system call for each byte read?
>
>
> 4)
> BufferedReader in = new
> BufferedReader( new FileReader("foo.in") );
>
> Does even simple in.read() without any parameters specified causes
> wrapped FileReader object to read more than just one character from
> underlying byte stream?
>
>
> 5)
> Next questions are about PrintStream class. Here is what Java docs and
> my book have to say about this class:
>
> ¡§All characters printed by a PrintStream are converted into bytes
> using the platform's default character encoding. ¡§
>
> I assume the text is referring to cases where we don¡¦t specify type of
> encoding in a constructor, since if we do specify which encoding to
> use, then PrintStream converts characters into bytes using specified
> encoding and not platform¡¦s default character encoding?!
>
>
>
> ¡§For real-world programs, the recommended method of
> writing to the console when using Java is through a PrintWriter
> stream. PrintWriter is one of the character-based classes. Using a
> character-based class for console output makes it easier to
> internationalize your program.¡¨
>
> ¡§The PrintWriter class should be used in situations that require
> writing characters rather than bytes.¡¨
>
>
> * Why should PrintWriter be used instead in situations that require
> writing characters instead of bytes?
>
> * How does PrintWriter make it easier to internationalize a program?
>
> * When dealing with characters, when and why ( or why not ) would you
> choose PrintWriter over some other character based stream ( like
> OutputStreamWriter )?
>
>
> thank you
>
> cheers


You asked too many questions to be answered individually, especially
because they stack on top of each other. I will try to give a brief
explanation and suggest you do some reading/googling.

The system calls you refer to primarily come down to the same two system
calls that can read/write n bytes from/to what is often called a file
descriptor. In C these system calls would be

int read(int fd,char *buff,int nbytes);
int write(int fd,char *buff,int nbytes);

For any Java implementation the basic systems routines might look
entirely different but this should be sufficiently accurate.

This answers your question about buffered streams, they read/write more
optimal sized blocks of bytes in one system call than the individual
read/write calls performed on the stream.

Readers/writers add the abstraction of characters and encodings of
characters into bytes. At the end they need a stream to read/write bytes.

Basically this is all you need but if you want to be able to write
strings, integers etc. to some character oriented output then you will
need some formatting logic and that is what a PrintWriter will do for you.

A DataInputStream or a DataOutputStream is something that adds binary IO
of Strings, integers etc. to a byte oriented stream.

This all fits together nicely. In combination of the java.text.XXXFormat
classes you have a rather complete set of basic IO tools in the Java SDK.

I would suggest a good Java textbook or the Sun website to learn more.

Good luck,

Silvio Bierman
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>What makes read and write operations time consuming ( or at least more
>time consuming than calls to ordinary, non IO methods? )?


they require mechanical motion of disk head, and waiting for disk
surfaces to spin under the read head, and for the data to mechanically
pass by the read head.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>b)
>* Buffer classes are supposed to help us with that, since they buffer
>data and thus don??t necessarelly call underlying system for each byte.
>But non the less, even if buffered stream doesn??t immediately call
>underlying system in order to write a byte, it still has to call
>underlying system ( say it buffers 5 bytes of data and at some point
>we call flush() ) once for each byte ?? just as non buffered stream
>classes have to?K so in the end, same amount of time was spent to write
>those five bytes to a file as if we??d write those 5 bytes with non
>buffered stream ?K only difference being that those 5 bytes were
>written at once and not every time write() was called?!


If you wrote 1 byte at a time, you would have to wait for the spot on
disk to spin round for each byte. If you write 64,000 bytes at a time,
you only have to wait once for the proper spot on disk to spin round.

see http://mindprod.com/jgloss/buffer.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>Even if buffered byte streams can somehow write all 5 bytes with one
>system call, that shouldn??t be true for BufferedWriter streams?!
>BufferedWriter stream doesn??t directly talk to underlying byte
>stream, so I assume it would still take 5 system calls to write those
>five bytes of data? So no time was saved!


when you write to a buffer, the buffer class only writes to the system
when the buffer is full, or when you flush or close.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>FileInputStream FS = new FileInputStream ( ??A.txt?? );
>BufferedInputStream BS = new BufferedInputStream ( FS );
>DataInputStream DS = new DataInputStream ( BS );
>
>Of the three objects above, I assume only the byte stream objects keep
>some sort of internal pointer which keeps track of which bytes in a
>stream were already written/read and thus advances this pointer with
>each read or write operation? Wrapper objects ( BS and DS in the above
>example ) don??t have such internal pointers?!


All streams keep track internally of how many bytes have been read
both on disk and from the buffer. RandomAccessFiles also keep track
of where you are in the file, but explicitly with getFilePointer and
seek.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>3)
>If you use say Fileoutputstream method write( int buf[]?K), does it act
>like kind of buffer and reads all those bytes with one system call, or
>does it make one system call for each byte read?


You can example the source for yourself in src.zip. It will write all
the bytes in one go. I do a lot of I/O megabytes at a pop and it is
very fast, certainly not a byte at a time.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>4)
> BufferedReader in = new
> BufferedReader( new FileReader("foo.in") );
>
>Does even simple in.read() without any parameters specified causes
>wrapped FileReader object to read more than just one character from
>underlying byte stream?


Buffered readers will either:

1. satisfy the request from the buffer.
2. read a buffer full, then satisfy the request.
3. read to the tail end of the file if it can't get a whole buffer
full, then satisfy the request.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>??All characters printed by a PrintStream are converted into bytes
>using the platform's default character encoding. ??


Inside the program you are using 16-bit Unicode. Your platform
typically supports 8-bit text files. What encoding depends where you
live. See http://mindprod.com/jgloss/encoding.html

PrintStream automatically encodes to the local 8-bit encoding,
However, you can explicitly choose the encoding, .e.g. UTF-8 or even
UTF-16.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 17:19:16 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>??The PrintWriter class should be used in situations that require
>writing characters rather than bytes.??


PrintWriters are for writing text files. They have translation going
on. This would confound efforts to compose binary bytes. For than use
DataOutputStream.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
logging buffered vs. logging history Christian Roos Cisco 4 02-05-2006 10:55 PM
whether is the standard input stream full buffered or line buffered after calling function setbuf()? kernelxu@hotmail.com C Programming 9 08-23-2005 02:24 PM
couple of questions on streams Mantorok Redgormor C Programming 9 10-30-2004 09:17 AM
sending web response wihout buffered =?Utf-8?B?TWVobWV0?= ASP .Net 1 01-13-2004 11:55 AM
Questions of streams Als C++ 4 01-06-2004 06:32 PM



Advertisments