下面代码的:
import java.io.*;public class BufferedInputStreamDemo{
public static void main(String[] args)throws IOException{
String s = "This is a © copyright symbol but this is © 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 © not.如果我改变(32)方法中的值为1,(1),但是输出还是:
true
This is a (c) copyright symbol but this is © not.请问为什么会这样?
按我的理解输入流重定位后应该,只记下一个字符啊。
import java.io.*;public class BufferedInputStreamDemo{
public static void main(String[] args)throws IOException{
String s = "This is a © copyright symbol but this is © 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 © not.如果我改变(32)方法中的值为1,(1),但是输出还是:
true
This is a (c) copyright symbol but this is © not.请问为什么会这样?
按我的理解输入流重定位后应该,只记下一个字符啊。
你的代码不会异常,因此仍然有效(2)doc: if (pos-pos)>limit, then the may be dropped by setting pos to -1
=======(3)由于BufferedInputStream设置后,会使用内部的一个缓冲保存数据(大小初始为2k), 只有这2k空间用完后才会检查是否失效. 由于你的字符串就是几十个字符, 所以(1),(32)是一样的,reset时都有效. 具体你可以看看源码的fill()
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.
你感兴趣可以仔细看看源代码,特别是fill函数
最后想问一个问题:()方法怎样会变得无效?