package com.google.common.io; import com.google.common.base.Ascii; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.AbstractIterator; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; import java.io.Writer; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import javax.annotation.CheckForNull; @ElementTypesAreNonnullByDefault /* loaded from: classes3.dex */ public abstract class CharSource { public abstract Reader openStream() throws IOException; public ByteSource asByteSource(Charset charset) { return new AsByteSource(charset); } public BufferedReader openBufferedStream() throws IOException { Reader openStream = openStream(); if (openStream instanceof BufferedReader) { return (BufferedReader) openStream; } return new BufferedReader(openStream); } public Optional lengthIfKnown() { return Optional.absent(); } public long length() throws IOException { Optional lengthIfKnown = lengthIfKnown(); if (lengthIfKnown.isPresent()) { return lengthIfKnown.get().longValue(); } try { return countBySkipping((Reader) Closer.create().register(openStream())); } finally { } } private long countBySkipping(Reader reader) throws IOException { long j = 0; while (true) { long skip = reader.skip(Long.MAX_VALUE); if (skip == 0) { return j; } j += skip; } } public long copyTo(Appendable appendable) throws IOException { Preconditions.checkNotNull(appendable); try { return CharStreams.copy((Reader) Closer.create().register(openStream()), appendable); } finally { } } public long copyTo(CharSink charSink) throws IOException { Preconditions.checkNotNull(charSink); Closer create = Closer.create(); try { return CharStreams.copy((Reader) create.register(openStream()), (Writer) create.register(charSink.openStream())); } finally { } } public String read() throws IOException { try { return CharStreams.toString((Reader) Closer.create().register(openStream())); } finally { } } @CheckForNull public String readFirstLine() throws IOException { try { return ((BufferedReader) Closer.create().register(openBufferedStream())).readLine(); } finally { } } public ImmutableList readLines() throws IOException { try { BufferedReader bufferedReader = (BufferedReader) Closer.create().register(openBufferedStream()); ArrayList newArrayList = Lists.newArrayList(); while (true) { String readLine = bufferedReader.readLine(); if (readLine != null) { newArrayList.add(readLine); } else { return ImmutableList.copyOf((Collection) newArrayList); } } } finally { } } @ParametricNullness public T readLines(LineProcessor lineProcessor) throws IOException { Preconditions.checkNotNull(lineProcessor); try { return (T) CharStreams.readLines((Reader) Closer.create().register(openStream()), lineProcessor); } finally { } } public boolean isEmpty() throws IOException { Optional lengthIfKnown = lengthIfKnown(); if (lengthIfKnown.isPresent()) { return lengthIfKnown.get().longValue() == 0; } Closer create = Closer.create(); try { return ((Reader) create.register(openStream())).read() == -1; } catch (Throwable th) { try { throw create.rethrow(th); } finally { create.close(); } } } public static CharSource concat(Iterable iterable) { return new ConcatenatedCharSource(iterable); } public static CharSource concat(Iterator it) { return concat(ImmutableList.copyOf(it)); } public static CharSource concat(CharSource... charSourceArr) { return concat(ImmutableList.copyOf(charSourceArr)); } public static CharSource wrap(CharSequence charSequence) { if (charSequence instanceof String) { return new StringCharSource((String) charSequence); } return new CharSequenceCharSource(charSequence); } public static CharSource empty() { return EmptyCharSource.INSTANCE; } /* loaded from: classes3.dex */ private final class AsByteSource extends ByteSource { final Charset charset; AsByteSource(Charset charset) { this.charset = (Charset) Preconditions.checkNotNull(charset); } @Override // com.google.common.io.ByteSource public CharSource asCharSource(Charset charset) { return charset.equals(this.charset) ? CharSource.this : super.asCharSource(charset); } @Override // com.google.common.io.ByteSource public InputStream openStream() throws IOException { return new ReaderInputStream(CharSource.this.openStream(), this.charset, 8192); } public String toString() { String obj = CharSource.this.toString(); String valueOf = String.valueOf(this.charset); return new StringBuilder(String.valueOf(obj).length() + 15 + String.valueOf(valueOf).length()).append(obj).append(".asByteSource(").append(valueOf).append(")").toString(); } } /* loaded from: classes3.dex */ private static class CharSequenceCharSource extends CharSource { private static final Splitter LINE_SPLITTER = Splitter.onPattern("\r\n|\n|\r"); protected final CharSequence seq; protected CharSequenceCharSource(CharSequence charSequence) { this.seq = (CharSequence) Preconditions.checkNotNull(charSequence); } @Override // com.google.common.io.CharSource public Reader openStream() { return new CharSequenceReader(this.seq); } @Override // com.google.common.io.CharSource public String read() { return this.seq.toString(); } @Override // com.google.common.io.CharSource public boolean isEmpty() { return this.seq.length() == 0; } @Override // com.google.common.io.CharSource public long length() { return this.seq.length(); } @Override // com.google.common.io.CharSource public Optional lengthIfKnown() { return Optional.of(Long.valueOf(this.seq.length())); } private Iterator linesIterator() { return new AbstractIterator() { // from class: com.google.common.io.CharSource.CharSequenceCharSource.1 Iterator lines; { this.lines = CharSequenceCharSource.LINE_SPLITTER.split(CharSequenceCharSource.this.seq).iterator(); } /* JADX INFO: Access modifiers changed from: protected */ @Override // com.google.common.collect.AbstractIterator @CheckForNull public String computeNext() { if (this.lines.hasNext()) { String next = this.lines.next(); if (this.lines.hasNext() || !next.isEmpty()) { return next; } } return endOfData(); } }; } @Override // com.google.common.io.CharSource @CheckForNull public String readFirstLine() { Iterator linesIterator = linesIterator(); if (linesIterator.hasNext()) { return linesIterator.next(); } return null; } @Override // com.google.common.io.CharSource public ImmutableList readLines() { return ImmutableList.copyOf(linesIterator()); } @Override // com.google.common.io.CharSource @ParametricNullness public T readLines(LineProcessor lineProcessor) throws IOException { Iterator linesIterator = linesIterator(); while (linesIterator.hasNext() && lineProcessor.processLine(linesIterator.next())) { } return lineProcessor.getResult(); } public String toString() { String truncate = Ascii.truncate(this.seq, 30, "..."); return new StringBuilder(String.valueOf(truncate).length() + 17).append("CharSource.wrap(").append(truncate).append(")").toString(); } } /* loaded from: classes3.dex */ private static class StringCharSource extends CharSequenceCharSource { protected StringCharSource(String str) { super(str); } @Override // com.google.common.io.CharSource.CharSequenceCharSource, com.google.common.io.CharSource public Reader openStream() { return new StringReader((String) this.seq); } @Override // com.google.common.io.CharSource public long copyTo(Appendable appendable) throws IOException { appendable.append(this.seq); return this.seq.length(); } @Override // com.google.common.io.CharSource public long copyTo(CharSink charSink) throws IOException { Preconditions.checkNotNull(charSink); try { ((Writer) Closer.create().register(charSink.openStream())).write((String) this.seq); return this.seq.length(); } finally { } } } /* loaded from: classes3.dex */ private static final class EmptyCharSource extends StringCharSource { private static final EmptyCharSource INSTANCE = new EmptyCharSource(); @Override // com.google.common.io.CharSource.CharSequenceCharSource public String toString() { return "CharSource.empty()"; } private EmptyCharSource() { super(""); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes3.dex */ public static final class ConcatenatedCharSource extends CharSource { private final Iterable sources; ConcatenatedCharSource(Iterable iterable) { this.sources = (Iterable) Preconditions.checkNotNull(iterable); } @Override // com.google.common.io.CharSource public Reader openStream() throws IOException { return new MultiReader(this.sources.iterator()); } @Override // com.google.common.io.CharSource public boolean isEmpty() throws IOException { Iterator it = this.sources.iterator(); while (it.hasNext()) { if (!it.next().isEmpty()) { return false; } } return true; } @Override // com.google.common.io.CharSource public Optional lengthIfKnown() { Iterator it = this.sources.iterator(); long j = 0; while (it.hasNext()) { Optional lengthIfKnown = it.next().lengthIfKnown(); if (!lengthIfKnown.isPresent()) { return Optional.absent(); } j += lengthIfKnown.get().longValue(); } return Optional.of(Long.valueOf(j)); } @Override // com.google.common.io.CharSource public long length() throws IOException { Iterator it = this.sources.iterator(); long j = 0; while (it.hasNext()) { j += it.next().length(); } return j; } public String toString() { String valueOf = String.valueOf(this.sources); return new StringBuilder(String.valueOf(valueOf).length() + 19).append("CharSource.concat(").append(valueOf).append(")").toString(); } } }