alexandroid/decompiled/jadx/sources/com/google/common/io/ByteSource.java
KaKi87 a47fad5135 📦 Add decompiled Alexa 2.2.730155.0 (jadx, apktool, Hermes strings)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 00:04:16 +01:00

436 lines
16 KiB
Java

package com.google.common.io;
import androidx.collection.SieveCacheKt;
import com.amazon.alexa.ra.core.ui.markdown.parser.formatter.MarkdownConstants;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.hash.Funnels;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hasher;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import defpackage.a$$ExternalSyntheticOutline0;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
@J2ktIncompatible
@GwtIncompatible
/* loaded from: classes8.dex */
public abstract class ByteSource {
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public class AsCharSource extends CharSource {
final Charset charset;
public AsCharSource(Charset charset) {
this.charset = (Charset) Preconditions.checkNotNull(charset);
}
@Override // com.google.common.io.CharSource
public ByteSource asByteSource(Charset charset) {
return charset.equals(this.charset) ? ByteSource.this : super.asByteSource(charset);
}
@Override // com.google.common.io.CharSource
public Reader openStream() throws IOException {
return new InputStreamReader(ByteSource.this.openStream(), this.charset);
}
@Override // com.google.common.io.CharSource
public String read() throws IOException {
return new String(ByteSource.this.read(), this.charset);
}
public String toString() {
return ByteSource.this.toString() + ".asCharSource(" + this.charset + MarkdownConstants.RIGHT_PARENTHESIS;
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public static final class ConcatenatedByteSource extends ByteSource {
final Iterable<? extends ByteSource> sources;
public ConcatenatedByteSource(Iterable<? extends ByteSource> iterable) {
this.sources = (Iterable) Preconditions.checkNotNull(iterable);
}
@Override // com.google.common.io.ByteSource
public boolean isEmpty() throws IOException {
Iterator<? extends ByteSource> it2 = this.sources.iterator();
while (it2.hasNext()) {
if (!it2.next().isEmpty()) {
return false;
}
}
return true;
}
@Override // com.google.common.io.ByteSource
public InputStream openStream() throws IOException {
return new MultiInputStream(this.sources.iterator());
}
@Override // com.google.common.io.ByteSource
public long size() throws IOException {
Iterator<? extends ByteSource> it2 = this.sources.iterator();
long j = 0;
while (it2.hasNext()) {
j += it2.next().size();
if (j < 0) {
return Long.MAX_VALUE;
}
}
return j;
}
@Override // com.google.common.io.ByteSource
public Optional<Long> sizeIfKnown() {
Iterable<? extends ByteSource> iterable = this.sources;
if (!(iterable instanceof Collection)) {
return Optional.absent();
}
Iterator<? extends ByteSource> it2 = iterable.iterator();
long j = 0;
while (it2.hasNext()) {
Optional<Long> sizeIfKnown = it2.next().sizeIfKnown();
if (!sizeIfKnown.isPresent()) {
return Optional.absent();
}
j += sizeIfKnown.get().longValue();
if (j < 0) {
return Optional.of(Long.MAX_VALUE);
}
}
return Optional.of(Long.valueOf(j));
}
public String toString() {
return "ByteSource.concat(" + this.sources + MarkdownConstants.RIGHT_PARENTHESIS;
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public static final class EmptyByteSource extends ByteArrayByteSource {
static final EmptyByteSource INSTANCE = new EmptyByteSource();
public EmptyByteSource() {
super(new byte[0]);
}
@Override // com.google.common.io.ByteSource
public CharSource asCharSource(Charset charset) {
Preconditions.checkNotNull(charset);
return CharSource.empty();
}
@Override // com.google.common.io.ByteSource.ByteArrayByteSource, com.google.common.io.ByteSource
public byte[] read() {
return this.bytes;
}
@Override // com.google.common.io.ByteSource.ByteArrayByteSource
public String toString() {
return "ByteSource.empty()";
}
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public final class SlicedByteSource extends ByteSource {
final long length;
final long offset;
public SlicedByteSource(long j, long j2) {
Preconditions.checkArgument(j >= 0, "offset (%s) may not be negative", j);
Preconditions.checkArgument(j2 >= 0, "length (%s) may not be negative", j2);
this.offset = j;
this.length = j2;
}
private InputStream sliceStream(InputStream inputStream) throws IOException {
long j = this.offset;
if (j > 0) {
try {
if (ByteStreams.skipUpTo(inputStream, j) < this.offset) {
inputStream.close();
return new ByteArrayInputStream(new byte[0]);
}
} finally {
}
}
return ByteStreams.limit(inputStream, this.length);
}
@Override // com.google.common.io.ByteSource
public boolean isEmpty() throws IOException {
return this.length == 0 || super.isEmpty();
}
@Override // com.google.common.io.ByteSource
public InputStream openBufferedStream() throws IOException {
return sliceStream(ByteSource.this.openBufferedStream());
}
@Override // com.google.common.io.ByteSource
public InputStream openStream() throws IOException {
return sliceStream(ByteSource.this.openStream());
}
@Override // com.google.common.io.ByteSource
public Optional<Long> sizeIfKnown() {
Optional<Long> sizeIfKnown = ByteSource.this.sizeIfKnown();
if (!sizeIfKnown.isPresent()) {
return Optional.absent();
}
long longValue = sizeIfKnown.get().longValue();
return Optional.of(Long.valueOf(Math.min(this.length, longValue - Math.min(this.offset, longValue))));
}
@Override // com.google.common.io.ByteSource
public ByteSource slice(long j, long j2) {
Preconditions.checkArgument(j >= 0, "offset (%s) may not be negative", j);
Preconditions.checkArgument(j2 >= 0, "length (%s) may not be negative", j2);
long j3 = this.length - j;
return j3 <= 0 ? ByteSource.empty() : ByteSource.this.slice(this.offset + j, Math.min(j2, j3));
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(ByteSource.this.toString());
sb.append(".slice(");
sb.append(this.offset);
sb.append(", ");
return a$$ExternalSyntheticOutline0.m(this.length, MarkdownConstants.RIGHT_PARENTHESIS, sb);
}
}
public static ByteSource concat(Iterator<? extends ByteSource> it2) {
return concat(ImmutableList.copyOf(it2));
}
private long countBySkipping(InputStream inputStream) throws IOException {
long j = 0;
while (true) {
long skipUpTo = ByteStreams.skipUpTo(inputStream, SieveCacheKt.NodeLinkMask);
if (skipUpTo <= 0) {
return j;
}
j += skipUpTo;
}
}
public static ByteSource empty() {
return EmptyByteSource.INSTANCE;
}
public static ByteSource wrap(byte[] bArr) {
return new ByteArrayByteSource(bArr);
}
public CharSource asCharSource(Charset charset) {
return new AsCharSource(charset);
}
public boolean contentEquals(ByteSource byteSource) throws IOException {
Preconditions.checkNotNull(byteSource);
Closer create = Closer.create();
try {
return ByteStreams.contentsEqual((InputStream) create.register(openStream()), (InputStream) create.register(byteSource.openStream()));
} finally {
}
}
@CanIgnoreReturnValue
public long copyTo(ByteSink byteSink) throws IOException {
Preconditions.checkNotNull(byteSink);
Closer create = Closer.create();
try {
return ByteStreams.copy((InputStream) create.register(openStream()), (OutputStream) create.register(byteSink.openStream()));
} finally {
}
}
public HashCode hash(HashFunction hashFunction) throws IOException {
Hasher newHasher = hashFunction.newHasher();
copyTo(Funnels.asOutputStream(newHasher));
return newHasher.hash();
}
public boolean isEmpty() throws IOException {
Optional<Long> sizeIfKnown = sizeIfKnown();
if (sizeIfKnown.isPresent()) {
return sizeIfKnown.get().longValue() == 0;
}
try {
return ((InputStream) Closer.create().register(openStream())).read() == -1;
} finally {
}
}
public InputStream openBufferedStream() throws IOException {
InputStream openStream = openStream();
return openStream instanceof BufferedInputStream ? (BufferedInputStream) openStream : new BufferedInputStream(openStream);
}
public abstract InputStream openStream() throws IOException;
public byte[] read() throws IOException {
Closer create = Closer.create();
try {
InputStream inputStream = (InputStream) create.register(openStream());
Optional<Long> sizeIfKnown = sizeIfKnown();
return sizeIfKnown.isPresent() ? ByteStreams.toByteArray(inputStream, sizeIfKnown.get().longValue()) : ByteStreams.toByteArray(inputStream);
} catch (Throwable th) {
try {
throw create.rethrow(th);
} finally {
create.close();
}
}
}
public long size() throws IOException {
Optional<Long> sizeIfKnown = sizeIfKnown();
if (sizeIfKnown.isPresent()) {
return sizeIfKnown.get().longValue();
}
Closer create = Closer.create();
try {
return countBySkipping((InputStream) create.register(openStream()));
} catch (IOException unused) {
create.close();
try {
return ByteStreams.exhaust((InputStream) Closer.create().register(openStream()));
} finally {
}
} finally {
}
}
public Optional<Long> sizeIfKnown() {
return Optional.absent();
}
public ByteSource slice(long j, long j2) {
return new SlicedByteSource(j, j2);
}
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
public static class ByteArrayByteSource extends ByteSource {
final byte[] bytes;
final int length;
final int offset;
public ByteArrayByteSource(byte[] bArr, int i, int i2) {
this.bytes = bArr;
this.offset = i;
this.length = i2;
}
@Override // com.google.common.io.ByteSource
public long copyTo(OutputStream outputStream) throws IOException {
outputStream.write(this.bytes, this.offset, this.length);
return this.length;
}
@Override // com.google.common.io.ByteSource
public HashCode hash(HashFunction hashFunction) throws IOException {
return hashFunction.hashBytes(this.bytes, this.offset, this.length);
}
@Override // com.google.common.io.ByteSource
public boolean isEmpty() {
return this.length == 0;
}
@Override // com.google.common.io.ByteSource
public InputStream openBufferedStream() {
return openStream();
}
@Override // com.google.common.io.ByteSource
public InputStream openStream() {
return new ByteArrayInputStream(this.bytes, this.offset, this.length);
}
@Override // com.google.common.io.ByteSource
@ParametricNullness
public <T> T read(ByteProcessor<T> byteProcessor) throws IOException {
byteProcessor.processBytes(this.bytes, this.offset, this.length);
return byteProcessor.getResult();
}
@Override // com.google.common.io.ByteSource
public long size() {
return this.length;
}
@Override // com.google.common.io.ByteSource
public Optional<Long> sizeIfKnown() {
return Optional.of(Long.valueOf(this.length));
}
@Override // com.google.common.io.ByteSource
public ByteSource slice(long j, long j2) {
Preconditions.checkArgument(j >= 0, "offset (%s) may not be negative", j);
Preconditions.checkArgument(j2 >= 0, "length (%s) may not be negative", j2);
long min = Math.min(j, this.length);
return new ByteArrayByteSource(this.bytes, this.offset + ((int) min), (int) Math.min(j2, this.length - min));
}
public String toString() {
return "ByteSource.wrap(" + Ascii.truncate(BaseEncoding.base16().encode(this.bytes, this.offset, this.length), 30, "...") + MarkdownConstants.RIGHT_PARENTHESIS;
}
public ByteArrayByteSource(byte[] bArr) {
this(bArr, 0, bArr.length);
}
@Override // com.google.common.io.ByteSource
public byte[] read() {
byte[] bArr = this.bytes;
int i = this.offset;
return Arrays.copyOfRange(bArr, i, this.length + i);
}
}
public static ByteSource concat(Iterable<? extends ByteSource> iterable) {
return new ConcatenatedByteSource(iterable);
}
public static ByteSource concat(ByteSource... byteSourceArr) {
return concat(ImmutableList.copyOf(byteSourceArr));
}
@CanIgnoreReturnValue
public long copyTo(OutputStream outputStream) throws IOException {
Preconditions.checkNotNull(outputStream);
try {
return ByteStreams.copy((InputStream) Closer.create().register(openStream()), outputStream);
} finally {
}
}
@CanIgnoreReturnValue
@ParametricNullness
public <T> T read(ByteProcessor<T> byteProcessor) throws IOException {
Preconditions.checkNotNull(byteProcessor);
try {
return (T) ByteStreams.readBytes((InputStream) Closer.create().register(openStream()), byteProcessor);
} finally {
}
}
}