Java PushbackInputStream:推回缓冲区已满
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19244142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PushbackInputStream: Push back buffer is full
提问by alien01
Why I am getting the following exception:
为什么我收到以下异常:
Exception in thread "main" java.io.IOException: Push back buffer is full
at java.io.PushbackInputStream.unread(PushbackInputStream.java:232)
at java.io.PushbackInputStream.unread(PushbackInputStream.java:252)
at org.tests.io.PushBackStream_FUN.read(PushBackStream_FUN.java:32)
at org.tests.io.PushBackStream_FUN.main(PushBackStream_FUN.java:43)
In this code:
在这段代码中:
public class PushBackStream_FUN {
public int write(String outFile) throws Exception {
FileOutputStream outputStream = new FileOutputStream(new File(outFile));
String str = new String("Hello World");
byte[] data = str.getBytes();
outputStream.write(data);
outputStream.close();
return data.length;
}
public void read(String inFile, int ln) throws Exception {
PushbackInputStream inputStream = new PushbackInputStream(new FileInputStream(new File(inFile)));
byte[] data = new byte[ln];
String str;
// read
inputStream.read(data);
str = new String(data);
System.out.println("MSG_0 = "+str);
// unread
inputStream.unread(data);
// read
inputStream.read(data);
str = new String(data);
System.out.println("MSG_1 = "+str);
}
public static void main(final String[] args) throws Exception {
PushBackStream_FUN fun = new PushBackStream_FUN();
String path = "aome/path/output_PushBack_FUN";
int ln = fun.write(path);
fun.read(path, ln);
}
}
UPDATE
更新
Think this is the solution. Java sources to the rescue. I have made some "experiments". It turns out that when I specify PushbackInputStreamwith a specified buffer size it works. The java sources tells me this:
认为这是解决方案。Java 源代码来救援。我做了一些“实验”。事实证明,当我使用PushbackInputStream指定的缓冲区大小指定时,它可以工作。java源告诉我这个:
public PushbackInputStream(InputStream in) {
this(in, 1);
}
public PushbackInputStream(InputStream in, int size) {
super(in);
if (size <= 0) {
throw new IllegalArgumentException("size <= 0");
}
this.buf = new byte[size];
this.pos = size;
}
I think that if you use PushbackInputStreamwith constrcutor that uses default buffer size, you can only unread single byte. I am unreading more than single byte, thus the exception.
我认为,如果您PushbackInputStream与使用默认缓冲区大小的构造函数一起使用,则只能未读取单个字节。我未读的不止一个字节,因此例外。
回答by Adam
By default, PushbackInputStreamonly allocates enough space to be able to unread()for a single character. If you want to be able to push back more than that you must specify the capacity at construction time.
默认情况下,PushbackInputStream只unread()为单个字符分配足够的空间。如果您希望能够推回更多,则必须在构建时指定容量。
In your case it'd look something like this:
在你的情况下,它看起来像这样:
final PushbackInputStream pis = new PushbackInputStream( inputStream, ln );

