Java Byte Stream

A byte stream controls input and output in 8-bit byte units, so it can be treated as binary data. Among the classes in the java.io package, the byte stream-related classes are as follows.

  • InputStream
    • FileInputStream
    • FilterInputStream
      • BufferedInputStream
      • DataInputStream
    • ObjectInputStream
  • OutputStream
    • FileOutputStream
    • FilterOutputStream
      • BufferedOutputStream
      • DataOutputStream
    • ObjectOutputStream

InputStream/OutputStream Abstract Classes

Because the InputStream and OutputStream classes are abstract classes, actual objects cannot be created from them.

InputStream Class

The InputStream class provides byte stream input functionality.

InputStream Methods

The InputStream class has methods that read data from an input source.

Method Description
int available() throws IOException Returns the number of bytes currently available to read.
void close() throws IOException Closes the input stream.
void mark(int readlimit) Marks the current position in the input stream.
boolean markSupported() Checks whether this input stream has a point specified by mark().
abstract int read() throws IOException Reads one byte from the input stream and returns it as an int value(0 to 255). Returns -1 if there is no more value to read.
int read(byte[] b) throws IOException Reads data up to byte[] b, stores it in b, and returns the number of bytes read.
int read(byte[] b, int off, int len) throws IOException Reads len bytes, stores them at position off in byte[] b, and returns the number of bytes read.
void reset() Moves to the position where mark() was last called.
long skip(long n) throws IOException Skips n bytes of data from the input stream and returns the number of bytes skipped.

Among the methods in the InputStream class, the abstract method read() is used to input data. The read() method reads 1 byte from the input stream, and when it reaches EOF(End of File), it returns -1 to indicate that the read operation has ended.

If the read() method reads 1 byte at a time, the read(byte[] b) method uses a user-specified byte[] to read the desired size all at once.

The return data type of the read() method is int, not byte. Java’s numeric system can represent byte-based values from -128 to 127. However, values read through the read() method are represented as positive values, and in this case only 0 to 127 can be used. To solve this, a larger data type is returned so that values from 0 to 255 can be used, and the negative value -1 is handled as a special input value.

The available() method can obtain the number of bytes that can be read from the stream, and then use that number to create a byte[] and read them all at once.

OutputStream Class

The OutputStream class provides byte stream output functionality.

OutputStream Methods

The OutputStream class has methods that output byte streams.

Method Description
void close() throws IOException Closes the output stream.
void flush() throws IOException Outputs all remaining output stream data in the buffer.
abstract void write(int i) throws IOException Outputs the lower 8 bits of integer i.
void write(byte buf[]) throws IOException Outputs the contents of buf.
void write(byte buf[], int index, int size) throws IOException Outputs size bytes starting from index in buf.

There are three forms of output methods. The first is the write(int i) method. This method outputs 1 byte, receives an integer(0 to 255) instead of a byte as an argument, and is declared as an abstract method that is overridden in subclasses. The write(byte buf[]) method receives bytes as an array and outputs them. The write(byte buf[], int index, int size) method receives a byte array along with a starting position and size.

The flush() method is related to the buffer, which helps input and output run a little faster. When data is output by calling the write method, the data is not output immediately but is first accumulated in the buffer. After some data has accumulated in the buffer, calling the flush() method finally outputs the data and empties the buffer. If flush() is not called, output data may remain only in the buffer and may not be output. For this reason, subclasses are usually implemented so that flush() is called automatically.