What does "Stream" and "Buffer" mean in Java I / O?

I just found out about I / O using BufferedReader .

I wanted to know what exactly mean the meaning of the term Stream and Buffer ?

And what does this line of code do for us:

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
+75
java buffer bufferedreader
Apr 13 '13 at 6:01
source share
6 answers

Java has two types of classes for input and output (I / O): streams and readers / writers .

Streams ( InputStream , OutputStream and everything that extends them) are designed to read and write binary data from files, a network, or any other device.

Readers and writers are designed to read and write text (characters). They are a layer on top of streams that converts binary data (bytes) into characters and vice versa using character encoding .

Reading data from byte to byte is very inefficient. One way to speed this up is to use a buffer: instead of reading one byte at a time, you read several thousand bytes at a time and put them in the buffer, in memory. Then you can look at the bytes in the buffer one by one.

The Oracle Java Tutorial on I / O explains this in detail.

Take a look at the line of code you provided:

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 

System.in is an InputStream . You create an InputStreamReader that reads bytes from System.in . Then you wrap it in a BufferedReader .

So in the end, you have a BufferedReader that reads with an InputStreamReader that reads from System.in .

+180
Apr 13 '13 at 6:06 on
source share

Well, this is a question in everbodys that starts working on the java.io package. To answer your questions, the terms InputStreamReader and BufferedReader represent only java objects (there is nothing special about them), but they are designed for io operations, such as reading and writing from / to different inputs / outputs, such as a file, an object, etc. d.

Now go to the line

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 

InputStreamReader is a class for reading an input byte stream. But reading each byte is expensive, so we wrap it around a BufferedReader so that it buffers (which is a decorator drawing).

So, what will happen, even before you start reading, bufferedReader will save some fragment of bytes in the register and when you do the read operation. it will be read from a place that is much cheaper than reading from the console / file. But in the case of InputStreamReader, when you perform a read operation every time a disk access operation occurs

+14
Apr 13 '13 at 6:06 on
source share

Buffer:

This is the area of physical memory used to temporarily store data as it moves from one place to another. In most cases, this physical memory will be RAM ( random access memory).

But from this context of the question, the buffer is used when reading / writing data. It does not need to be used when moving data from one place to another.

An example for a buffer: if your system has 4 GB of RAM, the system can allocate 4 KB of memory (RAM) for the buffer. KB - kilobytes (s), GB - gigabytes (s)

I / O stream (or) Stream:

An input / output stream is an input source or output destination. A stream can represent many different types of sources and destinations, including disk files, devices, other programs, and memory arrays.

I / O means I / O.

Thus, an Input Stream can be an input source, such as a file on disk, a network connection, etc.

And, Output Stream can be an output destination, such as a file on disk, a network connection, etc.

According to the official JAVA documentation , there are three types of streams.

  1. Byte streams (read or write bytes)
  2. Character streams (reading or writing characters)
  3. Buffered streams (read or write to the buffer for efficiency)

Byte streams:

They perform input and output of 8-bit bytes. All byte stream classes are derived from InputStream and OutputStream .

Bytes The input classes of the stream receive the input as raw bytes . The output byte classes give the output as raw bytes .

InputStream - Direct Known Subclasses

 AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream. 

OutputStream - Direct Known Subclasses

 ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream 

Character Streams: This is a layer on top of byte streams. They convert bytes (binary data) into characters and characters into bytes using character encoding.

All character stream classes come from Reader and Writer .

Reader - Direct Known Subclasses

 BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader 

Writer - Direct Known Subclasses

 BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter 

Byte streams and character streams use unbuffered I / O.

This means that every read or write request is processed directly by the underlying OS. This can make the program much less efficient, since each such request often causes disk access, network activity, or some other operation that is relatively expensive. To reduce this kind of overhead, the Java platform implements buffered I / O streams.

Buffered Streams:

Buffered input streams read data from a memory area known as a buffer ; The native input API is only called when the buffer is empty.

Similarly, buffered output streams write data to the buffer , and the native output API is called only when the buffer is full.

A program can convert an unbuffered stream to a buffered stream using the wrapping idiom, where the object of the unbuffered stream is passed to the constructor for the buffered stream class.

Example:

 inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt")); 

There are 4 classes of buffered streams that are used to package unbuffered streams:

To create buffered byte streams, use the BufferedInputStream and BufferedOutputStream classes.

To create buffered character streams, use the BufferedReader and BufferedWriter classes.

+12
Aug 18 '16 at 12:07 on
source share

Streams . Streams are data streams from which you can read or write. streams usually connect to a data source or data destination, such as a file, network connection, etc.

Buffer : a container for data of a certain primitive type. A buffer is a linear finite sequence of elements of a certain primitive type. In addition to its content, the essential properties of the buffer are its capacity, limit and position:

+5
Aug 04 '14 at 5:26
source share

A stream is a connection and actual information transferred between points. A buffer is a storage container that stores part or all of the streaming data and feeds it to an output device.

Of course, the fact is that if the stream slows down beyond the data rate necessary to display the data, then the output will be suspended. A buffer prevents this.

+3
Apr 13 '13 at 6:06 on
source share

A buffer is a part in memory that is used to store data stream from peripheral devices. Then from this buffer this data stream is collected and stored in variables. A stream can be defined as a continuous stream of data.

The term β€œI / O” itself means nothing more than moving data to and from buffers. Just keep that in mind all the time. The processes perform I / O by asking the operating system for data that should be unloaded from the buffer (write operation) or that the buffer is filled with data (read operation).
Logical data flow chart

Simply put, imagine that when you enter data on the keyboard, the data moves through the channel ( stream ) to the buffer, and then from the buffer to the disk (write operation). Similarly, when data is moved from disk to the buffer and from the buffer to the console, a read operation is performed.

You can read the links for a better understanding. Hope it helps !.
What is a buffer in Java
enter link description here

0
Jul 25 '19 at 5:07
source share



All Articles