如果我没有关闭任何流,PipedInputStream.read()
如何返回-1? closedByReader
为假,closedByWriter
为假,connected
为真,但返回-1;为什么它认为我完成了? read()
应该返回负数时的条件是什么?PipedInputStream.read()返回-1
编辑1
这里是库函数
public synchronized int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
/* possibly wait on the first character */
int c = read();
if (c < 0) {
return -1;
}
b[off] = (byte) c;
int rlen = 1;
while ((in >= 0) && (len > 1)) {
int available;
if (in > out) {
available = Math.min((buffer.length - out), (in - out));
} else {
available = buffer.length - out;
}
// A byte is read beforehand outside the loop
if (available > (len - 1)) {
available = len - 1;
}
System.arraycopy(buffer, out, b, off + rlen, available);
out += available;
rlen += available;
len -= available;
if (out >= buffer.length) {
out = 0;
}
if (in == out) {
/* now empty */
in = -1;
}
}
return rlen;
}
我无法跟踪变量值还没有的代码,但我可以看到它怎么行跳跃Eclipse调试器排队。所以我看到它在最后一行退出。只有一个while
循环执行。由于之前我们有rlen=1
,因此它可以改变的唯一原因是在rlen += available
一行。这个变量shuld等于-2
。我看到它最后一次访问available = len - 1
。所以,len
应-1
,但通过2048
...
EDIT 2
当然,错误是我的,是绝对在其他地方。其实PipedInputStream
被包装成AudioInputStream
,这是错误地配置为固定大小。达到这个尺寸会导致流程结束。
在这种情况下,'read()'应该阻塞(等待写者写东西)。 – 2012-04-05 20:18:59
如何将'EOF'发送到二进制流? – 2012-04-05 20:21:06
你说得对,你描述的情况似乎是不可能的。你确定你没有误解任何东西吗?也许如果你显示一些代码,这有人可以帮助更多。 – trutheality 2012-04-05 20:50:39