java - InputStream returns -1 before end of file -
i'm streaming file server , streaming out socket (basically stream proxy). being used buffer song. once buffer full, no more data read socket until buffer empties out. however, once more data read, inputstream read x more bytes, return -1 signaling end of file, when not. there timeout happening?
here's example code:
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++; }
an example of log like:
0: buffer:b@45030a50 readbytes: 16164 total: 16164 1: buffer:b@45030a50 readbytes: 16384 total: 32548 ... 100: buffer:b@45030a50 readbytes: 16384 total: 1654564 <a long pause here> 100: buffer:b@45030a50 readbytes: 16384 total: 1670948 100: buffer:b@45030a50 readbytes: 16384 total: 1687332 -1 received
any or suggestions appreciate
read() returns -1 @ end of stream , not before. if getting -1 earlier expected, data shorter expected, or server closing end earlier expected.
Comments
Post a Comment