Output

The OutputStream class is used to write single bytes or arrays of bytes to a stream.

  // Methods
void write(int b); // writes the specified byte to the output stream (least significant byte of the integer)
// this is because there are no unsigned types in Java
void write(byte[] b);
void write(byte[] b, int off, int len);
void flush();
void close();
  

Input

The InputStream class is used to read single bytes or arrays of bytes from a stream.

  // Methods
int read(); // reads the next byte of data from the input stream
// returns -1 if the end of the stream is reached
int read(byte[] b);
int read(byte[] b, int off, int len);
long skip(long n);
int available();
void close();

// Special
void mark(int readlimit); // marks the current position in the input stream
void reset(); // resets the input stream to the last marked position
boolean markSupported();

// Since Java 9
byte[] readAllBytes();
int readNBytes(byte[] b, int off, int len);
long transferTo(OutputStream out);
// Since Java 11
byte[] readNBytes(int len);
skipNBytes(long n);
nullInputStream();
  

Reading Pattern

The following pattern is used to read from an input stream:

  int len = 256;
byte[] buf = new byte[len];
int bytesRead = 0;
while(bytesRead < len) {
    int n = in.read(buf, read, len - read);
    if(n == -1) break;
    bytesRead += n;
}

// Since Java 11
byte[] buf = in.readNBytes(len);
  

Filter Streams

Filter streams are decorators for other streams. They add functionality to the underlying stream.

Examples:

  • BufferedInputStream and BufferedOutputStream add buffering
  • DataInputStream and DataOutputStream add support for reading and writing primitive data types
  • ObjectInputStream and ObjectOutputStream add support for reading and writing objects
  • PrintStream adds support for formatted output

Object Serialization

Object serialization is the process of converting an object into a stream of bytes. Java supports object serialization through the ObjectInputStream and ObjectOutputStream classes. Classes that implement the Serializable marker interface can be serialized.

Serial Version UID

The serialVersionUID is a unique identifier for the class. It is used to ensure that the serialized object can be deserialized correctly. If the serialVersionUID is not specified, the JVM will generate one based on the class definition.

The version UID depends on:

  • the number of fields and methods in the class
  • the names of the fields and methods
  • the access modifiers of the fields and methods
  • the types of the fields and methods

If the class definition changes, the serialVersionUID should be updated to reflect the changes.

The version UID can also be specified manually:

  private static final long serialVersionUID = 1L;
  

Summary

The complete overview of Java stream classes

classDiagram
    InputStream <|-- FilterInputStream
    InputStream <|-- ObjectInputStream
    InputStream <|-- FileInputStream
    InputStream <|-- SocketInputStream
    InputStream <|-- ByteArrayInputStream
    InputStream <|-- SequenceInputStream
    FilterInputStream <|-- BufferedInputStream
    FilterInputStream <|-- DataInputStream
    FilterInputStream <|-- PushbackInputStream
    FilterInputStream <|-- InflaterInputStream
    FilterInputStream <|-- DigestInputStream
    FilterInputStream <|-- CipherInputStream
    FilterInputStream <|-- CheckedInputStream
    FilterInputStream *-- InputStream
    ObjectInputStream *-- InputStream
    SequenceInputStream *-- InputStream
classDiagram
    OutputStream <|-- FilterOutputStream
    OutputStream <|-- ObjectOutputStream
    OutputStream <|-- FileOutputStream
    OutputStream <|-- SocketOutputStream
    OutputStream <|-- ByteArrayOutputStream
    FilterOutputStream <|-- BufferedOutputStream
    FilterOutputStream <|-- DataOutputStream
    FilterOutputStream <|-- PushbackOutputStream
    FilterOutputStream <|-- InflaterOutputStream
    FilterOutputStream <|-- DigestOutputStream
    FilterOutputStream <|-- CipherOutputStream
    FilterOutputStream <|-- CheckedOutputStream
    FilterOutputStream *-- OutputStream
    ObjectOutputStream *-- OutputStream