如何使用网络填充文件下载中的缓冲区 [android]
How to Fill the Buffer in File Download Using the Network [android]
我正在下载文件时对字节数组进行加密。但是由于使用了网络,即使缓冲区数组的大小固定为4096,数据也是动态填充的。在重新解密文件的时候,我们并没有使用网络,所以不断的有4096字节进来。 加密解密这方面出现问题。
我想知道的是下载时进入buffer的字节大小怎么总是能填满4096
谢谢。
显示您下载文件的代码。
实现它的一种方法是,在从网络读取数据时循环,并不断将接收到的字节推送到缓冲区,直到缓冲区已满(例如,您的情况为 4096),然后处理并清空缓冲区。
当您收到更多可以推送到缓冲区的数据时,您可能还必须处理这些情况...
我的两分钱...
What I want to know is how the size of the bytes coming into the buffer at the time of downloading can always be filled with 4096.
不能。 TCP API 可以免费为您提供从 1 字节到缓冲区大小的任何内容。
您需要修复加解密代码中的错误。 XY问题。
这是我的解决方案。
您可以根据需要获得缓冲区大小。需要时应用它。
private boolean readFully(InputStream is, byte[] data, int off, int length) {
int read;
while (length > 0) {
read = is.read(data, off, length);
if(read <=0){
return false;
}
length -= read;
off += read;
}
return true;
}
我正在下载文件时对字节数组进行加密。但是由于使用了网络,即使缓冲区数组的大小固定为4096,数据也是动态填充的。在重新解密文件的时候,我们并没有使用网络,所以不断的有4096字节进来。 加密解密这方面出现问题。
我想知道的是下载时进入buffer的字节大小怎么总是能填满4096
谢谢。
显示您下载文件的代码。
实现它的一种方法是,在从网络读取数据时循环,并不断将接收到的字节推送到缓冲区,直到缓冲区已满(例如,您的情况为 4096),然后处理并清空缓冲区。
当您收到更多可以推送到缓冲区的数据时,您可能还必须处理这些情况...
我的两分钱...
What I want to know is how the size of the bytes coming into the buffer at the time of downloading can always be filled with 4096.
不能。 TCP API 可以免费为您提供从 1 字节到缓冲区大小的任何内容。
您需要修复加解密代码中的错误。 XY问题。
这是我的解决方案。 您可以根据需要获得缓冲区大小。需要时应用它。
private boolean readFully(InputStream is, byte[] data, int off, int length) {
int read;
while (length > 0) {
read = is.read(data, off, length);
if(read <=0){
return false;
}
length -= read;
off += read;
}
return true;
}