com.dounine.clouddisk360.parser.JSONBianary Maven / Gradle / Ivy
package com.dounine.clouddisk360.parser;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.JSONWriter;
import com.dounine.clouddisk360.exception.CloudDiskException;
import com.dounine.clouddisk360.parser.deserializer.login.LoginUserToken;
import com.dounine.clouddisk360.store.BasePathCommon;
public class JSONBianary {
private static final Logger LOGGER = LoggerFactory.getLogger(JSONBianary.class);
private String bianaryFilename;
private LoginUserToken loginUserToken;
public void writeObjToDisk(T obj) {
if (StringUtils.isNotBlank(bianaryFilename)) {
File file = new File(BasePathCommon.basePath + loginUserToken.getAccount() + "/" + bianaryFilename);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
JSONWriter jsonWriter = null;
try {
jsonWriter = new JSONWriter(new FileWriter(file));
jsonWriter.startObject();
jsonWriter.writeObject(obj);
jsonWriter.endObject();
jsonWriter.flush();
LOGGER.info("文件 { " + file.getName() + " } 写入位置" + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
jsonWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public T readObjForDisk(Class clazz) {
if (StringUtils.isNotBlank(bianaryFilename)) {
File file = new File(BasePathCommon.basePath + loginUserToken.getAccount() + "/" + bianaryFilename);
if (!file.exists()) {
throw new CloudDiskException("读取的文件:[ " + file.getAbsolutePath() + " ]不存在");
}
JSONReader jsonReader = null;
T t = null;
try {
jsonReader = new JSONReader(new FileReader(file));
jsonReader.startObject();
t = jsonReader.readObject(clazz);
jsonReader.endObject();
} catch (IOException e) {
e.printStackTrace();
} finally {
jsonReader.close();
}
return t;
}
throw new CloudDiskException("读取的对象文件位置与文件名不能为空");
}
public void writeListToDisk(List lists) {
if (StringUtils.isNotBlank(bianaryFilename)) {
File file = new File(BasePathCommon.basePath + loginUserToken.getAccount() + "/" + bianaryFilename);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
JSONWriter jsonWriter = null;
try {
jsonWriter = new JSONWriter(new FileWriter(file));
jsonWriter.startArray();
lists.forEach(jsonWriter::writeObject);
jsonWriter.endArray();
jsonWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
jsonWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public List readListForDisk(Class clzz) {
List lists = new ArrayList<>(0);
if (StringUtils.isNotBlank(bianaryFilename)) {
File file = new File(BasePathCommon.basePath + loginUserToken.getAccount() + "/" + bianaryFilename);
if (!file.exists()) {
throw new CloudDiskException("读取的文件:[ " + file.getAbsolutePath() + " ]不存在");
}
JSONReader jsonReader = null;
try {
jsonReader = new JSONReader(new FileReader(file));
jsonReader.startArray();
while (jsonReader.hasNext()) {
lists.add(jsonReader.readObject(clzz));
}
jsonReader.endArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
jsonReader.close();
}
return lists;
}
throw new CloudDiskException("读取的对象集合文件位置与文件名不能为空");
}
public void setBianaryFilename(String bianaryFilename) {
this.bianaryFilename = bianaryFilename;
}
public LoginUserToken getLoginUserToken() {
return loginUserToken;
}
public void setLoginUserToken(LoginUserToken loginUserToken) {
this.loginUserToken = loginUserToken;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy