package com.google.firebase.messaging; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Queue; /* loaded from: classes3.dex */ public abstract class ByteStreams { public static int saturatedCast(long j) { if (j > 2147483647L) { return Integer.MAX_VALUE; } if (j < -2147483648L) { return Integer.MIN_VALUE; } return (int) j; } public static byte[] toByteArrayInternal(InputStream inputStream, Queue queue, int i) { int min = Math.min(8192, Math.max(128, Integer.highestOneBit(i) * 2)); while (i < 2147483639) { int min2 = Math.min(min, 2147483639 - i); byte[] bArr = new byte[min2]; queue.add(bArr); int i2 = 0; while (i2 < min2) { int read = inputStream.read(bArr, i2, min2 - i2); if (read == -1) { return combineBuffers(queue, i); } i2 += read; i += read; } min = saturatedCast(min * (min < 4096 ? 4 : 2)); } if (inputStream.read() == -1) { return combineBuffers(queue, 2147483639); } throw new OutOfMemoryError("input is too large to fit in a byte array"); } public static byte[] combineBuffers(Queue queue, int i) { if (queue.isEmpty()) { return new byte[0]; } byte[] bArr = (byte[]) queue.remove(); if (bArr.length == i) { return bArr; } int length = i - bArr.length; byte[] copyOf = Arrays.copyOf(bArr, i); while (length > 0) { byte[] bArr2 = (byte[]) queue.remove(); int min = Math.min(length, bArr2.length); System.arraycopy(bArr2, 0, copyOf, i - length, min); length -= min; } return copyOf; } public static byte[] toByteArray(InputStream inputStream) { return toByteArrayInternal(inputStream, new ArrayDeque(20), 0); } public static InputStream limit(InputStream inputStream, long j) { return new LimitedInputStream(inputStream, j); } public static final class LimitedInputStream extends FilterInputStream { public long left; public long mark; public LimitedInputStream(InputStream inputStream, long j) { super(inputStream); this.mark = -1L; this.left = j; } @Override // java.io.FilterInputStream, java.io.InputStream public int available() { return (int) Math.min(((FilterInputStream) this).in.available(), this.left); } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized void mark(int i) { ((FilterInputStream) this).in.mark(i); this.mark = this.left; } @Override // java.io.FilterInputStream, java.io.InputStream public int read() { if (this.left == 0) { return -1; } int read = ((FilterInputStream) this).in.read(); if (read != -1) { this.left--; } return read; } @Override // java.io.FilterInputStream, java.io.InputStream public int read(byte[] bArr, int i, int i2) { long j = this.left; if (j == 0) { return -1; } int read = ((FilterInputStream) this).in.read(bArr, i, (int) Math.min(i2, j)); if (read != -1) { this.left -= read; } return read; } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized void reset() { if (!((FilterInputStream) this).in.markSupported()) { throw new IOException("Mark not supported"); } if (this.mark == -1) { throw new IOException("Mark not set"); } ((FilterInputStream) this).in.reset(); this.left = this.mark; } @Override // java.io.FilterInputStream, java.io.InputStream public long skip(long j) { long skip = ((FilterInputStream) this).in.skip(Math.min(j, this.left)); this.left -= skip; return skip; } } }