|
|
@@ -12,11 +12,21 @@ import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
|
+import java.io.RandomAccessFile;
|
|
|
+import java.io.File;
|
|
|
|
|
|
public class DownTaskHandler {
|
|
|
+ public static final String LOCALE_LOG_ROOT_DIR = "/sdcard/phone/";
|
|
|
+
|
|
|
public int taskId = 0;
|
|
|
int mSum = 0;
|
|
|
Set<Integer> receivedPkts;
|
|
|
+ RandomAccessFile mZipRaFile;
|
|
|
+ public String mZipPath;
|
|
|
+
|
|
|
+ public DownTaskHandler(){
|
|
|
+ mZipRaFile = null;
|
|
|
+ }
|
|
|
|
|
|
public static int genTaskId(){
|
|
|
Date date = new Date();
|
|
|
@@ -36,12 +46,13 @@ public class DownTaskHandler {
|
|
|
return reqLogFileMsg;
|
|
|
}
|
|
|
|
|
|
- public void store(long skip,byte[] content, int pktNums, int pktNo,long fileSize) throws IOException{
|
|
|
+ public int store(long skip,byte[] content,int contentLen, int pktNums, int pktNo,long fileSize) throws IOException{
|
|
|
mSum = pktNums;
|
|
|
//output to local file
|
|
|
-
|
|
|
+ long bytesWritten = restorePartly(fileSize,skip,content,contentLen);
|
|
|
//record received sub pkts;
|
|
|
receivedPkts.add(pktNo);
|
|
|
+ return (int)(bytesWritten*1.0f/fileSize*100);
|
|
|
}
|
|
|
|
|
|
public BaseMsg checkLost(int taskId) throws IOException {
|
|
|
@@ -65,11 +76,47 @@ public class DownTaskHandler {
|
|
|
}
|
|
|
logFileLostMsg.taskId = taskId;
|
|
|
|
|
|
+ //无需补充包时,关闭本地文件
|
|
|
+ if(lostsize == 0){
|
|
|
+ if(mZipRaFile == null) {
|
|
|
+ mZipRaFile.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
lostPkts.clear();
|
|
|
|
|
|
return logFileLostMsg;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param fileSize 为本地文件设定大小
|
|
|
+ * @param offset 每个子包写入的起始位置
|
|
|
+ * @param data 子包数据
|
|
|
+ * @param dataLen 子包长度
|
|
|
+ */
|
|
|
+ private long restorePartly(long fileSize,long offset,byte[] data,long dataLen){
|
|
|
+ long bytesWritten = 0;
|
|
|
+ try {
|
|
|
+ if(mZipRaFile == null) {
|
|
|
+ File dir = new File(LOCALE_LOG_ROOT_DIR);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdir();
|
|
|
+ }
|
|
|
+ File zipF = new File(dir.getPath() + "/log_"+timeToFmtStr(taskId)+".zip");
|
|
|
+ mZipPath = zipF.getAbsolutePath();//用于显示文件路径
|
|
|
+ mZipRaFile = new RandomAccessFile(zipF, "rw");
|
|
|
+ mZipRaFile.setLength(fileSize);
|
|
|
+ }
|
|
|
+ mZipRaFile.seek(offset);
|
|
|
+ mZipRaFile.write(data, 0, (int) dataLen);
|
|
|
+ bytesWritten = mZipRaFile.getFilePointer();
|
|
|
+ } catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return bytesWritten;
|
|
|
+ }
|
|
|
+
|
|
|
public static Long getStringTolong(String date, String format) {
|
|
|
try {
|
|
|
DateFormat fmt = new SimpleDateFormat(format);
|
|
|
@@ -81,4 +128,12 @@ public class DownTaskHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static String timeToFmtStr(long time) {
|
|
|
+ Long long1970 = getStringTolong("1970-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss");
|
|
|
+ long cc_ts = time * 1000 + long1970;
|
|
|
+ DateFormat fmt = new SimpleDateFormat("yyyyMMdd_HHmmss");
|
|
|
+ String str = fmt.format(new Date(cc_ts));
|
|
|
+ return str;
|
|
|
+ };
|
|
|
+
|
|
|
}
|