package org.bouncycastle.bcpg;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import org.bouncycastle.asn1.cmc.BodyPartID;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
class StreamUtil {
    private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();
    static int flag_eof = 0;
    static int flag_isLongLength = 1;
    static int flag_partial = 2;

    StreamUtil() {
    }

    static int findLimit(InputStream inputStream) {
        if (inputStream instanceof ByteArrayInputStream) {
            return ((ByteArrayInputStream) inputStream).available();
        }
        if (inputStream instanceof FileInputStream) {
            try {
                FileChannel channel = ((FileInputStream) inputStream).getChannel();
                long size = channel != null ? channel.size() : 2147483647L;
                if (size < 2147483647L) {
                    return (int) size;
                }
            } catch (IOException e) {
            }
        }
        if (MAX_MEMORY > 2147483647L) {
            return Integer.MAX_VALUE;
        }
        return (int) MAX_MEMORY;
    }

    static int read2OctetLength(InputStream inputStream) throws IOException {
        return inputStream.read() | (inputStream.read() << 8);
    }

    static int read4OctetLength(InputStream inputStream) throws IOException {
        return inputStream.read() | (inputStream.read() << 24) | (inputStream.read() << 16) | (inputStream.read() << 8);
    }

    static long read8OctetLength(InputStream inputStream) throws IOException {
        return (((long) inputStream.read()) << 56) | (((long) inputStream.read()) << 48) | (((long) inputStream.read()) << 40) | (((long) inputStream.read()) << 32) | (((long) inputStream.read()) << 24) | (((long) inputStream.read()) << 16) | (((long) inputStream.read()) << 8) | ((long) inputStream.read());
    }

    static int readBodyLen(InputStream inputStream, boolean[] zArr) throws IOException {
        Arrays.fill(zArr, false);
        int i = inputStream.read();
        if (i < 0) {
            zArr[flag_eof] = true;
        }
        if (i < 192) {
            return i;
        }
        if (i <= 223) {
            return ((i - 192) << 8) + inputStream.read() + 192;
        }
        if (i == 255) {
            zArr[flag_isLongLength] = true;
            return read4OctetLength(inputStream);
        }
        zArr[flag_partial] = true;
        return 1 << (i & 31);
    }

    static long readKeyID(BCPGInputStream bCPGInputStream) throws IOException {
        return (((long) bCPGInputStream.read()) << 56) | (((long) bCPGInputStream.read()) << 48) | (((long) bCPGInputStream.read()) << 40) | (((long) bCPGInputStream.read()) << 32) | (((long) bCPGInputStream.read()) << 24) | (((long) bCPGInputStream.read()) << 16) | (((long) bCPGInputStream.read()) << 8) | ((long) bCPGInputStream.read());
    }

    static long readSeconds(BCPGInputStream bCPGInputStream) throws IOException {
        return ((long) read4OctetLength(bCPGInputStream)) & BodyPartID.bodyIdMax;
    }

    static long readTime(BCPGInputStream bCPGInputStream) throws IOException {
        return readSeconds(bCPGInputStream) * 1000;
    }

    static void write2OctetLength(OutputStream outputStream, int i) throws IOException {
        outputStream.write(i >> 8);
        outputStream.write(i);
    }

    static void write4OctetLength(OutputStream outputStream, int i) throws IOException {
        outputStream.write(i >> 24);
        outputStream.write(i >> 16);
        outputStream.write(i >> 8);
        outputStream.write(i);
    }

    static void write8OctetLength(OutputStream outputStream, long j) throws IOException {
        outputStream.write((int) (j >> 56));
        outputStream.write((int) (j >> 48));
        outputStream.write((int) (j >> 40));
        outputStream.write((int) (j >> 32));
        outputStream.write((int) (j >> 24));
        outputStream.write((int) (j >> 16));
        outputStream.write((int) (j >> 8));
        outputStream.write((int) j);
    }

    static void writeBodyLen(OutputStream outputStream, long j) throws IOException {
        outputStream.write((byte) (j >> 24));
        outputStream.write((byte) (j >> 16));
        outputStream.write((byte) (j >> 8));
        outputStream.write((byte) j);
    }

    static void writeKeyID(BCPGOutputStream bCPGOutputStream, long j) throws IOException {
        bCPGOutputStream.write((byte) (j >> 56));
        bCPGOutputStream.write((byte) (j >> 48));
        bCPGOutputStream.write((byte) (j >> 40));
        bCPGOutputStream.write((byte) (j >> 32));
        bCPGOutputStream.write((byte) (j >> 24));
        bCPGOutputStream.write((byte) (j >> 16));
        bCPGOutputStream.write((byte) (j >> 8));
        bCPGOutputStream.write((byte) j);
    }

    static void writeNewPacketLength(OutputStream outputStream, long j) throws IOException {
        if (j >= 192) {
            if (j > 8383) {
                outputStream.write(255);
                writeBodyLen(outputStream, j);
                return;
            } else {
                j -= 192;
                outputStream.write((byte) (((j >> 8) & 255) + 192));
            }
        }
        outputStream.write((byte) j);
    }

    static void writeSeconds(BCPGOutputStream bCPGOutputStream, long j) throws IOException {
        write4OctetLength(bCPGOutputStream, (int) j);
    }

    static void writeTime(BCPGOutputStream bCPGOutputStream, long j) throws IOException {
        writeSeconds(bCPGOutputStream, j / 1000);
    }
}
