使用InputStream的markSupported、mark和reset

两盒软妹~` 提交于 2020-02-28 14:28:19

使用InputStream的markSupported、mark和reset


mark用于标记地方,以后再调用reset时就可以再回到这个mark过的地方。

mark方法有个整型参数,意思是,在读出这么多个字符之前,保持mark有效。

比如说mark(10),那么在read()10个以内的字符时,reset()操作后可以重新读取已经读出的数据,

如果已经读取的数据超过10个,那reset()操作后,就不能正确读取以前的数据了,因为此时mark标记已经失效。 


Marks the current position in this input stream.

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


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

The general contract of mark is that, 

if the method markSupported returns true, 

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;

    }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!