diff options
author | Keuin <[email protected]> | 2023-01-13 02:49:57 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-01-13 02:49:57 +0800 |
commit | 5dc6e700cbcbc01833a02a193f660a286b934898 (patch) | |
tree | 72cf4cc3cfae2b3a7fb978bc0c465e329909db3e /src/main/java/com/keuin/kbackupfabric/util | |
parent | ff7d23b5b915f04c2b5f3614701cb56cfc605228 (diff) |
Code cleanup.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util')
5 files changed, 23 insertions, 70 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java b/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java index 2acc562..6fb1e3f 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java @@ -12,11 +12,7 @@ import java.lang.reflect.Method; public final class ReflectionUtils { /** - * 获取私有成员变量的值 - * - * @param instance - * @param filedName - * @return + * Read private fields. */ public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException { Field field = instance.getClass().getDeclaredField(filedName); @@ -25,13 +21,7 @@ public final class ReflectionUtils { } /** - * 设置私有成员的值 - * - * @param instance - * @param fieldName - * @param value - * @throws NoSuchFieldException - * @throws IllegalAccessException + * Modify private fields. */ public static void setPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { Field field = instance.getClass().getDeclaredField(fieldName); @@ -40,18 +30,10 @@ public final class ReflectionUtils { } /** - * 访问私有方法 - * - * @param instance - * @param methodName - * @param classes - * @param objects - * @return - * @throws NoSuchMethodException - * @throws InvocationTargetException - * @throws IllegalAccessException + * Call private methods. */ - public static Object invokePrivateMethod(Object instance, String methodName, Class[] classes, String objects) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public static Object invokePrivateMethod(Object instance, String methodName, Class<?>[] classes, String objects) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = instance.getClass().getDeclaredMethod(methodName, classes); method.setAccessible(true); return method.invoke(instance, objects); diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index eea1148..ce14c8a 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -4,6 +4,7 @@ import com.keuin.kbackupfabric.exception.ZipUtilException; import com.keuin.kbackupfabric.metadata.BackupMetadata; import java.io.*; +import java.nio.file.Files; import java.util.*; import java.util.zip.*; @@ -46,7 +47,7 @@ public final class ZipUtil { ZipEntry entry = new ZipEntry(subPath); zipOutputStream.putNextEntry(entry); // BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); - try (InputStream inputStream = new FileInputStream(file)) { + try (InputStream inputStream = Files.newInputStream(file.toPath())) { while ((count = inputStream.read(buffer, 0, zipBufferSize)) != -1) { zipOutputStream.write(buffer, 0, count); } @@ -100,7 +101,7 @@ public final class ZipUtil { File zipDir = new File(zipPath); if (!zipDir.exists() || !zipDir.isDirectory()) { if (!zipDir.mkdirs()) { - throw new IOException(String.format("Failed to make directory tree %s", zipDir.toString())); + throw new IOException(String.format("Failed to make directory tree %s", zipDir)); } } @@ -110,7 +111,7 @@ public final class ZipUtil { if (zipFile.exists()) { //删除已存在的目标文件 if (!zipFile.delete()) { - throw new IOException(String.format("Failed to delete existing file %s", zipFile.toString())); + throw new IOException(String.format("Failed to delete existing file %s", zipFile)); } } @@ -175,7 +176,7 @@ public final class ZipUtil { File unzipFileDir = new File(unzipFilePath); if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) { if (!unzipFileDir.mkdirs()) - throw new IOException(String.format("Failed to make directory tree %s", unzipFileDir.toString())); + throw new IOException(String.format("Failed to make directory tree %s", unzipFileDir)); } // 开始解压 @@ -202,7 +203,7 @@ public final class ZipUtil { // 如果文件夹路径不存在,则创建文件夹 if (!entryDir.exists() || !entryDir.isDirectory()) { if (!entryDir.mkdirs()) - throw new IOException(String.format("Failed to make directory tree %s", entryDir.toString())); + throw new IOException(String.format("Failed to make directory tree %s", entryDir)); } // 创建解压文件 @@ -213,16 +214,16 @@ public final class ZipUtil { securityManager.checkDelete(entryFilePath); // 删除已存在的目标文件 if (!entryFile.delete()) - throw new IOException(String.format("Failed to delete existing file %s", entryFile.toString())); + throw new IOException(String.format("Failed to delete existing file %s", entryFile)); } if (entry.isDirectory()) { // If the entry is a directory, we make its corresponding directory. if (!entryFile.mkdir()) - throw new IOException(String.format("Failed to create directory %s", entryFile.toString())); + throw new IOException(String.format("Failed to create directory %s", entryFile)); } else { // Is a file, we write the data // 写入文件 - try (OutputStream outputStream = new FileOutputStream(entryFile); + try (OutputStream outputStream = Files.newOutputStream(entryFile.toPath()); InputStream inputStream = zip.getInputStream(entry)) { while ((count = inputStream.read(buffer, 0, unzipBufferSize)) != -1) { outputStream.write(buffer, 0, count); diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java index 0cd36a0..38b96ab 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java @@ -8,7 +8,7 @@ import java.util.*; * or it will not be compatible with old backups. * It remains only to keep a backward compatibility, and should be converted to `ObjectCollection2` as soon as possible. * Thus, this class is marked as `Depreciated`. However, it should not be removed since it is needed to read legacy - * backups correctly. But new codes should not use this class any more. + * backups correctly. But new codes should not use this class anymore. */ @Deprecated public class ObjectCollection implements Serializable { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java index 31cde29..c640c0b 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java @@ -3,12 +3,11 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; import com.keuin.kbackupfabric.util.BytesUtil; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.util.Objects; +import static com.keuin.kbackupfabric.backup.incremental.identifier.Sha256Identifier.sha256Hash; + /** * Identifier based on sha256. * Immutable. @@ -50,35 +49,7 @@ public class Sha256Identifier extends SingleHashIdentifier { @Override protected byte[] hash(File file) throws IOException { - try { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - - try (FileInputStream inputStream = new FileInputStream(file)) { - // This does not work. I don't know why -// FileChannel channel = inputStream.getChannel(); -// ByteBuffer buffer = ByteBuffer.allocate(128); -// int readLength; -// while ((readLength = channel.read(buffer)) > 0) -// digest.update(buffer); - - // This also works, without warnings - byte[] readBuffer = new byte[1024 * 1024]; - int readLength; - while ((readLength = inputStream.read(readBuffer)) > 0) - digest.update(readBuffer, 0, readLength); - - // The below lines also works, but the IDE will complain about the while loop -// DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); -// while(digestInputStream.read() > 0) -// ; - - return digest.digest(); - } - - } catch (NoSuchAlgorithmException ignored) { - // this shouldn't happen - return new byte[SHA256_LENGTH]; - } + return sha256Hash(file); } }
\ No newline at end of file diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java index 6c67918..285c6ce 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java @@ -12,7 +12,6 @@ import java.util.Objects; * Immutable. */ public abstract class SingleHashIdentifier implements ObjectIdentifier { - private final byte[] hash; private final String type; @@ -37,11 +36,11 @@ public abstract class SingleHashIdentifier implements ObjectIdentifier { } @Override - public boolean equals(Object obj) { - if (!(obj instanceof com.keuin.kbackupfabric.backup.incremental.identifier.SingleHashIdentifier)) { - return false; - } - return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SingleHashIdentifier that = (SingleHashIdentifier) o; + return Arrays.equals(hash, that.hash) && type.equals(that.type); } @Override |