int byteRead=0;
int offset=0;
int count=0;
while(offset<contentLength)
{
byteRead=in.read(data,offset,data.length-offset);
if(byteRead==-1)
break;
offset+=byteRead;
}
也许你也发现了,BufferedInputStream.read(byte[],offset,length)方法需要循环读取,下面是文档的解释:
This method implements the general contract of the corresponding read method of the InputStream class. As an additional convenience, it attempts to read as many bytes as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true:
· The specified number of bytes have been read,