Top Description Inners Fields Constructors Methods
com.google.common.io

public final Class ByteStreams

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkPositionIndex, .Preconditions.checkPositionIndexes, java.lang.Math.max, .Math.min

Provides utility methods for working with byte arrays and I/O streams.
Authors
Chris Nokleberg, Colin Decker
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
MAX_ARRAY_LEN

Max array length on JVM.

private static final OutputStream
private static final int
TO_BYTE_ARRAY_DEQUE_SIZE

Large enough to never need to expand, given the geometric progression of buffer sizes.

private static final int
ZERO_COPY_CHUNK_SIZE

There are three methods to implement FileChannel#transferTo(long, long, WritableByteChannel):

  1. Use sendfile(2) or equivalent. Requires that both the input channel and the output channel have their own file descriptors. Generally this only happens when both channels are files or sockets. This performs zero copies - the bytes never enter userspace.
  2. Use mmap(2) or equivalent. Requires that either the input channel or the output channel have file descriptors. Bytes are copied from the file into a kernel buffer, then directly into the other buffer (userspace). Note that if the file is very large, a naive implementation will effectively put the whole file in memory. On many systems with paging and virtual memory, this is not a problem - because it is mapped read-only, the kernel can always page it to disk "for free". However, on systems where killing processes happens all the time in normal conditions (i.e., android) the OS must make a tradeoff between paging memory and killing other processes - so allocating a gigantic buffer and then sequentially accessing it could result in other processes dying. This is solvable via madvise(2), but that obviously doesn't exist in java.
  3. Ordinary copy. Kernel copies bytes into a kernel buffer, from a kernel buffer into a userspace buffer (byte[] or ByteBuffer), then copies them from that buffer into the destination channel.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static byte[]
combineBuffers(Queue<byte[]> bufs, int totalLen)

public static long

Returns:

the number of bytes copied
copy
(InputStream
the input stream to read from
from
,
OutputStream
the output stream to write to
to
)

Copies all bytes from the input stream to the output stream.

public static long

Returns:

the number of bytes copied
copy
(ReadableByteChannel
the readable channel to read from
from
,
WritableByteChannel
the writable channel to write to
to
)

Copies all bytes from the readable channel to the writable channel.

pack-priv static byte[]
createBuffer()

Creates a new byte array for buffering reads or writes.

public static long
exhaust(InputStream in)

Reads and discards data from the given InputStream until the end of the stream is reached.

public static InputStream

Returns:

a length-limited InputStream
limit
(InputStream
the input stream to be wrapped
in
,
long
the maximum number of bytes to be read
limit
)

Wraps a InputStream, limiting the number of bytes which can be read.

public static ByteArrayDataInput
newDataInput(byte[] bytes)

Returns a new ByteArrayDataInput instance to read from the bytes array from the beginning.

public static ByteArrayDataInput
newDataInput(byte[] bytes, int start)

Returns a new ByteArrayDataInput instance to read from the bytes array, starting at the given position.

public static ByteArrayDataInput
newDataInput(ByteArrayInputStream byteArrayInputStream)

Returns a new ByteArrayDataInput instance to read from the given ByteArrayInputStream.

public static ByteArrayDataOutput
newDataOutput()

Returns a new ByteArrayDataOutput instance with a default size.

public static ByteArrayDataOutput
newDataOutput(int size)

Returns a new ByteArrayDataOutput instance sized to hold size bytes before resizing.

public static ByteArrayDataOutput
newDataOutput(ByteArrayOutputStream byteArrayOutputStream)

Returns a new ByteArrayDataOutput instance which writes to the given ByteArrayOutputStream.

public static OutputStream
nullOutputStream()

Returns an OutputStream that simply discards written bytes.

public static int

Returns:

the number of bytes read
read
(InputStream
the input stream to read from
in
,
byte[]
the buffer into which the data is read
b
,
int
an int specifying the offset into the data
off
,
int
an int specifying the number of bytes to read
len
)

Reads some bytes from an input stream and stores them into the buffer array b.

public static <T extends @Nullable Object> T

Returns:

the result of the byte processor
readBytes
(InputStream
the input stream to process
input
,
ByteProcessor<T>
the object to which to pass the bytes of the stream
processor
)

Process the bytes of the given input stream using the given processor.

public static void
readFully(InputStream
the input stream to read from.
in
,
byte[]
the buffer into which the data is read.
b
)

Attempts to read enough bytes from the stream to fill the given byte array, with the same behavior as DataInput#readFully(byte[]).

public static void
readFully(InputStream
the input stream to read from.
in
,
byte[]
the buffer into which the data is read.
b
,
int
an int specifying the offset into the data.
off
,
int
an int specifying the number of bytes to read.
len
)

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

public static void
skipFully(InputStream
the input stream to read from
in
,
long
the number of bytes to skip
n
)

Discards n bytes of data from the input stream.

private static long
skipSafely(InputStream in, long n)

Attempts to skip up to n bytes from the given input stream, but not more than in.available() bytes.

pack-priv static long
skipUpTo(InputStream in, long n)

Discards up to n bytes of data from the input stream.

public static byte[]

Returns:

a byte array containing all the bytes from the stream
toByteArray
(InputStream
the input stream to read from
in
)

Reads all bytes from an input stream into a byte array.

pack-priv static byte[]
toByteArray(InputStream in, long expectedSize)

Reads all bytes from an input stream into a byte array.

private static byte[]
toByteArrayInternal(InputStream in, Queue<byte[]> bufs, int totalLen)

Returns a byte array containing the bytes from the buffers already in bufs (which have a total combined length of totalLen bytes) followed by all bytes remaining in the given input stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait