Rabbit-R1/android (non root)/java/sources/com/google/common/io/ByteStreams.java
2024-05-21 17:08:36 -04:00

667 lines
23 KiB
Java

package com.google.common.io;
import com.google.common.base.Preconditions;
import com.google.common.math.IntMath;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import javax.annotation.CheckForNull;
@ElementTypesAreNonnullByDefault
/* loaded from: classes3.dex */
public final class ByteStreams {
private static final int BUFFER_SIZE = 8192;
private static final int MAX_ARRAY_LEN = 2147483639;
private static final OutputStream NULL_OUTPUT_STREAM = new OutputStream() { // from class: com.google.common.io.ByteStreams.1
public String toString() {
return "ByteStreams.nullOutputStream()";
}
@Override // java.io.OutputStream
public void write(int i) {
}
@Override // java.io.OutputStream
public void write(byte[] bArr) {
Preconditions.checkNotNull(bArr);
}
@Override // java.io.OutputStream
public void write(byte[] bArr, int i, int i2) {
Preconditions.checkNotNull(bArr);
Preconditions.checkPositionIndexes(i, i2 + i, bArr.length);
}
};
private static final int TO_BYTE_ARRAY_DEQUE_SIZE = 20;
private static final int ZERO_COPY_CHUNK_SIZE = 524288;
/* JADX INFO: Access modifiers changed from: package-private */
public static byte[] createBuffer() {
return new byte[8192];
}
public static OutputStream nullOutputStream() {
return NULL_OUTPUT_STREAM;
}
private ByteStreams() {
}
public static long copy(InputStream inputStream, OutputStream outputStream) throws IOException {
Preconditions.checkNotNull(inputStream);
Preconditions.checkNotNull(outputStream);
byte[] createBuffer = createBuffer();
long j = 0;
while (true) {
int read = inputStream.read(createBuffer);
if (read == -1) {
return j;
}
outputStream.write(createBuffer, 0, read);
j += read;
}
}
public static long copy(ReadableByteChannel readableByteChannel, WritableByteChannel writableByteChannel) throws IOException {
Preconditions.checkNotNull(readableByteChannel);
Preconditions.checkNotNull(writableByteChannel);
long j = 0;
if (readableByteChannel instanceof FileChannel) {
FileChannel fileChannel = (FileChannel) readableByteChannel;
long position = fileChannel.position();
long j2 = position;
while (true) {
long transferTo = fileChannel.transferTo(j2, 524288L, writableByteChannel);
j2 += transferTo;
fileChannel.position(j2);
if (transferTo <= 0 && j2 >= fileChannel.size()) {
return j2 - position;
}
}
} else {
ByteBuffer wrap = ByteBuffer.wrap(createBuffer());
while (readableByteChannel.read(wrap) != -1) {
Java8Compatibility.flip(wrap);
while (wrap.hasRemaining()) {
j += writableByteChannel.write(wrap);
}
Java8Compatibility.clear(wrap);
}
return j;
}
}
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 = IntMath.saturatedMultiply(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");
}
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[] toByteArray(InputStream inputStream) throws IOException {
Preconditions.checkNotNull(inputStream);
return toByteArrayInternal(inputStream, new ArrayDeque(20), 0);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static byte[] toByteArray(InputStream inputStream, long j) throws IOException {
Preconditions.checkArgument(j >= 0, "expectedSize (%s) must be non-negative", j);
if (j > 2147483639) {
throw new OutOfMemoryError(new StringBuilder(62).append(j).append(" bytes is too large to fit in a byte array").toString());
}
int i = (int) j;
byte[] bArr = new byte[i];
int i2 = i;
while (i2 > 0) {
int i3 = i - i2;
int read = inputStream.read(bArr, i3, i2);
if (read == -1) {
return Arrays.copyOf(bArr, i3);
}
i2 -= read;
}
int read2 = inputStream.read();
if (read2 == -1) {
return bArr;
}
ArrayDeque arrayDeque = new ArrayDeque(22);
arrayDeque.add(bArr);
arrayDeque.add(new byte[]{(byte) read2});
return toByteArrayInternal(inputStream, arrayDeque, i + 1);
}
public static long exhaust(InputStream inputStream) throws IOException {
byte[] createBuffer = createBuffer();
long j = 0;
while (true) {
long read = inputStream.read(createBuffer);
if (read == -1) {
return j;
}
j += read;
}
}
public static ByteArrayDataInput newDataInput(byte[] bArr) {
return newDataInput(new ByteArrayInputStream(bArr));
}
public static ByteArrayDataInput newDataInput(byte[] bArr, int i) {
Preconditions.checkPositionIndex(i, bArr.length);
return newDataInput(new ByteArrayInputStream(bArr, i, bArr.length - i));
}
public static ByteArrayDataInput newDataInput(ByteArrayInputStream byteArrayInputStream) {
return new ByteArrayDataInputStream((ByteArrayInputStream) Preconditions.checkNotNull(byteArrayInputStream));
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes3.dex */
public static class ByteArrayDataInputStream implements ByteArrayDataInput {
final DataInput input;
ByteArrayDataInputStream(ByteArrayInputStream byteArrayInputStream) {
this.input = new DataInputStream(byteArrayInputStream);
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public void readFully(byte[] bArr) {
try {
this.input.readFully(bArr);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public void readFully(byte[] bArr, int i, int i2) {
try {
this.input.readFully(bArr, i, i2);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public int skipBytes(int i) {
try {
return this.input.skipBytes(i);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public boolean readBoolean() {
try {
return this.input.readBoolean();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public byte readByte() {
try {
return this.input.readByte();
} catch (EOFException e) {
throw new IllegalStateException(e);
} catch (IOException e2) {
throw new AssertionError(e2);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public int readUnsignedByte() {
try {
return this.input.readUnsignedByte();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public short readShort() {
try {
return this.input.readShort();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public int readUnsignedShort() {
try {
return this.input.readUnsignedShort();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public char readChar() {
try {
return this.input.readChar();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public int readInt() {
try {
return this.input.readInt();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public long readLong() {
try {
return this.input.readLong();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public float readFloat() {
try {
return this.input.readFloat();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public double readDouble() {
try {
return this.input.readDouble();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
@CheckForNull
public String readLine() {
try {
return this.input.readLine();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Override // com.google.common.io.ByteArrayDataInput, java.io.DataInput
public String readUTF() {
try {
return this.input.readUTF();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
public static ByteArrayDataOutput newDataOutput() {
return newDataOutput(new ByteArrayOutputStream());
}
public static ByteArrayDataOutput newDataOutput(int i) {
if (i < 0) {
throw new IllegalArgumentException(String.format("Invalid size: %s", Integer.valueOf(i)));
}
return newDataOutput(new ByteArrayOutputStream(i));
}
public static ByteArrayDataOutput newDataOutput(ByteArrayOutputStream byteArrayOutputStream) {
return new ByteArrayDataOutputStream((ByteArrayOutputStream) Preconditions.checkNotNull(byteArrayOutputStream));
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes3.dex */
public static class ByteArrayDataOutputStream implements ByteArrayDataOutput {
final ByteArrayOutputStream byteArrayOutputStream;
final DataOutput output;
ByteArrayDataOutputStream(ByteArrayOutputStream byteArrayOutputStream) {
this.byteArrayOutputStream = byteArrayOutputStream;
this.output = new DataOutputStream(byteArrayOutputStream);
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void write(int i) {
try {
this.output.write(i);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void write(byte[] bArr) {
try {
this.output.write(bArr);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void write(byte[] bArr, int i, int i2) {
try {
this.output.write(bArr, i, i2);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeBoolean(boolean z) {
try {
this.output.writeBoolean(z);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeByte(int i) {
try {
this.output.writeByte(i);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeBytes(String str) {
try {
this.output.writeBytes(str);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeChar(int i) {
try {
this.output.writeChar(i);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeChars(String str) {
try {
this.output.writeChars(str);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeDouble(double d) {
try {
this.output.writeDouble(d);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeFloat(float f) {
try {
this.output.writeFloat(f);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeInt(int i) {
try {
this.output.writeInt(i);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeLong(long j) {
try {
this.output.writeLong(j);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeShort(int i) {
try {
this.output.writeShort(i);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput, java.io.DataOutput
public void writeUTF(String str) {
try {
this.output.writeUTF(str);
} catch (IOException e) {
throw new AssertionError(e);
}
}
@Override // com.google.common.io.ByteArrayDataOutput
public byte[] toByteArray() {
return this.byteArrayOutputStream.toByteArray();
}
}
public static InputStream limit(InputStream inputStream, long j) {
return new LimitedInputStream(inputStream, j);
}
/* loaded from: classes3.dex */
private static final class LimitedInputStream extends FilterInputStream {
private long left;
private long mark;
LimitedInputStream(InputStream inputStream, long j) {
super(inputStream);
this.mark = -1L;
Preconditions.checkNotNull(inputStream);
Preconditions.checkArgument(j >= 0, "limit must be non-negative");
this.left = j;
}
@Override // java.io.FilterInputStream, java.io.InputStream
public int available() throws IOException {
return (int) Math.min(this.in.available(), this.left);
}
@Override // java.io.FilterInputStream, java.io.InputStream
public synchronized void mark(int i) {
this.in.mark(i);
this.mark = this.left;
}
@Override // java.io.FilterInputStream, java.io.InputStream
public int read() throws IOException {
if (this.left == 0) {
return -1;
}
int read = 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) throws IOException {
long j = this.left;
if (j == 0) {
return -1;
}
int read = 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 (!this.in.markSupported()) {
throw new IOException("Mark not supported");
}
if (this.mark == -1) {
throw new IOException("Mark not set");
}
this.in.reset();
this.left = this.mark;
}
@Override // java.io.FilterInputStream, java.io.InputStream
public long skip(long j) throws IOException {
long skip = this.in.skip(Math.min(j, this.left));
this.left -= skip;
return skip;
}
}
public static void readFully(InputStream inputStream, byte[] bArr) throws IOException {
readFully(inputStream, bArr, 0, bArr.length);
}
public static void readFully(InputStream inputStream, byte[] bArr, int i, int i2) throws IOException {
int read = read(inputStream, bArr, i, i2);
if (read != i2) {
throw new EOFException(new StringBuilder(81).append("reached end of stream after reading ").append(read).append(" bytes; ").append(i2).append(" bytes expected").toString());
}
}
public static void skipFully(InputStream inputStream, long j) throws IOException {
long skipUpTo = skipUpTo(inputStream, j);
if (skipUpTo < j) {
throw new EOFException(new StringBuilder(100).append("reached end of stream after skipping ").append(skipUpTo).append(" bytes; ").append(j).append(" bytes expected").toString());
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public static long skipUpTo(InputStream inputStream, long j) throws IOException {
byte[] bArr = null;
long j2 = 0;
while (j2 < j) {
long j3 = j - j2;
long skipSafely = skipSafely(inputStream, j3);
if (skipSafely == 0) {
int min = (int) Math.min(j3, 8192L);
if (bArr == null) {
bArr = new byte[min];
}
skipSafely = inputStream.read(bArr, 0, min);
if (skipSafely == -1) {
break;
}
}
j2 += skipSafely;
}
return j2;
}
private static long skipSafely(InputStream inputStream, long j) throws IOException {
int available = inputStream.available();
if (available == 0) {
return 0L;
}
return inputStream.skip(Math.min(available, j));
}
@ParametricNullness
public static <T> T readBytes(InputStream inputStream, ByteProcessor<T> byteProcessor) throws IOException {
int read;
Preconditions.checkNotNull(inputStream);
Preconditions.checkNotNull(byteProcessor);
byte[] createBuffer = createBuffer();
do {
read = inputStream.read(createBuffer);
if (read == -1) {
break;
}
} while (byteProcessor.processBytes(createBuffer, 0, read));
return byteProcessor.getResult();
}
public static int read(InputStream inputStream, byte[] bArr, int i, int i2) throws IOException {
Preconditions.checkNotNull(inputStream);
Preconditions.checkNotNull(bArr);
if (i2 < 0) {
throw new IndexOutOfBoundsException(String.format("len (%s) cannot be negative", Integer.valueOf(i2)));
}
Preconditions.checkPositionIndexes(i, i + i2, bArr.length);
int i3 = 0;
while (i3 < i2) {
int read = inputStream.read(bArr, i + i3, i2 - i3);
if (read == -1) {
break;
}
i3 += read;
}
return i3;
}
}