下面代码的:
import java.io.*;public class BufferedInputStreamDemo{
public static void main(String[] args)throws IOException{
String s = "This is a © copyright symbol but this is &copy not.\n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
BufferedInputStream f = new BufferedInputStream(in);
System.out.println(f.Supported());
int c;
boolean ed = false;

while((c =  f.read()) != -1){
switch(c){
case '&':
if(!ed){
f.(32);
ed = true;
}else{
ed = false;
}
break;
case ';':
if(ed){
ed = false;
System.out.print("(c)");
}else{
System.out.print((char)c);
}
break;
case ' ':
if(ed){
ed = false;
f.reset();
System.out.print("&");
}else{
System.out.print((char)c);
}
break;
default:
if(!ed){
System.out.print((char)c);
}
break;
}
}
}
}输出为:
true
This is a (c) copyright symbol but this is &copy not.如果我改变(32)方法中的值为1,(1),但是输出还是:
true
This is a (c) copyright symbol but this is &copy not.请问为什么会这样?
按我的理解输入流重定位后应该,只记下一个字符啊。

解决方案 »

  1.   

    (1)如果失效,reset会throw new IOException("Resetting to invalid ");
       你的代码不会异常,因此仍然有效(2)doc: if (pos-pos)>limit, then the  may be dropped by setting pos to -1
                                                      =======(3)由于BufferedInputStream设置后,会使用内部的一个缓冲保存数据(大小初始为2k), 只有这2k空间用完后才会检查是否失效. 由于你的字符串就是几十个字符, 所以(1),(32)是一样的,reset时都有效. 具体你可以看看源码的fill()   
      

  2.   

    对(2)pos = -1,意味着什么呢?如果我把()的参数设为1,毫无疑问,(pos-pos)>limit,将成立。对(3),如果真是这样,那么()方法中的值设置太小就没什么区别了。但java文档好象不是这么说的啊。
    The readlimit arguments tells this input stream to allow that many bytes to be read before the  position gets invalidated. The general contract of  is that, if the method Supported returns true, the stream somehow remembers all the bytes read after the call to  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. 
      

  3.   

    (2) pos=-1就是失效.  你的(pos-pos)>limit是成立的,但不超过2048的时候根本不去检查这个条件. 注意是"may be"而不是"must be"(3)文档当然只能按最通用的来说了. 另外,support 的流是应该实现这个要求. 
       你感兴趣可以仔细看看源代码,特别是fill函数
      

  4.   

    谢谢kingfish的指点。
    最后想问一个问题:()方法怎样会变得无效?
      

  5.   

    pos<0就是失效了. 但它是protected, 没提供外部修改的方法, 内部自己控制的