diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DefaultCacheRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DefaultCacheRepository.java index 540764de0..b00a5b736 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DefaultCacheRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/DefaultCacheRepository.java @@ -63,7 +63,7 @@ public class DefaultCacheRepository extends CacheRepository { lock.writeLock().lock(); try { if (Files.isRegularFile(indexFile)) - index = JsonUtils.fromNonNullJson(FileUtils.readText(indexFile.toFile()), Index.class); + index = JsonUtils.fromNonNullJson(FileUtils.readText(indexFile), Index.class); else index = new Index(); } catch (IOException | JsonParseException e) { @@ -180,7 +180,7 @@ public class DefaultCacheRepository extends CacheRepository { hash = DigestUtils.digestToString(SHA1, path); Path cache = getFile(SHA1, hash); - FileUtils.copyFile(path.toFile(), cache.toFile()); + FileUtils.copyFile(path, cache); Lock writeLock = lock.writeLock(); writeLock.lock(); @@ -198,7 +198,7 @@ public class DefaultCacheRepository extends CacheRepository { private void saveIndex() { if (indexFile == null || index == null) return; try { - FileUtils.writeText(indexFile.toFile(), JsonUtils.GSON.toJson(index)); + FileUtils.writeText(indexFile, JsonUtils.GSON.toJson(index)); } catch (IOException e) { Logging.LOG.log(Level.SEVERE, "Unable to save index.json", e); } @@ -217,7 +217,7 @@ public class DefaultCacheRepository extends CacheRepository { * // assets and versions will not be included in index. * } */ - private class Index implements Validation { + private static final class Index implements Validation { private final Set libraries; public Index() { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java index 9b68bc6fe..133ea1f32 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java @@ -27,7 +27,6 @@ import org.jackhuang.hmcl.util.io.IOUtils; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.RandomAccessFile; import java.net.URLConnection; import java.nio.ByteBuffer; import java.nio.channels.Channels; @@ -35,6 +34,7 @@ import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @@ -69,7 +69,7 @@ public class CacheRepository { } if (Files.isRegularFile(indexFile)) { - ETagIndex raw = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile.toFile()), ETagIndex.class); + ETagIndex raw = JsonUtils.GSON.fromJson(FileUtils.readText(indexFile), ETagIndex.class); if (raw == null) index = new HashMap<>(); else @@ -123,12 +123,12 @@ public class CacheRepository { public void tryCacheFile(Path path, String algorithm, String hash) throws IOException { Path cache = getFile(algorithm, hash); if (Files.isRegularFile(cache)) return; - FileUtils.copyFile(path.toFile(), cache.toFile()); + FileUtils.copyFile(path, cache); } public Path cacheFile(Path path, String algorithm, String hash) throws IOException { Path cache = getFile(algorithm, hash); - FileUtils.copyFile(path.toFile(), cache.toFile()); + FileUtils.copyFile(path, cache); return cache; } @@ -222,7 +222,7 @@ public class CacheRepository { cacheData(() -> { String hash = DigestUtils.digestToString(SHA1, bytes); Path cached = getFile(SHA1, hash); - FileUtils.writeBytes(cached.toFile(), bytes); + FileUtils.writeBytes(cached, bytes); return new CacheResult(hash, cached); }, conn); } @@ -287,7 +287,7 @@ public class CacheRepository { } public void saveETagIndex() throws IOException { - try (RandomAccessFile file = new RandomAccessFile(indexFile.toFile(), "rw"); FileChannel channel = file.getChannel()) { + try (FileChannel channel = FileChannel.open(indexFile, StandardOpenOption.READ, StandardOpenOption.WRITE)) { FileLock lock = channel.lock(); try { ETagIndex indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), ETagIndex.class); @@ -371,7 +371,7 @@ public class CacheRepository { /** * Universal cache */ - public static class Storage { + public static final class Storage { private final String name; private Map storage; private final ReadWriteLock lock = new ReentrantReadWriteLock(); @@ -409,7 +409,7 @@ public class CacheRepository { try { indexFile = cacheDirectory.resolve(name + ".json"); if (Files.isRegularFile(indexFile)) { - joinEntries(JsonUtils.fromNonNullJson(FileUtils.readText(indexFile.toFile()), new TypeToken>() { + joinEntries(JsonUtils.fromNonNullJson(FileUtils.readText(indexFile), new TypeToken>() { }.getType())); } } catch (IOException | JsonParseException e) { @@ -420,7 +420,7 @@ public class CacheRepository { } public void saveToFile() { - try (RandomAccessFile file = new RandomAccessFile(indexFile.toFile(), "rw"); FileChannel channel = file.getChannel()) { + try (FileChannel channel = FileChannel.open(indexFile, StandardOpenOption.READ, StandardOpenOption.WRITE)) { FileLock lock = channel.lock(); try { Map indexOnDisk = JsonUtils.fromMaybeMalformedJson(new String(IOUtils.readFullyWithoutClosing(Channels.newInputStream(channel)), UTF_8), new TypeToken>() {