使用InputStream的markSupported、mark和reset

使用InputStream的markSupported、mark和resetjava


mark用於標記地方,之後再調用reset時就能夠再回到這個mark過的地方。ui

mark方法有個整型參數,意思是,在讀出這麼多個字符以前,保持mark有效。this

好比說mark(10),那麼在read()10個之內的字符時,reset()操做後能夠從新讀取已經讀出的數據,.net

若是已經讀取的數據超過10個,那reset()操做後,就不能正確讀取之前的數據了,由於此時mark標記已經失效。 rem


Marks the current position in this input stream.get

A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.input


The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.it

The general contract of mark is that, io

if the method markSupported returns true, ast

the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called.  

However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called.


Marking a closed stream should not have any effect on the stream.


The mark method of InputStream does nothing.


@param   readlimit   the maximum limit of bytes that can be read before the mark position becomes invalid.

@see     java.io.InputStream#reset()


    public synchronized void mark(int readlimit) {}



Repositions this stream to the position at the time the mark method was last called on this input stream.


The general contract of reset is:


If the method markSupported returnstrue, then:


    If the method mark has not been called since the stream was created, or the number of bytes read from the stream

    since mark was last called is larger than the argument to mark at that last call, then an IOException might be thrown.


    If such an IOException is not thrown, then the stream is reset to a state such that all the bytes read since the

    most recent call to mark (or since the start of the file, if mark has not been called) will be resupplied

    to subsequent callers of the read method, followed by any bytes that otherwise would have been the next input data as of

    the time of the call to reset.


If the method markSupported returns false, then:


    The call to reset may throw an IOException.


    If an IOException is 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 the read method depend on the

    particular type of the input stream.


The method reset for class InputStream does nothing except throw an IOException.

@exception  IOException  if this stream has not been marked or if the mark has been invalidated.

@see     java.io.InputStream#mark(int)

@see     java.io.IOException

    public synchronized void reset() throws IOException {

        throw new IOException("mark/reset not supported");

    }



Tests if this input stream supports the mark and reset methods. 

Whether or not mark and reset are supported is an invariant property of a particular input stream instance. 

The markSupported methodof InputStream returns false.


@return  true if this stream instance supports the mark and reset methods; false otherwise.

@see     java.io.InputStream#mark(int)

@see     java.io.InputStream#reset()

    public boolean markSupported() {

        return false;

    }

相關文章
相關標籤/搜索