public final class

ByteStreams

extends Object
java.lang.Object
   ↳ com.facebook.common.internal.ByteStreams

Class Overview

Provides utility methods for working with byte arrays and I/O streams.

Summary

Public Methods
static long copy(InputStream from, OutputStream to)
Copies all bytes from the input stream to the output stream.
static int read(InputStream in, byte[] b, int off, int len)
Reads some bytes from an input stream and stores them into the buffer array b.
static void readFully(InputStream in, byte[] b, int off, int len)
Attempts to read len bytes from the stream into the given array starting at off, with the same behavior as readFully(byte[], int, int).
static byte[] toByteArray(InputStream in, int expectedSize)
Reads all bytes from an input stream into a byte array.
static byte[] toByteArray(InputStream in)
Reads all bytes from an input stream into a byte array.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static long copy (InputStream from, OutputStream to)

Copies all bytes from the input stream to the output stream. Does not close or flush either stream.

Parameters
from the input stream to read from
to the output stream to write to
Returns
  • the number of bytes copied
Throws
IOException if an I/O error occurs

public static int read (InputStream in, byte[] b, int off, int len)

Reads some bytes from an input stream and stores them into the buffer array b. This method blocks until len bytes of input data have been read into the array, or end of file is detected. The number of bytes read is returned, possibly zero. Does not close the stream.

A caller can detect EOF if the number of bytes read is less than len. All subsequent calls on the same stream will return zero.

If b is null, a NullPointerException is thrown. If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown. If len is zero, then no bytes are read. Otherwise, the first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len.

Parameters
in the input stream to read from
b the buffer into which the data is read
off an int specifying the offset into the data
len an int specifying the number of bytes to read
Returns
  • the number of bytes read
Throws
IOException if an I/O error occurs

public static void readFully (InputStream in, byte[] b, int off, int len)

Attempts to read len bytes from the stream into the given array starting at off, with the same behavior as readFully(byte[], int, int). Does not close the stream.

Parameters
in the input stream to read from.
b the buffer into which the data is read.
off an int specifying the offset into the data.
len an int specifying the number of bytes to read.
Throws
EOFException if this stream reaches the end before reading all the bytes.
IOException if an I/O error occurs.

public static byte[] toByteArray (InputStream in, int expectedSize)

Reads all bytes from an input stream into a byte array. The given expected size is used to create an initial byte array, but if the actual number of bytes read from the stream differs, the correct result will be returned anyway.

Throws
IOException

public static byte[] toByteArray (InputStream in)

Reads all bytes from an input stream into a byte array. Does not close the stream.

Parameters
in the input stream to read from
Returns
  • a byte array containing all the bytes from the stream
Throws
IOException if an I/O error occurs