This commit is contained in:
huanghongxun
2019-07-28 20:16:12 +08:00
parent 2b3e8ebfc0
commit e862a4b46b
2 changed files with 10 additions and 1 deletions

View File

@@ -263,7 +263,7 @@ public class CacheRepository {
try (RandomAccessFile file = new RandomAccessFile(indexFile.toFile(), "rw"); FileChannel channel = file.getChannel()) {
FileLock lock = channel.lock();
try {
ETagIndex indexOnDisk = JsonUtils.GSON.fromJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), ETagIndex.class);
ETagIndex indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), ETagIndex.class);
Map<String, ETagItem> newIndex = joinETagIndexes(indexOnDisk == null ? null : indexOnDisk.eTag, index.values());
channel.truncate(0);
OutputStream os = Channels.newOutputStream(channel);

View File

@@ -24,6 +24,7 @@ import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
/**
* @author yushijinhun
@@ -49,4 +50,12 @@ public final class JsonUtils {
throw new JsonParseException("Json object cannot be null.");
return parsed;
}
public static <T> T fromMaybeMalformedJson(String json, Class<T> classOfT) throws JsonParseException {
try {
return GSON.fromJson(json, classOfT);
} catch (JsonSyntaxException e) {
return null;
}
}
}