Java - IO - Connection (Stream and Channel)

> Procedural Languages > Java > Java - IO (Input/Output) - (File System|File|Console)

1 - About

I/O - Stream in Java.

In order to perform I/O operations (for example reading or writing), you need to perform a connection.

In Java, this connection are modelled through:

They are classes (and methods) that opens connection to an entity such as:

  • a file (mostly),
  • a hardware device,
  • a network socket,
  • or a program component

that is capable of performing one or more distinct I/O operations, for example reading or writing.

A stream or channel can be seen as a sequential byte (read and write operations):

  • you open a (channel|connection) from an entity (mostly a file)
  • ask or write repeatedly a quantity of data (by byte of array)
  • transform the byte (mostly as characters)
  • close (release) the (entity|channel|connection) when a condition is met (End of File, Until some address)
Advertising

3 - I/O Method

4 - Stream Operations

Streams may be:

  • Chained: Streams of the same direction (Input|Ouptut) can be chained to another by passing it to the constructor of some second stream.
  • Concatenated via a SequenceInputStream (A SequenceInputStream represents the logical concatenation of other input streams)
  • Piped Data is read from a (PipedInputStream|PipedReader) object by one thread and data is written to the corresponding (PipedOutputStream|PipedWriter) by some other thread.

5 - Architecture

All other stream types are built on the byte streams FileInputStream and FileOutputStream (reading/writing file one byte at a time) See Java - IO - Byte Stream

5.1 - Byte

5.2 - Data Type

5.2.1 - Characters

5.2.2 - Java Primitive

  • DataInputStream: A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way.
  • DataOutputStream: A data output stream lets an application write primitive Java data types to an output stream in a portable way.
Advertising

5.2.3 - Java Object

  • ObjectInputStream: An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
  • ObjectOutputStream: An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream.

5.3 - Filter

Filtered Streams = Transformation or More Functionalities on Streams.

A Filter(Input|Output)Stream uses (Input|Output) stream as its (source|target) of data:

  • possibly transforming the data along the way
  • or providing additional functionality.

The (superclass|abstract class) are:

Advertising

5.4 - Standard Stream

6 - Management Operations

6.1 - Close

Closing a stream when it's no longer needed is very important.

The use of a finally block to guarantee that streams will be closed even if an error occurs and helps avoid serious resource leaks.

Copy:

try {
    in = new FileInputStream(Parameters.FILE_PATH_READ);
    out = new FileOutputStream(Parameters.FILE_PATH_WRITE);
    int c;
 
    while ((c = in.read()) != -1) {
        out.write(c);
    }
} finally {
    if (in != null) {
        in.close();
    }
    if (out != null) {
        out.close();
    }
}

One stream can be chained to another by passing it to the constructor of some second stream. When this second stream is closed, then it automatically closes the original underlying stream as well.

If multiple streams are chained together, then closing the one which was the last to be constructed, and is thus at the highest level of abstraction, will automatically close all the underlying streams. So, one only has to call close on one stream in order to close an entire series of related streams.

You should close the outermost OutputStream or Writer you have created from the socket output stream.

When a stream is closed, the connection between the stream and the entity (mostly a file) is canceled. After you have closed a stream, you cannot perform any additional operations on it.

7 - Documentation / Reference

lang/java/io/stream.txt · Last modified: 2017/10/08 13:16 by gerardnico