package org.bouncycastle.bcpg;

import java.io.IOException;
import java.math.BigInteger;
import org.bouncycastle.util.BigIntegers;

/* JADX INFO: loaded from: classes3.dex */
public class MPInteger extends BCPGObject {
    private final BigInteger value;

    public MPInteger(BigInteger bigInteger) {
        if (bigInteger == null || bigInteger.signum() < 0) {
            throw new IllegalArgumentException("value must not be null, or negative");
        }
        this.value = bigInteger;
    }

    public MPInteger(BCPGInputStream bCPGInputStream) throws IOException {
        this.value = readMPI(bCPGInputStream, false);
    }

    private static BigInteger readMPI(BCPGInputStream bCPGInputStream, boolean z) throws IOException {
        int i = StreamUtil.read2OctetLength(bCPGInputStream);
        byte[] bArr = new byte[(i + 7) / 8];
        bCPGInputStream.readFully(bArr);
        BigInteger bigInteger = new BigInteger(1, bArr);
        if (!z || bigInteger.bitLength() == i) {
            return bigInteger;
        }
        throw new IOException("malformed MPI");
    }

    @Override // org.bouncycastle.bcpg.BCPGObject
    public void encode(BCPGOutputStream bCPGOutputStream) throws IOException {
        StreamUtil.write2OctetLength(bCPGOutputStream, this.value.bitLength());
        BigIntegers.writeUnsignedByteArray(bCPGOutputStream, this.value);
    }

    public BigInteger getValue() {
        return this.value;
    }
}
