我正在从服务器流式传输文件,然后将其流式传输回套接字(基本上是流代理)。这被用来缓冲歌曲。所以一旦缓冲区已满,在缓冲区清空之前不会再有数据从套接字读取。但是,再次读取数据时,InputStream将读取X个字节,然后突然返回-1表示文件结束,否则表示文件结束。是否有超时发生?InputStream在文件结束前返回-1
下面是一些示例代码:
InputStream data = realResponse.getEntity().getContent();
...some code...
int totalBytes = 0;
int count = 0;
// Start streaming content.
byte[] buff = new byte[1024 * 50];
while (isRunning && (readBytes = data.read(buff, 0, buff.length)) != -1) {
totalBytes += readBytes;
Log(count + ": buffer:" + buff + " readBytes:" + readBytes + " total:" + totalBytes);
client.getOutputStream().write(buff, 0, readBytes);
Log("Finished write");
count++;
}
日志的一个例子是这样的:
0: buffer:[email protected] readBytes: 16164 total: 16164
1: buffer:[email protected] readBytes: 16384 total: 32548
...
100: buffer:[email protected] readBytes: 16384 total: 1654564
<a long pause here>
100: buffer:[email protected] readBytes: 16384 total: 1670948
100: buffer:[email protected] readBytes: 16384 total: 1687332
-1 Received
任何帮助或建议,欣赏
上面的日志似乎不符合代码。日志中没有“完成写入”消息,并且没有记录“-1已接收”的代码。在'totalBytes'范围内定义了'readBytes'? 'count'在哪里设置? 'count'和'readBytes'之间有关系吗? – 2011-05-11 02:48:33
为了可读性,我清理了一些日志并取出了“完成的写入”,但忘记将它从代码中取出。 “-1已收到”在未显示的日志语句中。 readBytes,totalBytes和count都在范围内。 count变量只是用于调试已经完成了多少次读/写操作。 – Nick 2011-05-11 14:17:29
那'isRunning'布尔检查怎么样?这是一个多线程应用程序吗?可以设置该标志为false? – amischiefr 2011-05-11 14:31:10