what-the-bank/sources/com/google/common/io/BaseEncoding.java

1004 lines
40 KiB
Java

package com.google.common.io;
import com.google.common.base.Ascii;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.math.IntMath;
import com.google.common.primitives.UnsignedBytes;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.RoundingMode;
import java.util.Arrays;
/* loaded from: classes2.dex */
public abstract class BaseEncoding {
private static final BaseEncoding BASE64 = new Base64Encoding("base64()", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", '=');
private static final BaseEncoding BASE64_URL = new Base64Encoding("base64Url()", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", '=');
private static final BaseEncoding BASE32 = new StandardBaseEncoding("base32()", "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", '=');
private static final BaseEncoding BASE32_HEX = new StandardBaseEncoding("base32Hex()", "0123456789ABCDEFGHIJKLMNOPQRSTUV", '=');
private static final BaseEncoding BASE16 = new Base16Encoding("base16()", "0123456789ABCDEF");
public abstract boolean canDecode(CharSequence charSequence);
abstract int decodeTo(byte[] bArr, CharSequence charSequence) throws DecodingException;
public abstract InputStream decodingStream(Reader reader);
abstract void encodeTo(Appendable appendable, byte[] bArr, int i, int i2) throws IOException;
public abstract OutputStream encodingStream(Writer writer);
public abstract BaseEncoding lowerCase();
abstract int maxDecodedSize(int i);
abstract int maxEncodedSize(int i);
public abstract BaseEncoding omitPadding();
public abstract BaseEncoding upperCase();
public abstract BaseEncoding withPadChar(char c);
public abstract BaseEncoding withSeparator(String str, int i);
BaseEncoding() {
}
/* loaded from: classes2.dex */
public static final class DecodingException extends IOException {
public DecodingException(String str) {
super(str);
}
DecodingException(Throwable th) {
super(th);
}
}
public String encode(byte[] bArr) {
return encode(bArr, 0, bArr.length);
}
public final String encode(byte[] bArr, int i, int i2) {
Preconditions.checkPositionIndexes(i, i + i2, bArr.length);
StringBuilder sb = new StringBuilder(maxEncodedSize(i2));
try {
encodeTo(sb, bArr, i, i2);
return sb.toString();
} catch (IOException e) {
throw new AssertionError(e);
}
}
public final ByteSink encodingSink(CharSink charSink) {
Preconditions.checkNotNull(charSink);
return new ByteSink(this, charSink) { // from class: com.google.common.io.BaseEncoding.1
final BaseEncoding this$0;
final CharSink val$encodedSink;
{
this.this$0 = this;
this.val$encodedSink = charSink;
}
@Override // com.google.common.io.ByteSink
public OutputStream openStream() throws IOException {
return this.this$0.encodingStream(this.val$encodedSink.openStream());
}
};
}
private static byte[] extract(byte[] bArr, int i) {
if (i == bArr.length) {
return bArr;
}
byte[] bArr2 = new byte[i];
System.arraycopy(bArr, 0, bArr2, 0, i);
return bArr2;
}
public final byte[] decode(CharSequence charSequence) {
try {
return decodeChecked(charSequence);
} catch (DecodingException e) {
throw new IllegalArgumentException(e);
}
}
final byte[] decodeChecked(CharSequence charSequence) throws DecodingException {
CharSequence trimTrailingPadding = trimTrailingPadding(charSequence);
byte[] bArr = new byte[maxDecodedSize(trimTrailingPadding.length())];
return extract(bArr, decodeTo(bArr, trimTrailingPadding));
}
public final ByteSource decodingSource(CharSource charSource) {
Preconditions.checkNotNull(charSource);
return new ByteSource(this, charSource) { // from class: com.google.common.io.BaseEncoding.2
final BaseEncoding this$0;
final CharSource val$encodedSource;
{
this.this$0 = this;
this.val$encodedSource = charSource;
}
@Override // com.google.common.io.ByteSource
public InputStream openStream() throws IOException {
return this.this$0.decodingStream(this.val$encodedSource.openStream());
}
};
}
CharSequence trimTrailingPadding(CharSequence charSequence) {
return (CharSequence) Preconditions.checkNotNull(charSequence);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class Alphabet {
final int bitsPerChar;
final int bytesPerChunk;
private final char[] chars;
final int charsPerChunk;
private final byte[] decodabet;
final int mask;
private final String name;
private final boolean[] validPadding;
Alphabet(String str, char[] cArr) {
this.name = (String) Preconditions.checkNotNull(str);
this.chars = (char[]) Preconditions.checkNotNull(cArr);
try {
int log2 = IntMath.log2(cArr.length, RoundingMode.UNNECESSARY);
this.bitsPerChar = log2;
int min = Math.min(8, Integer.lowestOneBit(log2));
try {
this.charsPerChunk = 8 / min;
this.bytesPerChunk = log2 / min;
this.mask = cArr.length - 1;
byte[] bArr = new byte[128];
Arrays.fill(bArr, (byte) -1);
for (int i = 0; i < cArr.length; i++) {
char c = cArr[i];
Preconditions.checkArgument(c < 128, "Non-ASCII character: %s", c);
Preconditions.checkArgument(bArr[c] == -1, "Duplicate character: %s", c);
bArr[c] = (byte) i;
}
this.decodabet = bArr;
boolean[] zArr = new boolean[this.charsPerChunk];
for (int i2 = 0; i2 < this.bytesPerChunk; i2++) {
zArr[IntMath.divide(i2 << 3, this.bitsPerChar, RoundingMode.CEILING)] = true;
}
this.validPadding = zArr;
} catch (ArithmeticException e) {
String str2 = new String(cArr);
throw new IllegalArgumentException(str2.length() != 0 ? "Illegal alphabet ".concat(str2) : new String("Illegal alphabet "), e);
}
} catch (ArithmeticException e2) {
int length = cArr.length;
StringBuilder sb = new StringBuilder(35);
sb.append("Illegal alphabet length ");
sb.append(length);
throw new IllegalArgumentException(sb.toString(), e2);
}
}
final char encode(int i) {
return this.chars[i];
}
final boolean isValidPaddingStartPosition(int i) {
return this.validPadding[i % this.charsPerChunk];
}
final boolean canDecode(char c) {
return c <= 127 && this.decodabet[c] != -1;
}
final int decode(char c) throws DecodingException {
if (c > 127) {
String valueOf = String.valueOf(Integer.toHexString(c));
throw new DecodingException(valueOf.length() != 0 ? "Unrecognized character: 0x".concat(valueOf) : new String("Unrecognized character: 0x"));
}
byte b = this.decodabet[c];
if (b != -1) {
return b;
}
if (c <= ' ' || c == 127) {
String valueOf2 = String.valueOf(Integer.toHexString(c));
throw new DecodingException(valueOf2.length() != 0 ? "Unrecognized character: 0x".concat(valueOf2) : new String("Unrecognized character: 0x"));
}
StringBuilder sb = new StringBuilder(25);
sb.append("Unrecognized character: ");
sb.append(c);
throw new DecodingException(sb.toString());
}
private boolean hasLowerCase() {
for (char c : this.chars) {
if (Ascii.isLowerCase(c)) {
return true;
}
}
return false;
}
private boolean hasUpperCase() {
for (char c : this.chars) {
if (Ascii.isUpperCase(c)) {
return true;
}
}
return false;
}
final Alphabet upperCase() {
if (!hasLowerCase()) {
return this;
}
Preconditions.checkState(!hasUpperCase(), "Cannot call upperCase() on a mixed-case alphabet");
char[] cArr = new char[this.chars.length];
int i = 0;
while (true) {
char[] cArr2 = this.chars;
if (i < cArr2.length) {
cArr[i] = Ascii.toUpperCase(cArr2[i]);
i++;
} else {
return new Alphabet(String.valueOf(this.name).concat(".upperCase()"), cArr);
}
}
}
final Alphabet lowerCase() {
if (!hasUpperCase()) {
return this;
}
Preconditions.checkState(!hasLowerCase(), "Cannot call lowerCase() on a mixed-case alphabet");
char[] cArr = new char[this.chars.length];
int i = 0;
while (true) {
char[] cArr2 = this.chars;
if (i < cArr2.length) {
cArr[i] = Ascii.toLowerCase(cArr2[i]);
i++;
} else {
return new Alphabet(String.valueOf(this.name).concat(".lowerCase()"), cArr);
}
}
}
public final boolean matches(char c) {
byte[] bArr = this.decodabet;
return c < bArr.length && bArr[c] != -1;
}
public final boolean equals(Object obj) {
if (obj instanceof Alphabet) {
return Arrays.equals(this.chars, ((Alphabet) obj).chars);
}
return false;
}
public final int hashCode() {
return Arrays.hashCode(this.chars);
}
public final String toString() {
return this.name;
}
}
/* loaded from: classes2.dex */
static class StandardBaseEncoding extends BaseEncoding {
final Alphabet alphabet;
private transient BaseEncoding lowerCase;
final Character paddingChar;
private transient BaseEncoding upperCase;
StandardBaseEncoding(String str, String str2, Character ch) {
this(new Alphabet(str, str2.toCharArray()), ch);
}
StandardBaseEncoding(Alphabet alphabet, Character ch) {
this.alphabet = (Alphabet) Preconditions.checkNotNull(alphabet);
Preconditions.checkArgument(ch == null || !alphabet.matches(ch.charValue()), "Padding character %s was already in alphabet", ch);
this.paddingChar = ch;
}
@Override // com.google.common.io.BaseEncoding
int maxEncodedSize(int i) {
return this.alphabet.charsPerChunk * IntMath.divide(i, this.alphabet.bytesPerChunk, RoundingMode.CEILING);
}
@Override // com.google.common.io.BaseEncoding
public OutputStream encodingStream(Writer writer) {
Preconditions.checkNotNull(writer);
return new OutputStream(this, writer) { // from class: com.google.common.io.BaseEncoding.StandardBaseEncoding.1
final StandardBaseEncoding this$0;
final Writer val$out;
int bitBuffer = 0;
int bitBufferLength = 0;
int writtenChars = 0;
{
this.this$0 = this;
this.val$out = writer;
}
@Override // java.io.OutputStream
public void write(int i) throws IOException {
this.bitBuffer = (i & 255) | (this.bitBuffer << 8);
this.bitBufferLength += 8;
while (this.bitBufferLength >= this.this$0.alphabet.bitsPerChar) {
this.val$out.write(this.this$0.alphabet.encode((this.bitBuffer >> (this.bitBufferLength - this.this$0.alphabet.bitsPerChar)) & this.this$0.alphabet.mask));
this.writtenChars++;
this.bitBufferLength -= this.this$0.alphabet.bitsPerChar;
}
}
@Override // java.io.OutputStream, java.io.Flushable
public void flush() throws IOException {
this.val$out.flush();
}
@Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
if (this.bitBufferLength > 0) {
this.val$out.write(this.this$0.alphabet.encode((this.bitBuffer << (this.this$0.alphabet.bitsPerChar - this.bitBufferLength)) & this.this$0.alphabet.mask));
this.writtenChars++;
if (this.this$0.paddingChar != null) {
while (this.writtenChars % this.this$0.alphabet.charsPerChunk != 0) {
this.val$out.write(this.this$0.paddingChar.charValue());
this.writtenChars++;
}
}
}
this.val$out.close();
}
};
}
@Override // com.google.common.io.BaseEncoding
void encodeTo(Appendable appendable, byte[] bArr, int i, int i2) throws IOException {
Preconditions.checkNotNull(appendable);
Preconditions.checkPositionIndexes(i, i + i2, bArr.length);
int i3 = 0;
while (i3 < i2) {
encodeChunkTo(appendable, bArr, i + i3, Math.min(this.alphabet.bytesPerChunk, i2 - i3));
i3 += this.alphabet.bytesPerChunk;
}
}
void encodeChunkTo(Appendable appendable, byte[] bArr, int i, int i2) throws IOException {
Preconditions.checkNotNull(appendable);
Preconditions.checkPositionIndexes(i, i + i2, bArr.length);
int i3 = 0;
Preconditions.checkArgument(i2 <= this.alphabet.bytesPerChunk);
long j = 0;
for (int i4 = 0; i4 < i2; i4++) {
j = (j | (bArr[i + i4] & UnsignedBytes.MAX_VALUE)) << 8;
}
int i5 = this.alphabet.bitsPerChar;
while (i3 < (i2 << 3)) {
appendable.append(this.alphabet.encode(((int) (j >>> ((((i2 + 1) << 3) - i5) - i3))) & this.alphabet.mask));
i3 += this.alphabet.bitsPerChar;
}
if (this.paddingChar != null) {
while (i3 < (this.alphabet.bytesPerChunk << 3)) {
appendable.append(this.paddingChar.charValue());
i3 += this.alphabet.bitsPerChar;
}
}
}
@Override // com.google.common.io.BaseEncoding
int maxDecodedSize(int i) {
return (int) (((this.alphabet.bitsPerChar * i) + 7) / 8);
}
@Override // com.google.common.io.BaseEncoding
CharSequence trimTrailingPadding(CharSequence charSequence) {
Preconditions.checkNotNull(charSequence);
Character ch = this.paddingChar;
if (ch == null) {
return charSequence;
}
char charValue = ch.charValue();
int length = charSequence.length() - 1;
while (length >= 0 && charSequence.charAt(length) == charValue) {
length--;
}
return charSequence.subSequence(0, length + 1);
}
@Override // com.google.common.io.BaseEncoding
public boolean canDecode(CharSequence charSequence) {
Preconditions.checkNotNull(charSequence);
CharSequence trimTrailingPadding = trimTrailingPadding(charSequence);
if (!this.alphabet.isValidPaddingStartPosition(trimTrailingPadding.length())) {
return false;
}
for (int i = 0; i < trimTrailingPadding.length(); i++) {
if (!this.alphabet.canDecode(trimTrailingPadding.charAt(i))) {
return false;
}
}
return true;
}
@Override // com.google.common.io.BaseEncoding
int decodeTo(byte[] bArr, CharSequence charSequence) throws DecodingException {
Preconditions.checkNotNull(bArr);
CharSequence trimTrailingPadding = trimTrailingPadding(charSequence);
if (!this.alphabet.isValidPaddingStartPosition(trimTrailingPadding.length())) {
int length = trimTrailingPadding.length();
StringBuilder sb = new StringBuilder(32);
sb.append("Invalid input length ");
sb.append(length);
throw new DecodingException(sb.toString());
}
int i = 0;
int i2 = 0;
while (i < trimTrailingPadding.length()) {
long j = 0;
int i3 = 0;
for (int i4 = 0; i4 < this.alphabet.charsPerChunk; i4++) {
j <<= this.alphabet.bitsPerChar;
if (i + i4 < trimTrailingPadding.length()) {
j |= this.alphabet.decode(trimTrailingPadding.charAt(i3 + i));
i3++;
}
}
int i5 = this.alphabet.bytesPerChunk;
int i6 = this.alphabet.bitsPerChar;
int i7 = (this.alphabet.bytesPerChunk - 1) << 3;
while (i7 >= (i5 << 3) - (i3 * i6)) {
bArr[i2] = (byte) ((j >>> i7) & 255);
i7 -= 8;
i2++;
}
i += this.alphabet.charsPerChunk;
}
return i2;
}
@Override // com.google.common.io.BaseEncoding
public InputStream decodingStream(Reader reader) {
Preconditions.checkNotNull(reader);
return new InputStream(this, reader) { // from class: com.google.common.io.BaseEncoding.StandardBaseEncoding.2
final StandardBaseEncoding this$0;
final Reader val$reader;
int bitBuffer = 0;
int bitBufferLength = 0;
int readChars = 0;
boolean hitPadding = false;
{
this.this$0 = this;
this.val$reader = reader;
}
@Override // java.io.InputStream
public int read() throws IOException {
while (true) {
int read = this.val$reader.read();
if (read == -1) {
if (this.hitPadding || this.this$0.alphabet.isValidPaddingStartPosition(this.readChars)) {
return -1;
}
int i = this.readChars;
StringBuilder sb = new StringBuilder(32);
sb.append("Invalid input length ");
sb.append(i);
throw new DecodingException(sb.toString());
}
this.readChars++;
char c = (char) read;
if (this.this$0.paddingChar == null || this.this$0.paddingChar.charValue() != c) {
if (this.hitPadding) {
int i2 = this.readChars;
StringBuilder sb2 = new StringBuilder(61);
sb2.append("Expected padding character but found '");
sb2.append(c);
sb2.append("' at index ");
sb2.append(i2);
throw new DecodingException(sb2.toString());
}
int i3 = this.bitBuffer << this.this$0.alphabet.bitsPerChar;
this.bitBuffer = i3;
this.bitBuffer = this.this$0.alphabet.decode(c) | i3;
int i4 = this.bitBufferLength + this.this$0.alphabet.bitsPerChar;
this.bitBufferLength = i4;
if (i4 >= 8) {
int i5 = i4 - 8;
this.bitBufferLength = i5;
return (this.bitBuffer >> i5) & 255;
}
} else if (this.hitPadding || (this.readChars != 1 && this.this$0.alphabet.isValidPaddingStartPosition(this.readChars - 1))) {
this.hitPadding = true;
}
}
int i6 = this.readChars;
StringBuilder sb3 = new StringBuilder(41);
sb3.append("Padding cannot start at index ");
sb3.append(i6);
throw new DecodingException(sb3.toString());
}
@Override // java.io.InputStream
public int read(byte[] bArr, int i, int i2) throws IOException {
int i3 = i2 + i;
Preconditions.checkPositionIndexes(i, i3, bArr.length);
int i4 = i;
while (i4 < i3) {
int read = read();
if (read == -1) {
int i5 = i4 - i;
if (i5 == 0) {
return -1;
}
return i5;
}
bArr[i4] = (byte) read;
i4++;
}
return i4 - i;
}
@Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
this.val$reader.close();
}
};
}
@Override // com.google.common.io.BaseEncoding
public BaseEncoding omitPadding() {
return this.paddingChar == null ? this : newInstance(this.alphabet, null);
}
@Override // com.google.common.io.BaseEncoding
public BaseEncoding withPadChar(char c) {
Character ch;
return (8 % this.alphabet.bitsPerChar == 0 || ((ch = this.paddingChar) != null && ch.charValue() == c)) ? this : newInstance(this.alphabet, Character.valueOf(c));
}
@Override // com.google.common.io.BaseEncoding
public BaseEncoding withSeparator(String str, int i) {
for (int i2 = 0; i2 < str.length(); i2++) {
Preconditions.checkArgument(!this.alphabet.matches(str.charAt(i2)), "Separator (%s) cannot contain alphabet characters", str);
}
Character ch = this.paddingChar;
if (ch != null) {
Preconditions.checkArgument(str.indexOf(ch.charValue()) < 0, "Separator (%s) cannot contain padding character", str);
}
return new SeparatedBaseEncoding(this, str, i);
}
@Override // com.google.common.io.BaseEncoding
public BaseEncoding upperCase() {
BaseEncoding baseEncoding = this.upperCase;
if (baseEncoding == null) {
Alphabet upperCase = this.alphabet.upperCase();
baseEncoding = upperCase == this.alphabet ? this : newInstance(upperCase, this.paddingChar);
this.upperCase = baseEncoding;
}
return baseEncoding;
}
@Override // com.google.common.io.BaseEncoding
public BaseEncoding lowerCase() {
BaseEncoding baseEncoding = this.lowerCase;
if (baseEncoding == null) {
Alphabet lowerCase = this.alphabet.lowerCase();
baseEncoding = lowerCase == this.alphabet ? this : newInstance(lowerCase, this.paddingChar);
this.lowerCase = baseEncoding;
}
return baseEncoding;
}
BaseEncoding newInstance(Alphabet alphabet, Character ch) {
return new StandardBaseEncoding(alphabet, ch);
}
public String toString() {
StringBuilder sb = new StringBuilder("BaseEncoding.");
sb.append(this.alphabet.toString());
if (8 % this.alphabet.bitsPerChar != 0) {
if (this.paddingChar == null) {
sb.append(".omitPadding()");
} else {
sb.append(".withPadChar('");
sb.append(this.paddingChar);
sb.append("')");
}
}
return sb.toString();
}
public boolean equals(Object obj) {
if (!(obj instanceof StandardBaseEncoding)) {
return false;
}
StandardBaseEncoding standardBaseEncoding = (StandardBaseEncoding) obj;
return this.alphabet.equals(standardBaseEncoding.alphabet) && Objects.equal(this.paddingChar, standardBaseEncoding.paddingChar);
}
public int hashCode() {
return this.alphabet.hashCode() ^ Objects.hashCode(this.paddingChar);
}
}
/* loaded from: classes2.dex */
static final class Base16Encoding extends StandardBaseEncoding {
final char[] encoding;
Base16Encoding(String str, String str2) {
this(new Alphabet(str, str2.toCharArray()));
}
private Base16Encoding(Alphabet alphabet) {
super(alphabet, null);
this.encoding = new char[512];
Preconditions.checkArgument(alphabet.chars.length == 16);
for (int i = 0; i < 256; i++) {
this.encoding[i] = alphabet.encode(i >>> 4);
this.encoding[i | 256] = alphabet.encode(i & 15);
}
}
@Override // com.google.common.io.BaseEncoding.StandardBaseEncoding, com.google.common.io.BaseEncoding
final void encodeTo(Appendable appendable, byte[] bArr, int i, int i2) throws IOException {
Preconditions.checkNotNull(appendable);
Preconditions.checkPositionIndexes(i, i + i2, bArr.length);
for (int i3 = 0; i3 < i2; i3++) {
int i4 = bArr[i + i3] & UnsignedBytes.MAX_VALUE;
appendable.append(this.encoding[i4]);
appendable.append(this.encoding[i4 | 256]);
}
}
@Override // com.google.common.io.BaseEncoding.StandardBaseEncoding, com.google.common.io.BaseEncoding
final int decodeTo(byte[] bArr, CharSequence charSequence) throws DecodingException {
Preconditions.checkNotNull(bArr);
if (charSequence.length() % 2 == 1) {
int length = charSequence.length();
StringBuilder sb = new StringBuilder(32);
sb.append("Invalid input length ");
sb.append(length);
throw new DecodingException(sb.toString());
}
int i = 0;
int i2 = 0;
while (i < charSequence.length()) {
bArr[i2] = (byte) ((this.alphabet.decode(charSequence.charAt(i)) << 4) | this.alphabet.decode(charSequence.charAt(i + 1)));
i += 2;
i2++;
}
return i2;
}
@Override // com.google.common.io.BaseEncoding.StandardBaseEncoding
final BaseEncoding newInstance(Alphabet alphabet, Character ch) {
return new Base16Encoding(alphabet);
}
}
/* loaded from: classes2.dex */
static final class Base64Encoding extends StandardBaseEncoding {
Base64Encoding(String str, String str2, Character ch) {
this(new Alphabet(str, str2.toCharArray()), ch);
}
private Base64Encoding(Alphabet alphabet, Character ch) {
super(alphabet, ch);
Preconditions.checkArgument(alphabet.chars.length == 64);
}
@Override // com.google.common.io.BaseEncoding.StandardBaseEncoding, com.google.common.io.BaseEncoding
final void encodeTo(Appendable appendable, byte[] bArr, int i, int i2) throws IOException {
Preconditions.checkNotNull(appendable);
int i3 = i + i2;
Preconditions.checkPositionIndexes(i, i3, bArr.length);
while (i2 >= 3) {
int i4 = i + 3;
int i5 = (bArr[i + 2] & UnsignedBytes.MAX_VALUE) | ((bArr[i] & UnsignedBytes.MAX_VALUE) << 16) | ((bArr[i + 1] & UnsignedBytes.MAX_VALUE) << 8);
appendable.append(this.alphabet.encode(i5 >>> 18));
appendable.append(this.alphabet.encode((i5 >>> 12) & 63));
appendable.append(this.alphabet.encode((i5 >>> 6) & 63));
appendable.append(this.alphabet.encode(i5 & 63));
i2 -= 3;
i = i4;
}
if (i < i3) {
encodeChunkTo(appendable, bArr, i, i3 - i);
}
}
@Override // com.google.common.io.BaseEncoding.StandardBaseEncoding, com.google.common.io.BaseEncoding
final int decodeTo(byte[] bArr, CharSequence charSequence) throws DecodingException {
Preconditions.checkNotNull(bArr);
CharSequence trimTrailingPadding = trimTrailingPadding(charSequence);
if (!this.alphabet.isValidPaddingStartPosition(trimTrailingPadding.length())) {
int length = trimTrailingPadding.length();
StringBuilder sb = new StringBuilder(32);
sb.append("Invalid input length ");
sb.append(length);
throw new DecodingException(sb.toString());
}
int i = 0;
int i2 = 0;
while (i < trimTrailingPadding.length()) {
int i3 = i + 2;
int decode = (this.alphabet.decode(trimTrailingPadding.charAt(i)) << 18) | (this.alphabet.decode(trimTrailingPadding.charAt(i + 1)) << 12);
int i4 = i2 + 1;
bArr[i2] = (byte) (decode >>> 16);
if (i3 < trimTrailingPadding.length()) {
int i5 = i + 3;
int decode2 = decode | (this.alphabet.decode(trimTrailingPadding.charAt(i3)) << 6);
int i6 = i2 + 2;
bArr[i4] = (byte) (decode2 >>> 8);
if (i5 < trimTrailingPadding.length()) {
i += 4;
i2 += 3;
bArr[i6] = (byte) (decode2 | this.alphabet.decode(trimTrailingPadding.charAt(i5)));
} else {
i2 = i6;
i = i5;
}
} else {
i2 = i4;
i = i3;
}
}
return i2;
}
@Override // com.google.common.io.BaseEncoding.StandardBaseEncoding
final BaseEncoding newInstance(Alphabet alphabet, Character ch) {
return new Base64Encoding(alphabet, ch);
}
}
static Reader ignoringReader(Reader reader, String str) {
Preconditions.checkNotNull(reader);
Preconditions.checkNotNull(str);
return new Reader(reader, str) { // from class: com.google.common.io.BaseEncoding.3
final Reader val$delegate;
final String val$toIgnore;
{
this.val$delegate = reader;
this.val$toIgnore = str;
}
@Override // java.io.Reader
public int read() throws IOException {
int read;
do {
read = this.val$delegate.read();
if (read == -1) {
break;
}
} while (this.val$toIgnore.indexOf((char) read) >= 0);
return read;
}
@Override // java.io.Reader
public int read(char[] cArr, int i, int i2) throws IOException {
throw new UnsupportedOperationException();
}
@Override // java.io.Reader, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
this.val$delegate.close();
}
};
}
static Appendable separatingAppendable(Appendable appendable, String str, int i) {
Preconditions.checkNotNull(appendable);
Preconditions.checkNotNull(str);
Preconditions.checkArgument(i > 0);
return new Appendable(i, appendable, str) { // from class: com.google.common.io.BaseEncoding.4
int charsUntilSeparator;
final int val$afterEveryChars;
final Appendable val$delegate;
final String val$separator;
{
this.val$afterEveryChars = i;
this.val$delegate = appendable;
this.val$separator = str;
this.charsUntilSeparator = i;
}
@Override // java.lang.Appendable
public Appendable append(char c) throws IOException {
if (this.charsUntilSeparator == 0) {
this.val$delegate.append(this.val$separator);
this.charsUntilSeparator = this.val$afterEveryChars;
}
this.val$delegate.append(c);
this.charsUntilSeparator--;
return this;
}
@Override // java.lang.Appendable
public Appendable append(CharSequence charSequence, int i2, int i3) throws IOException {
throw new UnsupportedOperationException();
}
@Override // java.lang.Appendable
public Appendable append(CharSequence charSequence) throws IOException {
throw new UnsupportedOperationException();
}
};
}
static Writer separatingWriter(Writer writer, String str, int i) {
return new Writer(separatingAppendable(writer, str, i), writer) { // from class: com.google.common.io.BaseEncoding.5
final Writer val$delegate;
final Appendable val$separatingAppendable;
{
this.val$separatingAppendable = r1;
this.val$delegate = writer;
}
@Override // java.io.Writer
public void write(int i2) throws IOException {
this.val$separatingAppendable.append((char) i2);
}
@Override // java.io.Writer
public void write(char[] cArr, int i2, int i3) throws IOException {
throw new UnsupportedOperationException();
}
@Override // java.io.Writer, java.io.Flushable
public void flush() throws IOException {
this.val$delegate.flush();
}
@Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
this.val$delegate.close();
}
};
}
/* loaded from: classes2.dex */
static final class SeparatedBaseEncoding extends BaseEncoding {
private final int afterEveryChars;
private final BaseEncoding delegate;
private final String separator;
SeparatedBaseEncoding(BaseEncoding baseEncoding, String str, int i) {
this.delegate = (BaseEncoding) Preconditions.checkNotNull(baseEncoding);
this.separator = (String) Preconditions.checkNotNull(str);
this.afterEveryChars = i;
Preconditions.checkArgument(i > 0, "Cannot add a separator after every %s chars", i);
}
@Override // com.google.common.io.BaseEncoding
final CharSequence trimTrailingPadding(CharSequence charSequence) {
return this.delegate.trimTrailingPadding(charSequence);
}
@Override // com.google.common.io.BaseEncoding
final int maxEncodedSize(int i) {
int maxEncodedSize = this.delegate.maxEncodedSize(i);
return maxEncodedSize + (this.separator.length() * IntMath.divide(Math.max(0, maxEncodedSize - 1), this.afterEveryChars, RoundingMode.FLOOR));
}
@Override // com.google.common.io.BaseEncoding
public final OutputStream encodingStream(Writer writer) {
return this.delegate.encodingStream(separatingWriter(writer, this.separator, this.afterEveryChars));
}
@Override // com.google.common.io.BaseEncoding
final void encodeTo(Appendable appendable, byte[] bArr, int i, int i2) throws IOException {
this.delegate.encodeTo(separatingAppendable(appendable, this.separator, this.afterEveryChars), bArr, i, i2);
}
@Override // com.google.common.io.BaseEncoding
final int maxDecodedSize(int i) {
return this.delegate.maxDecodedSize(i);
}
@Override // com.google.common.io.BaseEncoding
public final boolean canDecode(CharSequence charSequence) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charSequence.length(); i++) {
char charAt = charSequence.charAt(i);
if (this.separator.indexOf(charAt) < 0) {
sb.append(charAt);
}
}
return this.delegate.canDecode(sb);
}
@Override // com.google.common.io.BaseEncoding
final int decodeTo(byte[] bArr, CharSequence charSequence) throws DecodingException {
StringBuilder sb = new StringBuilder(charSequence.length());
for (int i = 0; i < charSequence.length(); i++) {
char charAt = charSequence.charAt(i);
if (this.separator.indexOf(charAt) < 0) {
sb.append(charAt);
}
}
return this.delegate.decodeTo(bArr, sb);
}
@Override // com.google.common.io.BaseEncoding
public final InputStream decodingStream(Reader reader) {
return this.delegate.decodingStream(ignoringReader(reader, this.separator));
}
@Override // com.google.common.io.BaseEncoding
public final BaseEncoding omitPadding() {
return this.delegate.omitPadding().withSeparator(this.separator, this.afterEveryChars);
}
@Override // com.google.common.io.BaseEncoding
public final BaseEncoding withPadChar(char c) {
return this.delegate.withPadChar(c).withSeparator(this.separator, this.afterEveryChars);
}
@Override // com.google.common.io.BaseEncoding
public final BaseEncoding withSeparator(String str, int i) {
throw new UnsupportedOperationException("Already have a separator");
}
@Override // com.google.common.io.BaseEncoding
public final BaseEncoding upperCase() {
return this.delegate.upperCase().withSeparator(this.separator, this.afterEveryChars);
}
@Override // com.google.common.io.BaseEncoding
public final BaseEncoding lowerCase() {
return this.delegate.lowerCase().withSeparator(this.separator, this.afterEveryChars);
}
public final String toString() {
String valueOf = String.valueOf(this.delegate);
String str = this.separator;
int i = this.afterEveryChars;
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 31 + String.valueOf(str).length());
sb.append(valueOf);
sb.append(".withSeparator(\"");
sb.append(str);
sb.append("\", ");
sb.append(i);
sb.append(")");
return sb.toString();
}
}
public static BaseEncoding base64Url() {
return BASE64_URL;
}
public static BaseEncoding base64() {
return BASE64;
}
public static BaseEncoding base32Hex() {
return BASE32_HEX;
}
public static BaseEncoding base32() {
return BASE32;
}
public static BaseEncoding base16() {
return BASE16;
}
}