Buffered Stream class with its methods and examples


What & Why Buffered Stream class:
  • Most of the examples we taken so far use unbuffered I/O. 
  • What is unbuffered I/O?
    • This means each read or write request is handled directly by the underlying OS.
    • This can make a program much less efficient since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
  • Why buffered I/O?
    • To reduce this kind of overhead, the Java platform implements buffered I/O streams. 
    • Buffered input streams read data from a memory area known as a buffer; 
      • The native input API is called only when the buffer is empty. 
    • Similarly, buffered output streams write data to a buffer;
      • The native output API is called only when the buffer is full.
  • A JAVA program can convert an unbuffered stream into a buffered stream using the buffer input and output classes.
  • Example: 
    • The unbuffered stream object is passed to the constructor for a buffered stream class. 
                              inputStream = new BufferedReader(new FileReader("Sarthak.txt"));
                              outputStream = new BufferedWriter(new FileWriter("CDPatel.txt"));
  • There are four buffered stream classes used to wrap unbuffered streams: 
  1. BufferedInputStream and BufferedOutputStream create buffered byte streams.
  2. BufferedReader and BufferedWriter create buffered character streams.
  • What is Flushing in Buffered Streams?
    • It often makes sense to write out a buffer at critical points, without waiting for it to fill is known as flushing the buffer.
    • Some buffered output classes support autoflush. 
    • AutoFlush
      • When autoflush is enabled, certain key events cause the buffer to be flushed. 
      • For example, an autoflush PrintWriter object flushes the buffer on every invocation of println or format. 
    • Manual Flush
      • To flush a stream manually, invoke its flush method. 
      • The flush method is valid on any output stream but has no effect unless the stream is buffered.
BufferedInputStream class:

  • Syntax: 
          java.lang.Object
                  java.io.InputStream
                          java.io.FilterInputStream
                                  java.io.BufferedInputStream
  • A BufferedInputStream adds functionality to unbuffered class to buffered class. 
  • When the BufferedInputStream is created, an internal buffer array is created. 
  • As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. 
  • The mark operation remembers a point in the input stream.
  • It has five fields, two constructors and seven methods.
  • Field: 
    • 1. buf
      • Syntax: protected volatile byte[] buf
      • Use: The internal buffer array where the data is stored.
    • 2. count
      • Syntax: protected int count
      • Use: The index one greater than the index of the last valid byte in the buffer.
    • 3. marklimit
      • Syntax: protected int marklimit
      • Use: The maximum read ahead allowed after a call to the mark method before subsequent calls to the reset method fail.
    • 4. markpos
      • Syntax: protected int markpos
      • Use: The value of the pos field at the time the last mark method was called.
    • 5. pos
      • Syntax: protected int pos
      • Use: The current position in the buffer. This is the index of the next character to be read from the buf array.
  • Constructor: 
    • Syntax:
      • public BufferedInputStream(InputStream in)
      • public BufferedInputStream(InputStream in, int size)
    • Use
      • Creates a BufferedInputStream and saves its argument, the input stream in, for later use. An internal buffer array is created and stored in buf.
    • Parameters:
      • in - the underlying input stream.
      • size - the buffer size.
    • Throws:
      • IllegalArgumentException - if size <= 0.
  • Methods:
    • 1. available
      • Syntax: public int available() throws IOException
      • Override: available in class FilterInputStream extends InputStream
    • 2. close:
      • Syntax: public void close() throws IOException
      • Override: available in class FilterInputStream extends InputStream
    • 3. mark:
      • Syntax: public void mark(int readlimit)
      • Override: available in class FilterInputStream extends InputStream
    • 4. markSupported
      • Syntax: public boolean markSupported()
      • Override: available in class FilterInputStream extends InputStream
    • 5. read
      • Syntax
        • public int read() throws IOException
        • public int read(byte[] b, int off, int len) throws IOException
      • Override: available in class FilterInputStream extends InputStream
    • 6. reset
      • Syntax : public void reset() throws IOException
      • Override: available in class FilterInputStream extends InputStream
    • 7. skip
      • Syntax: public long skip(long n) throws IOException
      • Override: available in class FilterInputStream extends InputStream
BufferedOutputStream class:
  • Syntax: 
            java.lang.Object
                   java.io.OutputStream
                          java.io.FilterOutputStream
                                 java.io.BufferedOutputStream
  • The class implements a buffered output stream. 
  • By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.
  • It has two fields, two constructors and two methods.
  • Field: 
    • 1. buf
      • Syntax: protected byte[] buf
      • Use: The internal buffer where data is stored.
    • 2. count
      • Syntax: protected int count
      • Use: The number of valid bytes in the buffer. This value is always in the range 0 through buf.length; elements buf[0] through buf[count-1] contain valid byte data.
  • Constructor: 
    • Syntax:
      • public BufferedOutputStream(OutputStream out)
      • public BufferedOutputStream(OutputStream out, int size)
    • Use:
      • Creates a new buffered output stream to write data to the specified underlying output stream.
    • Parameters:
      • out - the underlying output stream.
      • size - the buffer size.
    • Throws:
      • IllegalArgumentException - if size <= 0.
  • Methods:
    • 1. flush
      • Syntax: public void flush() throws IOException
      • Overrides: flush in class FilterOutputStream extend OutputStream
    • 2. write
      • Syntax:
        • public void write(byte[] b, int off, int len) throws IOException
        • public void write(int b) throws IOException
      • Overrides: write in class FilterOutputStream extend OutputStream
BufferedReader class:

  • Syntax: 
            java.lang.Object
                   java.io.Reader
                           java.io.BufferedReader

  • Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
  • The buffer size may be specified, or the default size may be used. 
  • The default is large enough for most purposes.
  • For example: BufferedReader in = new BufferedReader(new FileReader("Sarthak.txt"));
  • It has one field, two constructors and eight methods.
  • Field: lock field is inherited from class java.io.Reader
  • Constructors: BufferedReader
    • Syntax:
      • public BufferedReader(Reader in)
      • public BufferedReader(Reader in, int sz)
    • Use:
      • Creates a buffering character-input stream that uses a default-sized input buffer.
    • Parameters:
      • in - A Reader
      • sz - Input-buffer size
    • Throws:
      • IllegalArgumentException - If sz <= 0
  • Methods:
    • 1. close: 
      • Syntax: public void close() throws IOException
      • Use: Closes the stream and releases any system resources associated with it.
      • Throws:
        • IOException - If an I/O error occurs
    • 2. lines: 
      • Syntax: public Stream lines()
      • Use: Returns a Stream, the elements of which are lines read from this BufferedReader.
      • Returns: a Stream providing the lines of text described by this BufferedReader
    • 3. mark:
      • Syntax: public void mark(int readAheadLimit) throws IOException
      • Overrides: mark in class Reader
    • 4. markSupported:
      • Syntax: public boolean markSupported()
      • Overrides: markSupported in class Reader
    • 5. read:
      • Syntax: 
        • public int read() throws IOException
        • public int read(char[] cbuf, int off, int len) throws IOException
      • Overrides: read in class Reader
    • 6. readLine:
      • Syntax: public String readLine() throws IOException
      • Use: 
        • Reads a line of text. 
        • A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
      • Returns:
        • A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
      • Throws: IOException - If an I/O error occurs
    • 7. ready:
      • Syntax: public boolean ready() throws IOException
      • Overrides: ready in class Reader
    • 8. reset:
      • Syntax: public void reset() throws IOException
      • Overrides: reset in class Reader
    • 9. skip:
      • Syntax: public long skip(long n) throws IOException
      • Overrides: skip in class Reader
BufferedWriter class:
  • Syntax: 
            java.lang.Object
                   java.io.Writer
                        java.io.BufferedWriter
  • Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
  • The default is large enough for most purposes.
  • The buffer size may be specified, or the default size may be accepted. 
  • For example: PrintWriter out  = new PrintWriter(new BufferedWriter(new FileWriter("ampics.txt")));
  • It has one field, two constructors and four methods.
  • Field: lock field is inherited from class java.io.Writer
  • Constructors:
    • Syntax
      • public BufferedWriter(Writer out)
      • public BufferedWriter(Writer out, int sz)
    • Use:
      • Creates a buffered character-output stream that uses a default-sized output buffer.
    • Parameters:
      • out - A Writer
      • sz - Output-buffer size, a positive integer
    • Throws:
      • IllegalArgumentException - If sz <= 0
  • Methods:
    • 1. close:
      • Syntax: public void close() throws IOException
      • Use: Closes the stream, flushing it first.
      • Throws: IOException - If an I/O error occurs
    • 2. flush
      • Syntax: public void flush() throws IOException
      • Use: Flushes the stream.
      • Throws: IOException - If an I/O error occurs
    • 3. newLine:
      • Syntax: public void newLine() throws IOException
      • Use:
        • Writes a line separator. 
        • The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.
      • Throws: IOException - If an I/O error occurs
    • 4. write:
      • Syntax:
        • public void write(int c) throws IOException
        • public void write(char[] cbuf, int off, int len) throws IOException
        • public void write(String s, int off, int len) throws IOException
      • Use:
        • Writes a single character.
        • Writes a portion of an array of characters.
        • Writes a portion of a String.
      • Overrides:
        • write in class Writer

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form