close streams in IOUtils

This commit is contained in:
aliveoutside
2022-09-16 00:13:09 +03:00
parent 0efdefb207
commit 22e12b6422
@@ -26,7 +26,10 @@ public class IOUtils {
public static byte[] decodeStream(InputStream encIs, String keyURL) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IOException { public static byte[] decodeStream(InputStream encIs, String keyURL) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
CipherInputStream cip = new CipherInputStream(encIs, getCipher(keyURL)); CipherInputStream cip = new CipherInputStream(encIs, getCipher(keyURL));
return readAllBytes(cip); var bytes = readAllBytes(cip);
cip.close();
return bytes;
} }
public static Cipher getCipher(String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { public static Cipher getCipher(String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
@@ -46,7 +49,11 @@ public class IOUtils {
} }
public static String readAllLines(File file) throws IOException { public static String readAllLines(File file) throws IOException {
return readAllLines(new FileInputStream(file)); var is = new FileInputStream(file);
var bytes = readAllBytes(is);
is.close();
return new String(bytes, StandardCharsets.UTF_8);
} }
public static String readAllLines(InputStream is) throws IOException { public static String readAllLines(InputStream is) throws IOException {
@@ -54,7 +61,11 @@ public class IOUtils {
} }
public static byte[] readAllBytes(File file) throws IOException { public static byte[] readAllBytes(File file) throws IOException {
return readAllBytes(new FileInputStream(file)); var is = new FileInputStream(file);
var bytes = readAllBytes(is);
is.close();
return bytes;
} }
public static byte[] readAllBytes(InputStream is) throws IOException { public static byte[] readAllBytes(InputStream is) throws IOException {
@@ -63,7 +74,11 @@ public class IOUtils {
int len; int len;
while ((len = is.read(buffer)) > 0) while ((len = is.read(buffer)) > 0)
bos.write(buffer, 0, len); bos.write(buffer, 0, len);
return bos.toByteArray();
var bytes = bos.toByteArray();
bos.close();
return bytes;
} }
public static void writeToFile(File file, String content) throws IOException { public static void writeToFile(File file, String content) throws IOException {
@@ -73,6 +88,7 @@ public class IOUtils {
public static void writeToFile(File file, byte[] content) throws IOException { public static void writeToFile(File file, byte[] content) throws IOException {
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
fos.write(content); fos.write(content);
fos.close();
} }
public static void deleteRecursive(File f) { public static void deleteRecursive(File f) {