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; /* loaded from: classes2.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 @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); } public String toString() { return "ByteStreams.nullOutputStream()"; } }; private static final int TO_BYTE_ARRAY_DEQUE_SIZE = 20; private static final int ZERO_COPY_CHUNK_SIZE = 524288; 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 queue, int i) throws IOException { int i2 = 8192; while (i < MAX_ARRAY_LEN) { int min = Math.min(i2, MAX_ARRAY_LEN - i); byte[] bArr = new byte[min]; queue.add(bArr); int i3 = 0; while (i3 < min) { int read = inputStream.read(bArr, i3, min - i3); if (read == -1) { return combineBuffers(queue, i); } i3 += read; i += read; } i2 = IntMath.saturatedMultiply(i2, 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 queue, int i) { byte[] bArr = new byte[i]; int i2 = i; while (i2 > 0) { byte[] remove = queue.remove(); int min = Math.min(i2, remove.length); System.arraycopy(remove, 0, bArr, i - i2, min); i2 -= min; } return bArr; } 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) { StringBuilder sb = new StringBuilder(62); sb.append(j); sb.append(" bytes is too large to fit in a byte array"); throw new OutOfMemoryError(sb.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: package-private */ /* loaded from: classes2.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 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: package-private */ /* loaded from: classes2.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: classes2.dex */ 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 final int available() throws IOException { return (int) Math.min(((FilterInputStream) this).in.available(), this.left); } @Override // java.io.FilterInputStream, java.io.InputStream public final void mark(int i) { synchronized (this) { ((FilterInputStream) this).in.mark(i); this.mark = this.left; } } @Override // java.io.FilterInputStream, java.io.InputStream public final int read() throws IOException { 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 final 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 final void reset() throws IOException { synchronized (this) { 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 final long skip(long j) throws IOException { long skip = ((FilterInputStream) 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) { return; } StringBuilder sb = new StringBuilder(81); sb.append("reached end of stream after reading "); sb.append(read); sb.append(" bytes; "); sb.append(i2); sb.append(" bytes expected"); throw new EOFException(sb.toString()); } public static void skipFully(InputStream inputStream, long j) throws IOException { long skipUpTo = skipUpTo(inputStream, j); if (skipUpTo >= j) { return; } StringBuilder sb = new StringBuilder(100); sb.append("reached end of stream after skipping "); sb.append(skipUpTo); sb.append(" bytes; "); sb.append(j); sb.append(" bytes expected"); throw new EOFException(sb.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)); } public static T readBytes(InputStream inputStream, ByteProcessor 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; } public static OutputStream nullOutputStream() { return NULL_OUTPUT_STREAM; } /* JADX INFO: Access modifiers changed from: package-private */ public static byte[] createBuffer() { return new byte[8192]; } }