// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class FileUtils
{
    public static boolean deleteRecursively(@Nullable final File file) {
        if (file == null || !file.exists()) {
            return true;
        }
        if (file.isFile()) {
            return file.delete();
        }
        final File[] children = file.listFiles();
        if (children == null) {
            return true;
        }
        for (final File f : children) {
            if (!deleteRecursively(f)) {
                return false;
            }
        }
        return file.delete();
    }
    
    @Nullable
    public static String readText(@Nullable final File file) throws IOException {
        if (file == null || !file.exists() || !file.isFile() || !file.canRead()) {
            return null;
        }
        final StringBuilder contentBuilder = new StringBuilder();
        try (final BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            if ((line = br.readLine()) != null) {
                contentBuilder.append(line);
            }
            while ((line = br.readLine()) != null) {
                contentBuilder.append("\n").append(line);
            }
        }
        return contentBuilder.toString();
    }
    
    public static byte[] readBytesFromFile(final String pathname, final long maxFileLength) throws IOException, SecurityException {
        final File file = new File(pathname);
        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.", pathname));
        }
        if (!file.canRead()) {
            throw new IOException(String.format("Reading the item %s failed, because can't read the file.", pathname));
        }
        if (file.length() > maxFileLength) {
            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.", pathname, file.length(), maxFileLength));
        }
        final FileInputStream fileInputStream = new FileInputStream(pathname);
        try (final BufferedInputStream inputStream = new BufferedInputStream(fileInputStream)) {
            try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                final byte[] bytes = new byte[1024];
                final int offset = 0;
                int length;
                while ((length = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, offset, length);
                }
                return outputStream.toByteArray();
            }
            fileInputStream.close();
        }
        catch (final Throwable t3) {
            try {
                fileInputStream.close();
            }
            catch (final Throwable exception3) {
                t3.addSuppressed(exception3);
            }
            throw t3;
        }
    }
}
