|
@@ -1,16 +1,17 @@
|
|
|
package com.xt.dsp.serviceImpl;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.FileReader;
|
|
|
-import java.io.FileWriter;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.ObjectInputStream;
|
|
|
+import java.io.ObjectOutputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.FileCopyUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.xt.dsp.bean.TaskCacheBean;
|
|
@@ -20,21 +21,6 @@ import com.xt.dsp.service.CacheBeanService;
|
|
|
public class CacheBeanServiceImpl implements CacheBeanService {
|
|
|
private final Logger log = LoggerFactory.getLogger(CacheBeanServiceImpl.class);
|
|
|
|
|
|
- private String getTaskCacheBeanData(TaskCacheBean taskCacheBean) {
|
|
|
- Object data = taskCacheBean.getCacheData();
|
|
|
- if (data == null) {
|
|
|
- return "";
|
|
|
- }
|
|
|
- if (data instanceof String) {
|
|
|
- return (String) data;
|
|
|
- }
|
|
|
- if (data instanceof JSONObject) {
|
|
|
- JSONObject dataJson = (JSONObject) data;
|
|
|
- return dataJson.toJSONString();
|
|
|
- }
|
|
|
- return String.valueOf(data);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public List<TaskCacheBean> readCache(String cacheFolder) {
|
|
|
File folder = new File(cacheFolder);
|
|
@@ -45,13 +31,21 @@ public class CacheBeanServiceImpl implements CacheBeanService {
|
|
|
for (File f : folder.listFiles()) {
|
|
|
TaskCacheBean tc = new TaskCacheBean();
|
|
|
tc.setFileName(f.getName());
|
|
|
+ ObjectInputStream in = null;
|
|
|
try {
|
|
|
- String data = FileCopyUtils.copyToString(new FileReader(f));
|
|
|
- tc.setCacheData(data);
|
|
|
+ in = new ObjectInputStream(new FileInputStream(f));
|
|
|
+ tc.setCacheData((JSONObject) in.readObject());
|
|
|
taskCacheBeanList.add(tc);
|
|
|
- f.delete();
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("file read error:{}", e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (null != in) {
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ f.delete();
|
|
|
}
|
|
|
}
|
|
|
return taskCacheBeanList;
|
|
@@ -72,10 +66,21 @@ public class CacheBeanServiceImpl implements CacheBeanService {
|
|
|
if (count > 0) {
|
|
|
f = new File(folder, fileName + "-" + count);
|
|
|
}
|
|
|
+ ObjectOutputStream out = null;
|
|
|
try {
|
|
|
- FileCopyUtils.copy(getTaskCacheBeanData(taskCacheBean), new FileWriter(f));
|
|
|
+ out = new ObjectOutputStream(new FileOutputStream(f));
|
|
|
+ out.writeObject(taskCacheBean.getCacheData());
|
|
|
+ out.flush();
|
|
|
} catch (IOException e) {
|
|
|
log.error("缓存写入失败:{}", e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (null != out) {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return 0;
|
|
|
}
|