使用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;

}

时间: 2024-10-14 18:48:00

使用InputStream的markSupported、mark和reset的相关文章

InputStream中通过mark和reset方法重复利用缓存

通过缓存InputStream可重复利用一个InputStream,但是要缓存一整个InputStream内存压力可能是比较大的.如果第一次读取InputStream是用来判断文件流类型,文件编码等用的,往往不需要所有的InputStream的数据,或许只需要前n个字节,这样一来,缓存一整个InputStream实际上也是一种浪费. 其实InputStream本身提供了三个接口: 第一个,InputStream是否支持mark,默认不支持. Java代码   public boolean mar

JAVA中mark()和reset()用法

根据JAVA官方文档的描述,mark(int readlimit)方法表示,标记当前位置,并保证在mark以后最多可以读取readlimit字节数据,mark标记仍有效.如果在mark后读取超过readlimit字节数据,mark标记就会失效,调用reset()方法会有异常. 但实际的运行情况却和JAVA文档中的描述并不完全相符. 有时候在BufferedInputStream类中调用mark(int readlimit)方法后,即使读取超过readlimit字节的数据,mark标记仍有效,仍然

java之十 高级IO流

java.io定义的输入/输出类列于下表: ObjectInputStream.GetField和ObjectOutputStream.PutField是Java2新添的内部类. java.io包还包含两个不受java2欢迎的类,这两个类没有在上表中列出:LineNumberInputStream和StringBufferInputStream.新代码不应该使用两个类. 下面是由java.io定义的接口: FileFilter接口是Java2新增的. java.io包中有很多类和接口.包括字节和

Netty in Action (十三) 第五章节 第二部分 ByteBuf字节层面的操作

5.3 Byte-level operations ByteBuf除了提供基本对数据读写操作之外,它还提供了很多其他的方法,在接下来的这个小节中,我们将讨论这些方法中比较重要的来分析讲解一下 5.3.1 Random access indexing 与正常的java的字节数组一样,ByteBuf的索引下标也是从0开始的,第一个索引下表是0,最后一个字节索引总是它的容量-1,下面的代码清单向你展示了ByteBuf封装了它的存储机制,可以使我们很方便地去迭代ByteBuf中的内容 注意到如果想获取B

netty io.netty.buffer简介

io.netty.util.ReferenceCounted 此接口代表一个引用计数的对象,此对象需要显示的释放. 当一个ReferenceCounted对象被实例化的时候,该对象的引用数量就是1,调用retain()方法会增加引用数量,调用 release() 方法会减少引用数量,如果引用数量减少到0,该对象就需要显示释放掉.访问释放掉的对象通常会导致访问冲突. 如果实现ReferenceCounted接口的对象是一个包含同样实现ReferenceCounted接口的对象的容器.当容器的引用数

Java流家族之InputStream、OutputStream

Java流家族之InputStream 实现的接口: Closeable , AutoCloseable 已知直接子类: AudioInputStream , ByteArrayInputStream , FileInputStream , FilterInputStream , InputStream , ObjectInputStream PipedInputStream , SequenceInputStream , StringBufferInputStream 具体的方法: Modif

java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)

我们以ByteArrayInputStream,拉开对字节类型的“输入流”的学习序幕.本章,我们会先对ByteArrayInputStream进行介绍,然后深入了解一下它的源码,最后通过示例来掌握它的用法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_02.html ByteArrayInputStream 介绍 ByteArrayInputStream 是字节数组输入流.它继承于InputStream.它包含一个内部缓冲区,该缓冲区包含从流

api-gateway实践(19)InputStream的复用

相关链接: inputstream复制:http://www.cnblogs.com/happyaday/p/4616023.html 对象克隆:https://zhidao.baidu.com/question/181598914089683044.html 一.问题提出 在进行网关引擎开发时,获取到一个http请求的inputstream后,可能要多次利用它进行read操作.由于流读过一次就不能再读了,所以需要实现InputStream的复制. 而InputStream对象本身不能复制,因为

InputStream源码分析

InputStream是一个抽象类,属于字节流的读取. 1 /** 2 * 该抽象类是所有字节输入流的超类. 3 */ 4 public abstract class InputStream implements Closeable { 5 6 // 该变量用于确定在skip方法中使用的最大缓存数组大小. 7 private static final int MAX_SKIP_BUFFER_SIZE = 2048; 8 9 /** 10 * 从输入流中读取下一字节数据.返回的字节值为一个范围在0