Java:输入流标记限制 [英] Java: Inputstream mark limit

查看:100
本文介绍了Java:输入流标记限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Java文档,类InputStream服务器中mark方法的 readlimit 参数,用于set 在标记位置变为无效之前可以读取的最大字节数。
我有一个名为 sample.txt 的文件,其内容为hello。我写了这段代码:

According to Java documentation, the readlimit parameter of the mark method in the Class InputStream server for set "the maximum limit of bytes that can be read before the mark position becomes invalid.". I have a file named sample.txt whose content is "hello". And i wrote this code:

import java.io.*;
public class InputStream{
 public static void main (String[] args) throws IOException {
  InputStream reader = new FileInputStream("sample.txt");
  BufferedInputStream bis = new BufferedInputStream(reader);
  bis.mark(1);
  bis.read();
  bis.read();
  bis.read();
  bis.read();
  bis.reset();
  System.out.println((char)bis.read());
 }
}

输出为h。但是,如果我在标记方法之后读取多个字节,那么我是否应该为无效的重置方法调用收到错误?

The output is "h". But if i after the mark method read more than one bytes, shouldn't i get an error for the invalid reset method call?

推荐答案

我会将此归结为文档错误。

I would put this down to documentation error.

<$ c $的非参数文档c> BufferedInputStream 是查看InputStream的mark方法的常规合约,这对我来说表明 BufferedInputStream 的行为不同,参数doc尽管如此。

The non-parameter doc for BufferedInputStream is "See the general contract of the mark method of InputStream," which to me indicates that BufferedInputStream does not behave differently, parameter doc notwithstanding.

InputStream 指定的一般合约是


readlimit参数告诉此输入流允许在标记位置无效之前读取多个字节[...]流不需要记住任何数据如果从流中读取超过readlimit字节

The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated [...] the stream is not required to remember any data at all if more than readlimit bytes are read from the stream

换句话说, readlimit 是一个建议;流可以免费承诺和过度交付。

In other words, readlimit is a suggestion; the stream is free to under-promise and over-deliver.

这篇关于Java:输入流标记限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆