package io.grpc; import com.google.android.gms.measurement.api.AppMeasurementSdk; import com.google.common.base.Charsets; import com.google.common.base.Preconditions; import com.google.common.io.BaseEncoding; import com.google.common.io.ByteStreams; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import java.util.NoSuchElementException; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public final class Metadata { static final boolean $assertionsDisabled = false; public static final String BINARY_HEADER_SUFFIX = "-bin"; private Object[] namesAndValues; private int size; private static final Logger logger = Logger.getLogger(Metadata.class.getName()); public static final BinaryMarshaller BINARY_BYTE_MARSHALLER = new BinaryMarshaller() { // from class: io.grpc.Metadata.1 @Override // io.grpc.Metadata.BinaryMarshaller public byte[] parseBytes(byte[] bArr) { return bArr; } @Override // io.grpc.Metadata.BinaryMarshaller public byte[] toBytes(byte[] bArr) { return bArr; } }; public static final AsciiMarshaller ASCII_STRING_MARSHALLER = new AsciiMarshaller() { // from class: io.grpc.Metadata.2 @Override // io.grpc.Metadata.AsciiMarshaller public String parseAsciiString(String str) { return str; } @Override // io.grpc.Metadata.AsciiMarshaller public String toAsciiString(String str) { return str; } }; static final BaseEncoding BASE64_ENCODING_OMIT_PADDING = BaseEncoding.base64().omitPadding(); /* loaded from: classes6.dex */ public interface AsciiMarshaller { T parseAsciiString(String str); String toAsciiString(T t); } /* loaded from: classes6.dex */ public interface BinaryMarshaller { T parseBytes(byte[] bArr); byte[] toBytes(T t); } /* loaded from: classes6.dex */ public interface BinaryStreamMarshaller { T parseStream(InputStream inputStream); InputStream toStream(T t); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public interface TrustedAsciiMarshaller { T parseAsciiString(byte[] bArr); byte[] toAsciiString(T t); } /* JADX INFO: Access modifiers changed from: package-private */ public Metadata(byte[]... bArr) { this(bArr.length / 2, bArr); } /* JADX INFO: Access modifiers changed from: package-private */ public Metadata(int i, byte[]... bArr) { this(i, (Object[]) bArr); } /* JADX INFO: Access modifiers changed from: package-private */ public Metadata(int i, Object[] objArr) { this.size = i; this.namesAndValues = objArr; } /* JADX INFO: Access modifiers changed from: private */ public byte[] name(int i) { return (byte[]) this.namesAndValues[i << 1]; } private void name(int i, byte[] bArr) { this.namesAndValues[i << 1] = bArr; } private Object value(int i) { return this.namesAndValues[(i << 1) + 1]; } private void value(int i, byte[] bArr) { this.namesAndValues[(i << 1) + 1] = bArr; } private void value(int i, Object obj) { if (this.namesAndValues instanceof byte[][]) { expand(cap()); } this.namesAndValues[(i << 1) + 1] = obj; } private byte[] valueAsBytes(int i) { Object value = value(i); if (value instanceof byte[]) { return (byte[]) value; } return ((LazyValue) value).toBytes(); } private Object valueAsBytesOrStream(int i) { Object value = value(i); return value instanceof byte[] ? value : ((LazyValue) value).toStream(); } /* JADX INFO: Access modifiers changed from: private */ public T valueAsT(int i, Key key) { Object value = value(i); if (value instanceof byte[]) { return key.parseBytes((byte[]) value); } return (T) ((LazyValue) value).toObject(key); } private int cap() { Object[] objArr = this.namesAndValues; if (objArr != null) { return objArr.length; } return 0; } public Metadata() { } public final boolean containsKey(Key key) { for (int i = 0; i < this.size; i++) { if (bytesEqual(key.asciiName(), name(i))) { return true; } } return false; } public final T get(Key key) { for (int i = this.size - 1; i >= 0; i--) { if (bytesEqual(key.asciiName(), name(i))) { return (T) valueAsT(i, key); } } return null; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class IterableAt implements Iterable { private final Key key; private int startIdx; final Metadata this$0; private IterableAt(Metadata metadata, Key key, int i) { this.this$0 = metadata; this.key = key; this.startIdx = i; } @Override // java.lang.Iterable public final Iterator iterator() { return new Iterator(this) { // from class: io.grpc.Metadata.IterableAt.1 private boolean hasNext = true; private int idx; final IterableAt this$1; { this.this$1 = this; this.idx = this.startIdx; } @Override // java.util.Iterator public boolean hasNext() { if (this.hasNext) { return true; } while (this.idx < this.this$1.this$0.size) { if (this.this$1.this$0.bytesEqual(this.this$1.key.asciiName(), this.this$1.this$0.name(this.idx))) { this.hasNext = true; return true; } this.idx++; } return false; } @Override // java.util.Iterator public T next() { if (hasNext()) { this.hasNext = false; Metadata metadata = this.this$1.this$0; int i = this.idx; this.idx = i + 1; return (T) metadata.valueAsT(i, this.this$1.key); } throw new NoSuchElementException(); } @Override // java.util.Iterator public void remove() { throw new UnsupportedOperationException(); } }; } } public final Iterable getAll(Key key) { int i = 0; while (true) { if (i >= this.size) { return null; } if (bytesEqual(key.asciiName(), name(i))) { return new IterableAt(key, i); } i++; } } public final Set keys() { if (isEmpty()) { return Collections.emptySet(); } HashSet hashSet = new HashSet(this.size); for (int i = 0; i < this.size; i++) { hashSet.add(new String(name(i), 0)); } return Collections.unmodifiableSet(hashSet); } public final void put(Key key, T t) { Preconditions.checkNotNull(key, "key"); Preconditions.checkNotNull(t, AppMeasurementSdk.ConditionalUserProperty.VALUE); maybeExpand(); name(this.size, key.asciiName()); if (key.serializesToStreams()) { value(this.size, LazyValue.create(key, t)); } else { value(this.size, key.toBytes(t)); } this.size++; } private void maybeExpand() { if (len() == 0 || len() == cap()) { expand(Math.max(len() << 1, 8)); } } private void expand(int i) { Object[] objArr = new Object[i]; if (!isEmpty()) { System.arraycopy(this.namesAndValues, 0, objArr, 0, len()); } this.namesAndValues = objArr; } public final boolean remove(Key key, T t) { Preconditions.checkNotNull(key, "key"); Preconditions.checkNotNull(t, AppMeasurementSdk.ConditionalUserProperty.VALUE); for (int i = 0; i < this.size; i++) { if (bytesEqual(key.asciiName(), name(i)) && t.equals(valueAsT(i, key))) { int i2 = (i + 1) << 1; int len = len(); Object[] objArr = this.namesAndValues; System.arraycopy(objArr, i2, objArr, i << 1, len - i2); int i3 = this.size - 1; this.size = i3; name(i3, null); value(this.size, (byte[]) null); return true; } } return false; } public final Iterable removeAll(Key key) { if (isEmpty()) { return null; } int i = 0; ArrayList arrayList = null; for (int i2 = 0; i2 < this.size; i2++) { if (bytesEqual(key.asciiName(), name(i2))) { if (arrayList == null) { arrayList = new ArrayList(); } arrayList.add(valueAsT(i2, key)); } else { name(i, name(i2)); value(i, value(i2)); i++; } } Arrays.fill(this.namesAndValues, i << 1, len(), (Object) null); this.size = i; return arrayList; } public final void discardAll(Key key) { if (isEmpty()) { return; } int i = 0; for (int i2 = 0; i2 < this.size; i2++) { if (!bytesEqual(key.asciiName(), name(i2))) { name(i, name(i2)); value(i, value(i2)); i++; } } Arrays.fill(this.namesAndValues, i << 1, len(), (Object) null); this.size = i; } /* JADX INFO: Access modifiers changed from: package-private */ public final byte[][] serialize() { byte[][] bArr = new byte[len()]; Object[] objArr = this.namesAndValues; if (objArr instanceof byte[][]) { System.arraycopy(objArr, 0, bArr, 0, len()); } else { for (int i = 0; i < this.size; i++) { int i2 = i << 1; bArr[i2] = name(i); bArr[i2 + 1] = valueAsBytes(i); } } return bArr; } /* JADX INFO: Access modifiers changed from: package-private */ public final Object[] serializePartial() { Object[] objArr = new Object[len()]; for (int i = 0; i < this.size; i++) { int i2 = i << 1; objArr[i2] = name(i); objArr[i2 + 1] = valueAsBytesOrStream(i); } return objArr; } public final void merge(Metadata metadata) { if (metadata.isEmpty()) { return; } int cap = cap(); int len = len(); if (isEmpty() || cap - len < metadata.len()) { expand(len() + metadata.len()); } System.arraycopy(metadata.namesAndValues, 0, this.namesAndValues, len(), metadata.len()); this.size += metadata.size; } public final void merge(Metadata metadata, Set> set) { Preconditions.checkNotNull(metadata, "other"); HashMap hashMap = new HashMap(set.size()); for (Key key : set) { hashMap.put(ByteBuffer.wrap(key.asciiName()), key); } for (int i = 0; i < metadata.size; i++) { if (hashMap.containsKey(ByteBuffer.wrap(metadata.name(i)))) { maybeExpand(); name(this.size, metadata.name(i)); value(this.size, metadata.value(i)); this.size++; } } } public final String toString() { StringBuilder sb = new StringBuilder("Metadata("); for (int i = 0; i < this.size; i++) { if (i != 0) { sb.append(','); } String str = new String(name(i), Charsets.US_ASCII); sb.append(str); sb.append('='); if (str.endsWith(BINARY_HEADER_SUFFIX)) { sb.append(BASE64_ENCODING_OMIT_PADDING.encode(valueAsBytes(i))); } else { sb.append(new String(valueAsBytes(i), Charsets.US_ASCII)); } } sb.append(')'); return sb.toString(); } /* JADX INFO: Access modifiers changed from: private */ public boolean bytesEqual(byte[] bArr, byte[] bArr2) { return Arrays.equals(bArr, bArr2); } /* loaded from: classes6.dex */ public static abstract class Key { private static final BitSet VALID_T_CHARS = generateValidTChars(); private final Object marshaller; private final String name; private final byte[] nameBytes; private final String originalName; abstract T parseBytes(byte[] bArr); boolean serializesToStreams() { return false; } abstract byte[] toBytes(T t); public static Key of(String str, BinaryMarshaller binaryMarshaller) { return new BinaryKey(str, binaryMarshaller); } public static Key of(String str, BinaryStreamMarshaller binaryStreamMarshaller) { return new LazyStreamBinaryKey(str, binaryStreamMarshaller); } public static Key of(String str, AsciiMarshaller asciiMarshaller) { return of(str, false, (AsciiMarshaller) asciiMarshaller); } /* JADX INFO: Access modifiers changed from: package-private */ public static Key of(String str, boolean z, AsciiMarshaller asciiMarshaller) { return new AsciiKey(str, z, asciiMarshaller); } /* JADX INFO: Access modifiers changed from: package-private */ public static Key of(String str, boolean z, TrustedAsciiMarshaller trustedAsciiMarshaller) { return new TrustedAsciiKey(str, z, trustedAsciiMarshaller); } private static BitSet generateValidTChars() { BitSet bitSet = new BitSet(127); bitSet.set(45); bitSet.set(95); bitSet.set(46); for (char c = '0'; c <= '9'; c = (char) (c + 1)) { bitSet.set(c); } for (char c2 = 'a'; c2 <= 'z'; c2 = (char) (c2 + 1)) { bitSet.set(c2); } return bitSet; } private static String validateName(String str, boolean z) { Preconditions.checkNotNull(str, "name"); Preconditions.checkArgument(!str.isEmpty(), "token must have at least 1 tchar"); if (str.equals("connection")) { Metadata.logger.log(Level.WARNING, "Metadata key is 'Connection', which should not be used. That is used by HTTP/1 for connection-specific headers which are not to be forwarded. There is probably an HTTP/1 conversion bug. Simply removing the Connection header is not enough; you should remove all headers it references as well. See RFC 7230 section 6.1", (Throwable) new RuntimeException("exception to show backtrace")); } for (int i = 0; i < str.length(); i++) { char charAt = str.charAt(i); if (!z || charAt != ':' || i != 0) { Preconditions.checkArgument(VALID_T_CHARS.get(charAt), "Invalid character '%s' in key name '%s'", charAt, (Object) str); } } return str; } private Key(String str, boolean z, Object obj) { String str2 = (String) Preconditions.checkNotNull(str, "name"); this.originalName = str2; String validateName = validateName(str2.toLowerCase(Locale.ROOT), z); this.name = validateName; this.nameBytes = validateName.getBytes(Charsets.US_ASCII); this.marshaller = obj; } public final boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } return this.name.equals(((Key) obj).name); } public final int hashCode() { return this.name.hashCode(); } public String toString() { StringBuilder sb = new StringBuilder("Key{name='"); sb.append(this.name); sb.append("'}"); return sb.toString(); } final M getMarshaller(Class cls) { if (cls.isInstance(this.marshaller)) { return cls.cast(this.marshaller); } return null; } public final String originalName() { return this.originalName; } public final String name() { return this.name; } byte[] asciiName() { return this.nameBytes; } } /* loaded from: classes6.dex */ static class BinaryKey extends Key { private final BinaryMarshaller marshaller; private BinaryKey(String str, BinaryMarshaller binaryMarshaller) { super(str, false, binaryMarshaller); Preconditions.checkArgument(str.endsWith(Metadata.BINARY_HEADER_SUFFIX), "Binary header is named %s. It must end with %s", str, Metadata.BINARY_HEADER_SUFFIX); Preconditions.checkArgument(str.length() > 4, "empty key name"); this.marshaller = (BinaryMarshaller) Preconditions.checkNotNull(binaryMarshaller, "marshaller is null"); } @Override // io.grpc.Metadata.Key byte[] toBytes(T t) { return this.marshaller.toBytes(t); } @Override // io.grpc.Metadata.Key T parseBytes(byte[] bArr) { return this.marshaller.parseBytes(bArr); } } /* loaded from: classes6.dex */ static class LazyStreamBinaryKey extends Key { private final BinaryStreamMarshaller marshaller; @Override // io.grpc.Metadata.Key boolean serializesToStreams() { return true; } private LazyStreamBinaryKey(String str, BinaryStreamMarshaller binaryStreamMarshaller) { super(str, false, binaryStreamMarshaller); Preconditions.checkArgument(str.endsWith(Metadata.BINARY_HEADER_SUFFIX), "Binary header is named %s. It must end with %s", str, Metadata.BINARY_HEADER_SUFFIX); Preconditions.checkArgument(str.length() > 4, "empty key name"); this.marshaller = (BinaryStreamMarshaller) Preconditions.checkNotNull(binaryStreamMarshaller, "marshaller is null"); } @Override // io.grpc.Metadata.Key byte[] toBytes(T t) { return Metadata.streamToBytes(this.marshaller.toStream(t)); } @Override // io.grpc.Metadata.Key T parseBytes(byte[] bArr) { return this.marshaller.parseStream(new ByteArrayInputStream(bArr)); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class LazyValue { private final BinaryStreamMarshaller marshaller; private volatile byte[] serialized; private final T value; static LazyValue create(Key key, T t) { return new LazyValue<>((BinaryStreamMarshaller) Preconditions.checkNotNull(getBinaryStreamMarshaller(key)), t); } /* JADX INFO: Access modifiers changed from: package-private */ public LazyValue(BinaryStreamMarshaller binaryStreamMarshaller, T t) { this.marshaller = binaryStreamMarshaller; this.value = t; } final InputStream toStream() { return this.marshaller.toStream(this.value); } final byte[] toBytes() { if (this.serialized == null) { synchronized (this) { if (this.serialized == null) { this.serialized = Metadata.streamToBytes(toStream()); } } } return this.serialized; } final T2 toObject(Key key) { BinaryStreamMarshaller binaryStreamMarshaller; if (key.serializesToStreams() && (binaryStreamMarshaller = getBinaryStreamMarshaller(key)) != null) { return (T2) binaryStreamMarshaller.parseStream(toStream()); } return key.parseBytes(toBytes()); } private static BinaryStreamMarshaller getBinaryStreamMarshaller(Key key) { return (BinaryStreamMarshaller) key.getMarshaller(BinaryStreamMarshaller.class); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static class AsciiKey extends Key { private final AsciiMarshaller marshaller; private AsciiKey(String str, boolean z, AsciiMarshaller asciiMarshaller) { super(str, z, asciiMarshaller); Preconditions.checkArgument(!str.endsWith(Metadata.BINARY_HEADER_SUFFIX), "ASCII header is named %s. Only binary headers may end with %s", str, Metadata.BINARY_HEADER_SUFFIX); this.marshaller = (AsciiMarshaller) Preconditions.checkNotNull(asciiMarshaller, "marshaller"); } @Override // io.grpc.Metadata.Key byte[] toBytes(T t) { return this.marshaller.toAsciiString(t).getBytes(Charsets.US_ASCII); } @Override // io.grpc.Metadata.Key T parseBytes(byte[] bArr) { return this.marshaller.parseAsciiString(new String(bArr, Charsets.US_ASCII)); } } /* loaded from: classes6.dex */ static final class TrustedAsciiKey extends Key { private final TrustedAsciiMarshaller marshaller; private TrustedAsciiKey(String str, boolean z, TrustedAsciiMarshaller trustedAsciiMarshaller) { super(str, z, trustedAsciiMarshaller); Preconditions.checkArgument(!str.endsWith(Metadata.BINARY_HEADER_SUFFIX), "ASCII header is named %s. Only binary headers may end with %s", str, Metadata.BINARY_HEADER_SUFFIX); this.marshaller = (TrustedAsciiMarshaller) Preconditions.checkNotNull(trustedAsciiMarshaller, "marshaller"); } @Override // io.grpc.Metadata.Key final byte[] toBytes(T t) { return this.marshaller.toAsciiString(t); } @Override // io.grpc.Metadata.Key final T parseBytes(byte[] bArr) { return this.marshaller.parseAsciiString(bArr); } } /* JADX INFO: Access modifiers changed from: private */ public static byte[] streamToBytes(InputStream inputStream) { try { return ByteStreams.toByteArray(inputStream); } catch (IOException e) { throw new RuntimeException("failure reading serialized stream", e); } } /* JADX INFO: Access modifiers changed from: package-private */ public final int headerCount() { return this.size; } private int len() { return this.size << 1; } private boolean isEmpty() { return this.size == 0; } }