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

383 lines
13 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
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;
/* loaded from: classes2.dex */
public abstract class CharSource {
public abstract Reader openStream() throws IOException;
public ByteSource asByteSource(Charset charset) {
return new AsByteSource(this, charset);
}
public BufferedReader openBufferedStream() throws IOException {
Reader openStream = openStream();
if (openStream instanceof BufferedReader) {
return (BufferedReader) openStream;
}
return new BufferedReader(openStream);
}
public Optional<Long> lengthIfKnown() {
return Optional.absent();
}
public long length() throws IOException {
Optional<Long> 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 {
}
}
public String readFirstLine() throws IOException {
try {
return ((BufferedReader) Closer.create().register(openBufferedStream())).readLine();
} finally {
}
}
public ImmutableList<String> 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 {
}
}
public <T> T readLines(LineProcessor<T> lineProcessor) throws IOException {
Preconditions.checkNotNull(lineProcessor);
try {
return (T) CharStreams.readLines((Reader) Closer.create().register(openStream()), lineProcessor);
} finally {
}
}
public boolean isEmpty() throws IOException {
Optional<Long> 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<? extends CharSource> iterable) {
return new ConcatenatedCharSource(iterable);
}
public static CharSource concat(Iterator<? extends CharSource> 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;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class AsByteSource extends ByteSource {
final Charset charset;
final CharSource this$0;
AsByteSource(CharSource charSource, Charset charset) {
this.this$0 = charSource;
this.charset = (Charset) Preconditions.checkNotNull(charset);
}
@Override // com.google.common.io.ByteSource
public final CharSource asCharSource(Charset charset) {
return charset.equals(this.charset) ? this.this$0 : super.asCharSource(charset);
}
@Override // com.google.common.io.ByteSource
public final InputStream openStream() throws IOException {
return new ReaderInputStream(this.this$0.openStream(), this.charset, 8192);
}
public final String toString() {
String obj = this.this$0.toString();
String valueOf = String.valueOf(this.charset);
StringBuilder sb = new StringBuilder(String.valueOf(obj).length() + 15 + String.valueOf(valueOf).length());
sb.append(obj);
sb.append(".asByteSource(");
sb.append(valueOf);
sb.append(")");
return sb.toString();
}
}
/* loaded from: classes2.dex */
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<Long> lengthIfKnown() {
return Optional.of(Long.valueOf(this.seq.length()));
}
private Iterator<String> linesIterator() {
return new AbstractIterator<String>(this) { // from class: com.google.common.io.CharSource.CharSequenceCharSource.1
Iterator<String> lines;
final CharSequenceCharSource this$0;
{
this.this$0 = this;
this.lines = CharSequenceCharSource.LINE_SPLITTER.split(this.seq).iterator();
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // com.google.common.collect.AbstractIterator
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
public String readFirstLine() {
Iterator<String> linesIterator = linesIterator();
if (linesIterator.hasNext()) {
return linesIterator.next();
}
return null;
}
@Override // com.google.common.io.CharSource
public ImmutableList<String> readLines() {
return ImmutableList.copyOf(linesIterator());
}
@Override // com.google.common.io.CharSource
public <T> T readLines(LineProcessor<T> lineProcessor) throws IOException {
Iterator<String> linesIterator = linesIterator();
while (linesIterator.hasNext() && lineProcessor.processLine(linesIterator.next())) {
}
return lineProcessor.getResult();
}
public String toString() {
String truncate = Ascii.truncate(this.seq, 30, "...");
StringBuilder sb = new StringBuilder(String.valueOf(truncate).length() + 17);
sb.append("CharSource.wrap(");
sb.append(truncate);
sb.append(")");
return sb.toString();
}
}
/* loaded from: classes2.dex */
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 {
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class EmptyCharSource extends StringCharSource {
private static final EmptyCharSource INSTANCE = new EmptyCharSource();
private EmptyCharSource() {
super("");
}
@Override // com.google.common.io.CharSource.CharSequenceCharSource
public final String toString() {
return "CharSource.empty()";
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class ConcatenatedCharSource extends CharSource {
private final Iterable<? extends CharSource> sources;
ConcatenatedCharSource(Iterable<? extends CharSource> iterable) {
this.sources = (Iterable) Preconditions.checkNotNull(iterable);
}
@Override // com.google.common.io.CharSource
public final Reader openStream() throws IOException {
return new MultiReader(this.sources.iterator());
}
@Override // com.google.common.io.CharSource
public final boolean isEmpty() throws IOException {
Iterator<? extends CharSource> it = this.sources.iterator();
while (it.hasNext()) {
if (!it.next().isEmpty()) {
return false;
}
}
return true;
}
@Override // com.google.common.io.CharSource
public final Optional<Long> lengthIfKnown() {
Iterator<? extends CharSource> it = this.sources.iterator();
long j = 0;
while (it.hasNext()) {
Optional<Long> 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 final long length() throws IOException {
Iterator<? extends CharSource> it = this.sources.iterator();
long j = 0;
while (it.hasNext()) {
j += it.next().length();
}
return j;
}
public final String toString() {
String valueOf = String.valueOf(this.sources);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 19);
sb.append("CharSource.concat(");
sb.append(valueOf);
sb.append(")");
return sb.toString();
}
}
}