Java Streams

Streams
  • A stream is a sequence of data. 
  • In Java, a stream is composed of bytes. 
  • It's called a stream because it is a continuous flow of data. 

Use of I/O Streams:

The primary use of I/O streams is to perform data transfer between a Java program and an external entity or another part of the same program. Common use cases include:

  • File Operations: Reading data from files and writing data to files.
  • Network Communication: Sending and receiving data over network connections (using Sockets which provide InputStream and OutputStream).
  • In-Memory Data Handling: Treating byte arrays or character arrays as sources or destinations of data.
  • Data Conversion: Converting between byte streams and character streams using specified encodings.
  • Buffering: Improving the efficiency of I/O operations by reading or writing data in larger chunks.
  • Data Formatting: Writing formatted output using PrintStream and PrintWriter.
  • Object Persistence: Saving the state of Java objects to storage and restoring them later using object serialization streams.
  • Inter-Thread Communication: Allowing threads within the same program to exchange data using piped streams.

Character Streams:

  • Abstract Base Classes:

    • Reader: Represents an input stream of characters. Provides methods for reading characters.
    • Writer: Represents an output stream of characters. Provides methods for writing characters.
  • Common Concrete Implementations:

    • File I/O:
      • FileReader: Reads characters from a file (uses the default character encoding).
      • FileWriter: Writes characters to a file (uses the default character encoding).
    • Memory I/O:
      • CharArrayReader: Reads characters from a character array in memory.
      • CharArrayWriter: Writes characters to a character array in memory.
      • StringReader: Reads characters from a String.
      • StringWriter: Writes characters to a String buffer.
    • Buffered I/O (for efficiency):
      • BufferedReader: Adds buffering to a character input stream, reading characters in larger chunks and providing line-reading capabilities (readLine()). Often wraps other Reader implementations.
      • BufferedWriter: Adds buffering to a character output stream, writing characters in larger chunks and providing a newLine() method. Often wraps other Writer implementations.
    • Character Encoding Conversion:
      • InputStreamReader: An adapter that converts a byte input stream (InputStream) into a character input stream (Reader) using a specified character encoding.
      • OutputStreamWriter: An adapter that converts a character output stream (Writer) into a byte output stream (OutputStream) using a specified character encoding.
    • Print Writer (for formatted character output):
      • PrintWriter: Adds functionality to print formatted representations of various data values as characters. It's similar to PrintStream but operates on character streams.
    • Piped Streams (for inter-thread character communication):
      • PipedReader: The receiving end of a character pipe.
      • PipedWriter: The sending end of a character pipe.

Byte Streams:

  • Abstract Base Classes:

    • InputStream: Represents an input stream of bytes. It provides methods for reading bytes from a source.
    • OutputStream: Represents an output stream of bytes. It provides methods for writing bytes to a destination.
  • Common Concrete Implementations:

    • File I/O:
      • FileInputStream: Reads bytes from a file.
      • FileOutputStream: Writes bytes to a file.
    • Memory I/O:
      • ByteArrayInputStream: Reads bytes from a byte array in memory.
      • ByteArrayOutputStream: Writes bytes to a byte array in memory.
    • Buffered I/O (for efficiency):
      • BufferedInputStream: Adds buffering to an input stream, reading data in larger chunks. Often wraps other InputStream implementations.
      • BufferedOutputStream: Adds buffering to an output stream, writing data in larger chunks. Often wraps other OutputStream implementations.
    • Data Conversion:
      • DataInputStream: Reads primitive Java data types (int, float, boolean, etc.) from an underlying input stream in a machine-independent way.
      • DataOutputStream: Writes primitive Java data types to an underlying output stream in a machine-independent way.
    • Object Serialization:
      • ObjectInputStream: Reads Java objects that were previously serialized.
      • ObjectOutputStream: Writes Java objects in a serialized format.
    • Piped Streams (for inter-thread communication):
      • PipedInputStream: The receiving end of a pipe.
      • PipedOutputStream: The sending end of a pipe.
    • Filtering Streams (for data transformation):
      • FilterInputStream: An abstract class that provides a basis for filtering input streams (e.g., BufferedInputStream, DataInputStream).
      • FilterOutputStream: An abstract class that provides a basis for filtering output streams (e.g., BufferedOutputStream, DataOutputStream).
    • Print Stream (for formatted output):
      • PrintStream: Adds functionality to print formatted representations of various data values. System.out and System.err are instances of PrintStream.

Comments

Popular Posts