[英]Is the method read(byte b[], int off, int len) in InputStream same as the method read(byte b[], int off, int len) in FileInputStream?
[英]JAVA: Byte array allocation in InputStream.read(byte[] b, int off, int len)
我有一个大文件,希望从该文件中将字节分成20,000,000字节的块。
我写了这样的代码:
File clipFile = new File("/home/adam/Temp/TheClip.mp4");
InputStream clipIStream = new FileInputStream(clipFile);
long chunkSize = 20000000;
long fileSize = clipFile.length();
long totalParts = (long) Math.ceil(fileSize / chunkSize);
for(int part=0; part < totalParts; part++) {
long startOffset = chunkSize * part;
byte[] bytes = new byte[(int)chunkSize];
clipIStream.read(bytes, (int) startOffset, (int) chunkSize));
// Code for processing the bytes array
// ...
}
程序在第一次迭代后崩溃,生成IndexOutOfBoundsException
。
public int read(byte [] b,int off,int len)抛出IOException
(......)
读取的第一个字节存储在元素b [off]中,下一个字节存储在b [off + 1]中,依此类推。
这意味着,在第二次迭代中, read
开始在位置byte [20000000]上写入,而不是我想要的bytes [0]。
是否有任何方法可以在每次迭代时在字节数组的开头写入字节?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.