public class

ByteArrayInputStream

extends InputStream
java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.ByteArrayInputStream

Class Overview

A specialized InputStream for reading the contents of a byte array.

Summary

Fields
protected byte[] buf The byte array containing the bytes to stream over.
protected int count The total number of bytes initially available in the byte array buf.
protected int mark The current mark position.
protected int pos The current position within the byte array.
Public Constructors
ByteArrayInputStream(byte[] buf)
Constructs a new ByteArrayInputStream on the byte array buf.
ByteArrayInputStream(byte[] buf, int offset, int length)
Constructs a new ByteArrayInputStream on the byte array buf with the initial position set to offset and the number of bytes available set to offset + length.
Public Methods
synchronized int available()
Returns the number of remaining bytes.
void close()
Closes this stream and frees resources associated with this stream.
synchronized void mark(int readlimit)
Sets a mark position in this ByteArrayInputStream.
boolean markSupported()
Indicates whether this stream supports the mark() and reset() methods.
synchronized int read()
Reads a single byte from the source byte array and returns it as an integer in the range from 0 to 255.
synchronized int read(byte[] buffer, int offset, int length)
Reads at most len bytes from this stream and stores them in byte array b starting at offset.
synchronized void reset()
Resets this stream to the last marked location.
synchronized long skip(long n)
Skips count number of bytes in this InputStream.
[Expand]
Inherited Methods
From class java.io.InputStream
From class java.lang.Object
From interface java.io.Closeable

Fields

protected byte[] buf

Since: API Level 1

The byte array containing the bytes to stream over.

protected int count

Since: API Level 1

The total number of bytes initially available in the byte array buf.

protected int mark

Since: API Level 1

The current mark position. Initially set to 0 or the offset parameter within the constructor.

protected int pos

Since: API Level 1

The current position within the byte array.

Public Constructors

public ByteArrayInputStream (byte[] buf)

Since: API Level 1

Constructs a new ByteArrayInputStream on the byte array buf.

Parameters
buf the byte array to stream over.

public ByteArrayInputStream (byte[] buf, int offset, int length)

Since: API Level 1

Constructs a new ByteArrayInputStream on the byte array buf with the initial position set to offset and the number of bytes available set to offset + length.

Parameters
buf the byte array to stream over.
offset the initial position in buf to start streaming from.
length the number of bytes available for streaming.

Public Methods

public synchronized int available ()

Since: API Level 1

Returns the number of remaining bytes.

Returns
  • count - pos

public void close ()

Since: API Level 1

Closes this stream and frees resources associated with this stream.

Throws
IOException if an I/O error occurs while closing this stream.

public synchronized void mark (int readlimit)

Since: API Level 1

Sets a mark position in this ByteArrayInputStream. The parameter readlimit is ignored. Sending reset() will reposition the stream back to the marked position.

Parameters
readlimit ignored.

public boolean markSupported ()

Since: API Level 1

Indicates whether this stream supports the mark() and reset() methods. Returns true since this class supports these methods.

Returns
  • always true.
See Also

public synchronized int read ()

Since: API Level 1

Reads a single byte from the source byte array and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the source array has been reached.

Returns
  • the byte read or -1 if the end of this stream has been reached.

public synchronized int read (byte[] buffer, int offset, int length)

Since: API Level 1

Reads at most len bytes from this stream and stores them in byte array b starting at offset. This implementation reads bytes from the source byte array.

Parameters
buffer the byte array in which to store the bytes read.
offset the initial position in b to store the bytes read from this stream.
length the maximum number of bytes to store in b.
Returns
  • the number of bytes actually read or -1 if no bytes were read and the end of the stream was encountered.
Throws
IndexOutOfBoundsException if offset < 0 or length < 0, or if offset + length is greater than the size of b.
NullPointerException if b is null.

public synchronized void reset ()

Since: API Level 1

Resets this stream to the last marked location. This implementation resets the position to either the marked position, the start position supplied in the constructor or 0 if neither has been provided.

See Also

public synchronized long skip (long n)

Since: API Level 1

Skips count number of bytes in this InputStream. Subsequent read()s will not return these bytes unless reset() is used. This implementation skips count number of bytes in the target stream. It does nothing and returns 0 if n is negative.

Parameters
n the number of bytes to skip.
Returns
  • the number of bytes actually skipped.