Class: java.io.InputStream
- public abstract class InputStream
- implements Closeable
Applications that need to define a subclass of InputStream
must always provide a method that returns the next byte of input.
Methods
-
InputStreamtop
public InputStream() -
availabletop
public int available() throws IOExceptionReturns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
A subclass' implementation of this method may choose to throw an java.io.IOException if this input stream has been closed by invoking the java.io.InputStream.close() method.
The available method for class InputStream always returns 0.
This method should be overridden by subclasses.
-
closetop
public void close() throws IOExceptionCloses this input stream and releases any system resources associated with the stream.The
closemethod ofInputStreamdoes nothing. -
marktop
public synchronized void mark(int readlimit)Marks the current position in this input stream. A subsequent call to theresetmethod repositions this stream at the last marked position so that subsequent reads re-read the same bytes.The
readlimitarguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.The general contract of
markis that, if the methodmarkSupportedreturnstrue, the stream somehow remembers all the bytes read after the call tomarkand stands ready to supply those same bytes again if and whenever the methodresetis called. However, the stream is not required to remember any data at all if more thanreadlimitbytes are read from the stream beforeresetis called.Marking a closed stream should not have any effect on the stream.
The
markmethod ofInputStreamdoes nothing. -
markSupportedtop
public boolean markSupported()Tests if this input stream supports themarkandresetmethods. Whether or notmarkandresetare supported is an invariant property of a particular input stream instance. ThemarkSupportedmethod ofInputStreamreturnsfalse. -
readtop
public abstract int read() throws IOExceptionReads the next byte of data from the input stream. The value byte is returned as anintin the range0to255. If no byte is available because the end of the stream has been reached, the value-1is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.A subclass must provide an implementation of this method.
-
readtop
public int read(byte[] b) throws IOExceptionReads some number of bytes from the input stream and stores them into the buffer arrayb. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.If the length of
bis zero, then no bytes are read and0is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value-1is returned; otherwise, at least one byte is read and stored intob.The first byte read is stored into element
b[0], the next one intob[1], and so on. The number of bytes read is, at most, equal to the length ofb. Let k be the number of bytes actually read; these bytes will be stored in elementsb[0]throughb[k-1], leaving elementsb[k]throughb[b.length-1]unaffected.The
read(b)method for classInputStreamhas the same effect as:read(b, 0, b.length) -
readtop
public int read(byte[] b, int off, int len) throws IOExceptionReads up tolenbytes of data from the input stream into an array of bytes. An attempt is made to read as many aslenbytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.This method blocks until input data is available, end of file is detected, or an exception is thrown.
If
lenis zero, then no bytes are read and0is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value-1is returned; otherwise, at least one byte is read and stored intob.The first byte read is stored into element
b[off], the next one intob[off+1], and so on. The number of bytes read is, at most, equal tolen. Let k be the number of bytes actually read; these bytes will be stored in elementsb[off]throughb[off+k-1], leaving elementsb[off+k]throughb[off+len-1]unaffected.In every case, elements
b[0]throughb[off]and elementsb[off+len]throughb[b.length-1]are unaffected.The
read(b,off,len)method for classInputStreamsimply calls the methodread()repeatedly. If the first such call results in anIOException, that exception is returned from the call to theread(b,off,len)method. If any subsequent call toread()results in aIOException, the exception is caught and treated as if it were end of file; the bytes read up to that point are stored intoband the number of bytes read before the exception occurred is returned. The default implementation of this method blocks until the requested amount of input datalenhas been read, end of file is detected, or an exception is thrown. Subclasses are encouraged to provide a more efficient implementation of this method. -
resettop
public synchronized void reset() throws IOExceptionRepositions this stream to the position at the time themarkmethod was last called on this input stream.The general contract of
resetis:- If the method
markSupportedreturnstrue, then:- If the method
markhas not been called since the stream was created, or the number of bytes read from the stream sincemarkwas last called is larger than the argument tomarkat that last call, then anIOExceptionmight be thrown. - If such an
IOExceptionis not thrown, then the stream is reset to a state such that all the bytes read since the most recent call tomark(or since the start of the file, ifmarkhas not been called) will be resupplied to subsequent callers of thereadmethod, followed by any bytes that otherwise would have been the next input data as of the time of the call toreset.
- If the method
- If the method
markSupportedreturnsfalse, then:- The call to
resetmay throw anIOException. - If an
IOExceptionis not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of thereadmethod depend on the particular type of the input stream.
- The call to
The method
resetfor classInputStreamdoes nothing except throw anIOException. - If the method
-
skiptop
public long skip(long n) throws IOExceptionSkips over and discardsnbytes of data from this input stream. Theskipmethod may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly0. This may result from any of a number of conditions; reaching end of file beforenbytes have been skipped is only one possibility. The actual number of bytes skipped is returned. Ifnis negative, no bytes are skipped.The
skipmethod of this class creates a byte array and then repeatedly reads into it untilnbytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method. For instance, the implementation may depend on the ability to seek.
