alexandroid/decompiled/jadx/sources/com/google/firebase/messaging/ByteStreams.java
KaKi87 a47fad5135 📦 Add decompiled Alexa 2.2.730155.0 (jadx, apktool, Hermes strings)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 00:04:16 +01:00

151 lines
5.1 KiB
Java

package com.google.firebase.messaging;
import androidx.collection.SieveCacheKt;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
/* loaded from: classes8.dex */
final class ByteStreams {
private static final int BUFFER_SIZE = 8192;
private static final int MAX_ARRAY_LEN = 2147483639;
private static final int TO_BYTE_ARRAY_DEQUE_SIZE = 20;
private ByteStreams() {
}
private static byte[] combineBuffers(Queue<byte[]> queue, int i) {
if (queue.isEmpty()) {
return new byte[0];
}
byte[] remove = queue.remove();
if (remove.length == i) {
return remove;
}
int length = i - remove.length;
byte[] copyOf = Arrays.copyOf(remove, i);
while (length > 0) {
byte[] remove2 = queue.remove();
int min = Math.min(length, remove2.length);
System.arraycopy(remove2, 0, copyOf, i - length, min);
length -= min;
}
return copyOf;
}
public static byte[] createBuffer() {
return new byte[8192];
}
public static InputStream limit(InputStream inputStream, long j) {
return new LimitedInputStream(inputStream, j);
}
private static int saturatedCast(long j) {
if (j > SieveCacheKt.NodeLinkMask) {
return Integer.MAX_VALUE;
}
if (j < SieveCacheKt.NodeMetaAndPreviousMask) {
return Integer.MIN_VALUE;
}
return (int) j;
}
public static byte[] toByteArray(InputStream inputStream) throws IOException {
return toByteArrayInternal(inputStream, new ArrayDeque(20), 0);
}
private static byte[] toByteArrayInternal(InputStream inputStream, Queue<byte[]> queue, int i) throws IOException {
int min = Math.min(8192, Math.max(128, Integer.highestOneBit(i) * 2));
while (i < MAX_ARRAY_LEN) {
int min2 = Math.min(min, MAX_ARRAY_LEN - 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, MAX_ARRAY_LEN);
}
throw new OutOfMemoryError("input is too large to fit in a byte array");
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public static final class LimitedInputStream extends FilterInputStream {
private long left;
private 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() throws IOException {
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(byte[] bArr, int i, int i2) throws IOException {
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() throws IOException {
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) throws IOException {
long skip = ((FilterInputStream) this).in.skip(Math.min(j, this.left));
this.left -= skip;
return skip;
}
@Override // java.io.FilterInputStream, java.io.InputStream
public int read() throws IOException {
if (this.left == 0) {
return -1;
}
int read = ((FilterInputStream) this).in.read();
if (read != -1) {
this.left--;
}
return read;
}
}
}