224 lines
7.0 KiB
Java
224 lines
7.0 KiB
Java
|
package com.google.common.io;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import java.io.EOFException;
|
||
|
import java.io.IOException;
|
||
|
import java.io.Reader;
|
||
|
import java.io.Writer;
|
||
|
import java.nio.CharBuffer;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public final class CharStreams {
|
||
|
private static final int DEFAULT_BUF_SIZE = 2048;
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public static CharBuffer createBuffer() {
|
||
|
return CharBuffer.allocate(2048);
|
||
|
}
|
||
|
|
||
|
private CharStreams() {
|
||
|
}
|
||
|
|
||
|
public static long copy(Readable readable, Appendable appendable) throws IOException {
|
||
|
if (readable instanceof Reader) {
|
||
|
if (appendable instanceof StringBuilder) {
|
||
|
return copyReaderToBuilder((Reader) readable, (StringBuilder) appendable);
|
||
|
}
|
||
|
return copyReaderToWriter((Reader) readable, asWriter(appendable));
|
||
|
}
|
||
|
Preconditions.checkNotNull(readable);
|
||
|
Preconditions.checkNotNull(appendable);
|
||
|
CharBuffer createBuffer = createBuffer();
|
||
|
long j = 0;
|
||
|
while (readable.read(createBuffer) != -1) {
|
||
|
Java8Compatibility.flip(createBuffer);
|
||
|
appendable.append(createBuffer);
|
||
|
j += createBuffer.remaining();
|
||
|
Java8Compatibility.clear(createBuffer);
|
||
|
}
|
||
|
return j;
|
||
|
}
|
||
|
|
||
|
static long copyReaderToBuilder(Reader reader, StringBuilder sb) throws IOException {
|
||
|
Preconditions.checkNotNull(reader);
|
||
|
Preconditions.checkNotNull(sb);
|
||
|
char[] cArr = new char[2048];
|
||
|
long j = 0;
|
||
|
while (true) {
|
||
|
int read = reader.read(cArr);
|
||
|
if (read == -1) {
|
||
|
return j;
|
||
|
}
|
||
|
sb.append(cArr, 0, read);
|
||
|
j += read;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static long copyReaderToWriter(Reader reader, Writer writer) throws IOException {
|
||
|
Preconditions.checkNotNull(reader);
|
||
|
Preconditions.checkNotNull(writer);
|
||
|
char[] cArr = new char[2048];
|
||
|
long j = 0;
|
||
|
while (true) {
|
||
|
int read = reader.read(cArr);
|
||
|
if (read == -1) {
|
||
|
return j;
|
||
|
}
|
||
|
writer.write(cArr, 0, read);
|
||
|
j += read;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static String toString(Readable readable) throws IOException {
|
||
|
return toStringBuilder(readable).toString();
|
||
|
}
|
||
|
|
||
|
private static StringBuilder toStringBuilder(Readable readable) throws IOException {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
if (readable instanceof Reader) {
|
||
|
copyReaderToBuilder((Reader) readable, sb);
|
||
|
} else {
|
||
|
copy(readable, sb);
|
||
|
}
|
||
|
return sb;
|
||
|
}
|
||
|
|
||
|
public static List<String> readLines(Readable readable) throws IOException {
|
||
|
ArrayList arrayList = new ArrayList();
|
||
|
LineReader lineReader = new LineReader(readable);
|
||
|
while (true) {
|
||
|
String readLine = lineReader.readLine();
|
||
|
if (readLine == null) {
|
||
|
return arrayList;
|
||
|
}
|
||
|
arrayList.add(readLine);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static <T> T readLines(Readable readable, LineProcessor<T> lineProcessor) throws IOException {
|
||
|
String readLine;
|
||
|
Preconditions.checkNotNull(readable);
|
||
|
Preconditions.checkNotNull(lineProcessor);
|
||
|
LineReader lineReader = new LineReader(readable);
|
||
|
do {
|
||
|
readLine = lineReader.readLine();
|
||
|
if (readLine == null) {
|
||
|
break;
|
||
|
}
|
||
|
} while (lineProcessor.processLine(readLine));
|
||
|
return lineProcessor.getResult();
|
||
|
}
|
||
|
|
||
|
public static long exhaust(Readable readable) throws IOException {
|
||
|
CharBuffer createBuffer = createBuffer();
|
||
|
long j = 0;
|
||
|
while (true) {
|
||
|
long read = readable.read(createBuffer);
|
||
|
if (read == -1) {
|
||
|
return j;
|
||
|
}
|
||
|
j += read;
|
||
|
Java8Compatibility.clear(createBuffer);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static void skipFully(Reader reader, long j) throws IOException {
|
||
|
Preconditions.checkNotNull(reader);
|
||
|
while (j > 0) {
|
||
|
long skip = reader.skip(j);
|
||
|
if (skip == 0) {
|
||
|
throw new EOFException();
|
||
|
}
|
||
|
j -= skip;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static Writer nullWriter() {
|
||
|
return NullWriter.INSTANCE;
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
static final class NullWriter extends Writer {
|
||
|
private static final NullWriter INSTANCE = new NullWriter();
|
||
|
|
||
|
@Override // java.io.Writer, java.lang.Appendable
|
||
|
public final Writer append(char c) {
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.lang.Appendable
|
||
|
public final Writer append(CharSequence charSequence) {
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
|
||
|
public final void close() {
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.io.Flushable
|
||
|
public final void flush() {
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer
|
||
|
public final void write(int i) {
|
||
|
}
|
||
|
|
||
|
private NullWriter() {
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.lang.Appendable
|
||
|
public final /* bridge */ /* synthetic */ Appendable append(char c) throws IOException {
|
||
|
return append(c);
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.lang.Appendable
|
||
|
public final /* bridge */ /* synthetic */ Appendable append(CharSequence charSequence) throws IOException {
|
||
|
return append(charSequence);
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.lang.Appendable
|
||
|
public final /* bridge */ /* synthetic */ Appendable append(CharSequence charSequence, int i, int i2) throws IOException {
|
||
|
return append(charSequence, i, i2);
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer
|
||
|
public final void write(char[] cArr) {
|
||
|
Preconditions.checkNotNull(cArr);
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer
|
||
|
public final void write(char[] cArr, int i, int i2) {
|
||
|
Preconditions.checkPositionIndexes(i, i2 + i, cArr.length);
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer
|
||
|
public final void write(String str) {
|
||
|
Preconditions.checkNotNull(str);
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer
|
||
|
public final void write(String str, int i, int i2) {
|
||
|
Preconditions.checkPositionIndexes(i, i2 + i, str.length());
|
||
|
}
|
||
|
|
||
|
@Override // java.io.Writer, java.lang.Appendable
|
||
|
public final Writer append(CharSequence charSequence, int i, int i2) {
|
||
|
Preconditions.checkPositionIndexes(i, i2, charSequence == null ? 4 : charSequence.length());
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
public final String toString() {
|
||
|
return "CharStreams.nullWriter()";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static Writer asWriter(Appendable appendable) {
|
||
|
if (appendable instanceof Writer) {
|
||
|
return (Writer) appendable;
|
||
|
}
|
||
|
return new AppendableWriter(appendable);
|
||
|
}
|
||
|
}
|