Rabbit-R1/switch port/java/sources/io/sentry/util/FileUtils.java
2024-05-21 17:08:36 -04:00

108 lines
3.8 KiB
Java

package io.sentry.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
/* loaded from: classes3.dex */
public final class FileUtils {
public static boolean deleteRecursively(File file) {
if (file == null || !file.exists()) {
return true;
}
if (file.isFile()) {
return file.delete();
}
File[] listFiles = file.listFiles();
if (listFiles == null) {
return true;
}
for (File file2 : listFiles) {
if (!deleteRecursively(file2)) {
return false;
}
}
return file.delete();
}
public static String readText(File file) throws IOException {
if (file == null || !file.exists() || !file.isFile() || !file.canRead()) {
return null;
}
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
try {
String readLine = bufferedReader.readLine();
if (readLine != null) {
sb.append(readLine);
}
while (true) {
String readLine2 = bufferedReader.readLine();
if (readLine2 != null) {
sb.append("\n").append(readLine2);
} else {
bufferedReader.close();
return sb.toString();
}
}
} catch (Throwable th) {
try {
bufferedReader.close();
} catch (Throwable th2) {
th.addSuppressed(th2);
}
throw th;
}
}
public static byte[] readBytesFromFile(String str, long j) throws IOException, SecurityException {
File file = new File(str);
if (!file.exists()) {
throw new IOException(String.format("File '%s' doesn't exists", file.getName()));
}
if (!file.isFile()) {
throw new IOException(String.format("Reading path %s failed, because it's not a file.", str));
}
if (!file.canRead()) {
throw new IOException(String.format("Reading the item %s failed, because can't read the file.", str));
}
if (file.length() > j) {
throw new IOException(String.format("Reading file failed, because size located at '%s' with %d bytes is bigger than the maximum allowed size of %d bytes.", str, Long.valueOf(file.length()), Long.valueOf(j)));
}
FileInputStream fileInputStream = new FileInputStream(str);
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
byte[] bArr = new byte[1024];
while (true) {
int read = bufferedInputStream.read(bArr);
if (read != -1) {
byteArrayOutputStream.write(bArr, 0, read);
} else {
byte[] byteArray = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
bufferedInputStream.close();
fileInputStream.close();
return byteArray;
}
}
} finally {
}
} finally {
}
} catch (Throwable th) {
try {
fileInputStream.close();
} catch (Throwable th2) {
th.addSuppressed(th2);
}
throw th;
}
}
}