Remove GSON lib
This commit is contained in:
parent
0b29cf086b
commit
b494b2ea39
6 changed files with 39 additions and 76 deletions
|
|
@ -3,8 +3,6 @@ package us.shandian.giga.get;
|
|||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.RandomAccessFile;
|
||||
|
|
@ -156,16 +154,8 @@ public class DownloadManagerImpl implements DownloadManager {
|
|||
|
||||
for (File sub : subs) {
|
||||
if (sub.isFile() && sub.getName().endsWith(".giga")) {
|
||||
String str = Utility.readFromFile(sub.getAbsolutePath());
|
||||
if (str != null && !str.trim().equals("")) {
|
||||
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "loading mission " + sub.getName());
|
||||
Log.d(TAG, str);
|
||||
}
|
||||
|
||||
DownloadMission mis = new Gson().fromJson(str, DownloadMission.class);
|
||||
|
||||
DownloadMission mis = Utility.readFromFile(sub.getAbsolutePath());
|
||||
if (mis != null) {
|
||||
if (mis.finished) {
|
||||
if (!sub.delete()) {
|
||||
Log.w(TAG, "Unable to delete .giga file: " + sub.getPath());
|
||||
|
|
|
|||
|
|
@ -4,9 +4,8 @@ import android.os.Handler;
|
|||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -18,7 +17,9 @@ import us.shandian.giga.util.Utility;
|
|||
|
||||
import static org.schabi.newpipe.BuildConfig.DEBUG;
|
||||
|
||||
public class DownloadMission {
|
||||
public class DownloadMission implements Serializable {
|
||||
private static final long serialVersionUID = 0L;
|
||||
|
||||
private static final String TAG = DownloadMission.class.getSimpleName();
|
||||
|
||||
public interface MissionListener {
|
||||
|
|
@ -79,7 +80,6 @@ public class DownloadMission {
|
|||
private transient boolean mWritingToFile;
|
||||
|
||||
private static final int NO_IDENTIFIER = -1;
|
||||
private long db_identifier = NO_IDENTIFIER;
|
||||
|
||||
public DownloadMission() {
|
||||
}
|
||||
|
|
@ -308,7 +308,7 @@ public class DownloadMission {
|
|||
*/
|
||||
private void doWriteThisToFile() {
|
||||
synchronized (blockState) {
|
||||
Utility.writeToFile(getMetaFilename(), new Gson().toJson(this));
|
||||
Utility.writeToFile(getMetaFilename(), this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import android.content.ClipboardManager;
|
|||
import android.content.Context;
|
||||
import android.support.annotation.ColorRes;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
|
@ -16,6 +17,9 @@ import java.io.FileInputStream;
|
|||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
|
@ -51,57 +55,47 @@ public class Utility {
|
|||
}
|
||||
}
|
||||
|
||||
public static void writeToFile(String fileName, String content) {
|
||||
public static void writeToFile(@NonNull String fileName, @NonNull Serializable serializable) {
|
||||
ObjectOutputStream objectOutputStream = null;
|
||||
|
||||
try {
|
||||
writeToFile(fileName, content.getBytes("UTF-8"));
|
||||
objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName));
|
||||
objectOutputStream.writeObject(serializable);
|
||||
} catch (Exception e) {
|
||||
|
||||
//nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeToFile(String fileName, byte[] content) {
|
||||
File f = new File(fileName);
|
||||
|
||||
if (!f.exists()) {
|
||||
if(objectOutputStream != null) {
|
||||
try {
|
||||
f.createNewFile();
|
||||
objectOutputStream.close();
|
||||
} catch (Exception e) {
|
||||
|
||||
//nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
FileOutputStream opt = new FileOutputStream(f, false);
|
||||
opt.write(content, 0, content.length);
|
||||
opt.close();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static String readFromFile(String file) {
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T readFromFile(String file) {
|
||||
T object = null;
|
||||
ObjectInputStream objectInputStream = null;
|
||||
|
||||
try {
|
||||
File f = new File(file);
|
||||
|
||||
if (!f.exists() || !f.canRead()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BufferedInputStream ipt = new BufferedInputStream(new FileInputStream(f));
|
||||
|
||||
byte[] buf = new byte[512];
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while (ipt.available() > 0) {
|
||||
int len = ipt.read(buf, 0, 512);
|
||||
sb.append(new String(buf, 0, len, "UTF-8"));
|
||||
}
|
||||
|
||||
ipt.close();
|
||||
return sb.toString();
|
||||
objectInputStream = new ObjectInputStream(new FileInputStream(file));
|
||||
object = (T) objectInputStream.readObject();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
if(objectInputStream != null){
|
||||
try {
|
||||
objectInputStream .close();
|
||||
} catch (Exception e) {
|
||||
//nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue