package org.bouncycastle.asn1; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.channels.FileChannel; /* loaded from: classes6.dex */ class StreamUtil { private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory(); /* JADX INFO: Access modifiers changed from: package-private */ public static int calculateBodyLength(int i) { int i2 = 1; if (i > 127) { int i3 = 1; while (true) { i >>>= 8; if (i == 0) { break; } i3++; } for (int i4 = (i3 - 1) << 3; i4 >= 0; i4 -= 8) { i2++; } } return i2; } /* JADX INFO: Access modifiers changed from: package-private */ public static int findLimit(InputStream inputStream) { if (inputStream instanceof LimitedInputStream) { return ((LimitedInputStream) inputStream).getRemaining(); } if (inputStream instanceof ASN1InputStream) { return ((ASN1InputStream) inputStream).getLimit(); } 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 unused) { } } long j = MAX_MEMORY; if (j > 2147483647L) { return Integer.MAX_VALUE; } return (int) j; } /* JADX INFO: Access modifiers changed from: package-private */ public static int calculateTagLength(int i) throws IOException { if (i < 31) { return 1; } if (i < 128) { return 2; } byte[] bArr = new byte[5]; int i2 = 4; bArr[4] = (byte) (i & 127); do { i >>= 7; i2--; bArr[i2] = (byte) ((i & 127) | 128); } while (i > 127); return 6 - i2; } StreamUtil() { } }