Update IOUtils.java

This commit is contained in:
Vologhat
2022-12-01 23:08:29 +03:00
parent 09807a2954
commit 2f46c193dd
@@ -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) {
@@ -117,8 +125,4 @@ public class IOUtils {
return size;
}
public static String getValidFileName(String fileName) {
return fileName.replaceAll("[\\\\/:*?\"<>|]", "");
}
}