package kotlin.io.encoding; import io.flutter.plugin.editing.SpellCheckPlugin; import io.sentry.SentryEnvelopeItemHeader; import io.sentry.protocol.TransactionInfo; import java.io.IOException; import java.io.OutputStream; import kotlin.Metadata; import kotlin.collections.ArraysKt; import kotlin.jvm.internal.Intrinsics; /* compiled from: Base64IOStream.kt */ @Metadata(d1 = {"\u00000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0012\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\b\r\b\u0003\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0001\u0012\u0006\u0010\u0003\u001a\u00020\u0004¢\u0006\u0002\u0010\u0005J\b\u0010\u000e\u001a\u00020\u000fH\u0002J\b\u0010\u0010\u001a\u00020\u000fH\u0016J \u0010\u0011\u001a\u00020\t2\u0006\u0010\u0012\u001a\u00020\u00072\u0006\u0010\u0013\u001a\u00020\t2\u0006\u0010\u0014\u001a\u00020\tH\u0002J\b\u0010\u0015\u001a\u00020\u000fH\u0002J \u0010\u0016\u001a\u00020\t2\u0006\u0010\u0012\u001a\u00020\u00072\u0006\u0010\u0013\u001a\u00020\t2\u0006\u0010\u0014\u001a\u00020\tH\u0002J\b\u0010\u0017\u001a\u00020\u000fH\u0016J \u0010\u0018\u001a\u00020\u000f2\u0006\u0010\u0012\u001a\u00020\u00072\u0006\u0010\u0019\u001a\u00020\t2\u0006\u0010\u001a\u001a\u00020\tH\u0016J\u0010\u0010\u0018\u001a\u00020\u000f2\u0006\u0010\u001b\u001a\u00020\tH\u0016R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u000bX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0001X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\r\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u001c"}, d2 = {"Lkotlin/io/encoding/EncodeOutputStream;", "Ljava/io/OutputStream;", "output", "base64", "Lkotlin/io/encoding/Base64;", "(Ljava/io/OutputStream;Lkotlin/io/encoding/Base64;)V", "byteBuffer", "", "byteBufferLength", "", "isClosed", "", "lineLength", "symbolBuffer", "checkOpen", "", "close", "copyIntoByteBuffer", TransactionInfo.JsonKeys.SOURCE, SpellCheckPlugin.START_INDEX_KEY, SpellCheckPlugin.END_INDEX_KEY, "encodeByteBufferIntoOutput", "encodeIntoOutput", "flush", "write", "offset", SentryEnvelopeItemHeader.JsonKeys.LENGTH, "b", "kotlin-stdlib"}, k = 1, mv = {1, 9, 0}, xi = 48) /* loaded from: classes3.dex */ final class EncodeOutputStream extends OutputStream { private final Base64 base64; private final byte[] byteBuffer; private int byteBufferLength; private boolean isClosed; private int lineLength; private final OutputStream output; private final byte[] symbolBuffer; public EncodeOutputStream(OutputStream output, Base64 base64) { Intrinsics.checkNotNullParameter(output, "output"); Intrinsics.checkNotNullParameter(base64, "base64"); this.output = output; this.base64 = base64; this.lineLength = base64.getIsMimeScheme() ? 76 : -1; this.symbolBuffer = new byte[1024]; this.byteBuffer = new byte[3]; } @Override // java.io.OutputStream public void write(int b) { checkOpen(); byte[] bArr = this.byteBuffer; int i = this.byteBufferLength; int i2 = i + 1; this.byteBufferLength = i2; bArr[i] = (byte) b; if (i2 == 3) { encodeByteBufferIntoOutput(); } } @Override // java.io.OutputStream public void write(byte[] source, int offset, int length) { int i; Intrinsics.checkNotNullParameter(source, "source"); checkOpen(); if (offset < 0 || length < 0 || (i = offset + length) > source.length) { throw new IndexOutOfBoundsException("offset: " + offset + ", length: " + length + ", source size: " + source.length); } if (length == 0) { return; } int i2 = this.byteBufferLength; if (i2 >= 3) { throw new IllegalStateException("Check failed.".toString()); } if (i2 != 0) { offset += copyIntoByteBuffer(source, offset, i); if (this.byteBufferLength != 0) { return; } } while (offset + 3 <= i) { int min = Math.min((this.base64.getIsMimeScheme() ? this.lineLength : this.symbolBuffer.length) / 4, (i - offset) / 3); int i3 = (min * 3) + offset; if (encodeIntoOutput(source, offset, i3) != min * 4) { throw new IllegalStateException("Check failed.".toString()); } offset = i3; } ArraysKt.copyInto(source, this.byteBuffer, 0, offset, i); this.byteBufferLength = i - offset; } @Override // java.io.OutputStream, java.io.Flushable public void flush() { checkOpen(); this.output.flush(); } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.isClosed) { return; } this.isClosed = true; if (this.byteBufferLength != 0) { encodeByteBufferIntoOutput(); } this.output.close(); } private final int copyIntoByteBuffer(byte[] source, int startIndex, int endIndex) { int min = Math.min(3 - this.byteBufferLength, endIndex - startIndex); ArraysKt.copyInto(source, this.byteBuffer, this.byteBufferLength, startIndex, startIndex + min); int i = this.byteBufferLength + min; this.byteBufferLength = i; if (i == 3) { encodeByteBufferIntoOutput(); } return min; } private final void encodeByteBufferIntoOutput() { if (encodeIntoOutput(this.byteBuffer, 0, this.byteBufferLength) != 4) { throw new IllegalStateException("Check failed.".toString()); } this.byteBufferLength = 0; } private final int encodeIntoOutput(byte[] source, int startIndex, int endIndex) { int encodeIntoByteArray = this.base64.encodeIntoByteArray(source, this.symbolBuffer, 0, startIndex, endIndex); if (this.lineLength == 0) { this.output.write(Base64.INSTANCE.getMimeLineSeparatorSymbols$kotlin_stdlib()); this.lineLength = 76; if (encodeIntoByteArray > 76) { throw new IllegalStateException("Check failed.".toString()); } } this.output.write(this.symbolBuffer, 0, encodeIntoByteArray); this.lineLength -= encodeIntoByteArray; return encodeIntoByteArray; } private final void checkOpen() { if (this.isClosed) { throw new IOException("The output stream is closed."); } } }