seek就是查询的意思,可以根据文件中的字节进行查询数据(不知道这么说对不对)
下面这个函数中
private void setThreadBreakpoint(
File file,
File tempFile,
long contentLength,
long[] startPos,
long[] endPos) {
RandomAccessFile tempFileFos = null;
try {
if (file.exists()) {
System.out.println("file " + fileName + " has exists!");
long localFileSize = file.length();
// 下载的目标文件已存在,判断目标文件是否完整
if (localFileSize < contentLength) {
System.out.println("Now download continue ... ");
tempFileFos = new RandomAccessFile(tempFile, "rw");
// 遍历目标文件的所有临时文件,设置断点的位置,即每个临时文件的长度
for (int i = 0; i < threadNum; i++) {
tempFileFos.seek(4 + 24 * i + 8);
endPos[i] = tempFileFos.readLong();
tempFileFos.seek(4 + 24 * i + 16);
startPos[i] = tempFileFos.readLong();
}
} else {
System.out.println("This file has download complete!");
}
} else {
// 如果下载的目标文件不存在,则创建新文件
file.createNewFile();
tempFile.createNewFile();
tempFileFos = new RandomAccessFile(tempFile, "rw");
tempFileFos.writeInt(threadNum);
for (int i = 0; i < threadNum; i++) {
// 创建子线程来负责下载数据,每段数据的起始位置为(threadLength * i)
startPos[i] = threadLength * i;
tempFileFos.writeLong(startPos[i]);
/*
* 设置子线程的终止位置,非最后一个线程即为(threadLength * (i + 1) - 1)
* 最后一个线程的终止位置即为下载内容的长度
*/
if (i == threadNum - 1) {
endPos[i] = contentLength;
} else {
endPos[i] = threadLength * (i + 1) - 1;
}
// end position
tempFileFos.writeLong(endPos[i]);
// current position
tempFileFos.writeLong(startPos[i]);
}
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
tempFileFos.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
这个循环方法体里
// 遍历目标文件的所有临时文件,设置断点的位置,即每个临时文件的长度
for (int i = 0; i < threadNum; i++) {
tempFileFos.seek(4 + 24 * i + 8);
endPos[i] = tempFileFos.readLong();
tempFileFos.seek(4 + 24 * i + 16);
startPos[i] = tempFileFos.readLong();
}
这句话
tempFileFos.seek(4 + 24 * i + 8);
这里面seek的参数是根据什么确定的?