From 9f2b3edbe88378a7455bed600c46ba8ecf6ab1aa Mon Sep 17 00:00:00 2001 From: Vologhat Date: Thu, 1 Dec 2022 23:23:55 +0300 Subject: [PATCH] Update IOUtils.java --- .../java/ru/vtosters/lite/utils/IOUtils.java | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/utils/IOUtils.java b/app/src/main/java/ru/vtosters/lite/utils/IOUtils.java index d06def389..e1e2c00c3 100644 --- a/app/src/main/java/ru/vtosters/lite/utils/IOUtils.java +++ b/app/src/main/java/ru/vtosters/lite/utils/IOUtils.java @@ -26,10 +26,7 @@ public class IOUtils { public static byte[] decodeStream(InputStream encIs, String keyURL) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IOException { CipherInputStream cip = new CipherInputStream(encIs, getCipher(keyURL)); - var bytes = readAllBytes(cip); - cip.close(); - - return bytes; + return readAllBytes(cip); } public static Cipher getCipher(String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { @@ -49,23 +46,15 @@ public class IOUtils { } public static String readAllLines(File file) throws IOException { - var is = new FileInputStream(file); - var bytes = readAllBytes(is); - is.close(); - - return new String(bytes, StandardCharsets.UTF_8); + return readAllLines(new FileInputStream(file)); } public static String readAllLines(InputStream is) throws IOException { - return new String(readAllBytes(is), StandardCharsets.UTF_8); + return new String(readAllBytes(is), "utf-8"); } public static byte[] readAllBytes(File file) throws IOException { - var is = new FileInputStream(file); - var bytes = readAllBytes(is); - is.close(); - - return bytes; + return readAllBytes(new FileInputStream(file)); } public static byte[] readAllBytes(InputStream is) throws IOException { @@ -74,11 +63,7 @@ public class IOUtils { int len; while ((len = is.read(buffer)) > 0) bos.write(buffer, 0, len); - - var bytes = bos.toByteArray(); - bos.close(); - - return bytes; + return bos.toByteArray(); } public static void writeToFile(File file, String content) throws IOException { @@ -86,9 +71,32 @@ public class IOUtils { } public static void writeToFile(File file, byte[] content) throws IOException { + if (!file.exists()) + file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write(content); - fos.close(); + } + + public static void copyFileToDirectory(File dest, File targetDir) throws IOException { + if (!targetDir.exists()) + targetDir.mkdirs(); + var path = dest.getAbsolutePath(); + var target = new File(targetDir, path.substring(path.lastIndexOf("/"))); + copyFile(dest, target); + } + + public static void copyFile(File dest, File target) throws IOException { + var parent = target.getParentFile(); + if (!parent.exists()) + parent.getParentFile().mkdirs(); + writeToFile(target, readAllBytes(dest)); + } + + public static void copyFile(InputStream is, File target) throws IOException { + var parent = target.getParentFile(); + if (!parent.exists()) + parent.getParentFile().mkdirs(); + writeToFile(target, readAllBytes(is)); } public static void deleteRecursive(File f) { @@ -121,4 +129,4 @@ public class IOUtils { public static String getValidFileName(String fileName) { return fileName.replaceAll("[\\\\/:*?\"<>|]", ""); } -} +} \ No newline at end of file