Package java.io
Class ByteArrayInputStream
java.lang.Object
java.io.InputStream
java.io.ByteArrayInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
public class ByteArrayInputStream extends InputStream
A specialized
InputStream for reading the contents of a byte array.- See Also:
ByteArrayOutputStream
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThebytearray containing the bytes to stream over.protected intcountThe total number of bytes initially available in the byte arraybuf.protected intmarkThe current mark position.protected intposThe current position within the byte array. -
Constructor Summary
Constructors Constructor Description ByteArrayInputStream(byte[] buf)Constructs a newByteArrayInputStreamon the byte arraybuf.ByteArrayInputStream(byte[] buf, int offset, int length)Constructs a newByteArrayInputStreamon the byte arraybufwith the initial position set tooffsetand the number of bytes available set tooffset+length. -
Method Summary
Modifier and Type Method Description intavailable()Returns the number of remaining bytes.voidclose()Closes this stream and frees resources associated with this stream.voidmark(int readlimit)Sets a mark position in this ByteArrayInputStream.booleanmarkSupported()Indicates whether this stream supports themark()andreset()methods.intread()Reads a single byte from the source byte array and returns it as an integer in the range from 0 to 255.intread(byte[] buffer, int byteOffset, int byteCount)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.voidreset()Resets this stream to the last marked location.longskip(long byteCount)SkipsbyteCountbytes in this InputStream.Methods inherited from class java.io.InputStream
read
-
Field Details
-
buf
protected byte[] bufThebytearray containing the bytes to stream over. -
pos
protected int posThe current position within the byte array. -
mark
protected int markThe current mark position. Initially set to 0 or theoffsetparameter within the constructor. -
count
protected int countThe total number of bytes initially available in the byte arraybuf.
-
-
Constructor Details
-
ByteArrayInputStream
public ByteArrayInputStream(byte[] buf)Constructs a newByteArrayInputStreamon the byte arraybuf.- Parameters:
buf- the byte array to stream over.
-
ByteArrayInputStream
public ByteArrayInputStream(byte[] buf, int offset, int length)Constructs a newByteArrayInputStreamon the byte arraybufwith the initial position set tooffsetand the number of bytes available set tooffset+length.- Parameters:
buf- the byte array to stream over.offset- the initial position inbufto start streaming from.length- the number of bytes available for streaming.
-
-
Method Details
-
available
public int available()Returns the number of remaining bytes.- Overrides:
availablein classInputStream- Returns:
count - pos
-
close
Closes this stream and frees resources associated with this stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if an I/O error occurs while closing this stream.
-
mark
public void mark(int readlimit)Sets a mark position in this ByteArrayInputStream. The parameterreadlimitis ignored. Sendingreset()will reposition the stream back to the marked position.- Overrides:
markin classInputStream- Parameters:
readlimit- ignored.- See Also:
markSupported(),reset()
-
markSupported
public boolean markSupported()Indicates whether this stream supports themark()andreset()methods. Returnstruesince this class supports these methods.- Overrides:
markSupportedin classInputStream- Returns:
- always
true. - See Also:
mark(int),reset()
-
read
public int read()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.- Specified by:
readin classInputStream- Returns:
- the byte read or -1 if the end of this stream has been reached.
-
read
public int read(byte[] buffer, int byteOffset, int byteCount)Description copied from class:InputStreamReads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.- Overrides:
readin classInputStream
-
reset
public void reset()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.- Overrides:
resetin classInputStream- See Also:
mark(int)
-
skip
public long skip(long byteCount)SkipsbyteCountbytes in this InputStream. Subsequent calls toreadwill not return these bytes unlessresetis used. This implementation skipsbyteCountnumber of bytes in the target stream. It does nothing and returns 0 ifbyteCountis negative.- Overrides:
skipin classInputStream- Parameters:
byteCount- the number of bytes to skip.- Returns:
- the number of bytes actually skipped.
-