use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.
the class ByteSource method read.
/**
* Reads the contents of this byte source using the given {@code processor} to process bytes as
* they are read. Stops when all bytes have been read or the consumer returns {@code false}.
* Returns the result produced by the processor.
*
* @throws IOException if an I/O error occurs while reading from this source or if
* {@code processor} throws an {@code IOException}
* @since 16.0
*/
@Beta
// some processors won't return a useful result
@CanIgnoreReturnValue
public <T> T read(ByteProcessor<T> processor) throws IOException {
checkNotNull(processor);
Closer closer = Closer.create();
try {
InputStream in = closer.register(openStream());
return ByteStreams.readBytes(in, processor);
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.
the class CharSink method writeFrom.
/**
* Writes all the text from the given {@link Readable} (such as a {@link Reader}) to this sink.
* Does not close {@code readable} if it is {@code Closeable}.
*
* @return the number of characters written
* @throws IOException if an I/O error occurs while reading from {@code readable} or writing to
* this sink
*/
@CanIgnoreReturnValue
public long writeFrom(Readable readable) throws IOException {
checkNotNull(readable);
Closer closer = Closer.create();
try {
Writer out = closer.register(openStream());
long written = CharStreams.copy(readable, out);
// https://code.google.com/p/guava-libraries/issues/detail?id=1330
out.flush();
return written;
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.
the class CharSource method copyTo.
/**
* Copies the contents of this source to the given sink.
*
* @return the number of characters copied
* @throws IOException if an I/O error occurs while reading from this source or writing to
* {@code sink}
*/
@CanIgnoreReturnValue
public long copyTo(CharSink sink) throws IOException {
checkNotNull(sink);
Closer closer = Closer.create();
try {
Reader reader = closer.register(openStream());
Writer writer = closer.register(sink.openStream());
return CharStreams.copy(reader, writer);
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.
the class CharSource method readLines.
/**
* Reads lines of text from this source, processing each line as it is read using the given
* {@link LineProcessor processor}. Stops when all lines have been processed or the processor
* returns {@code false} and returns the result produced by the processor.
*
* <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of
* text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or
* {@code \n}. If the source's content does not end in a line termination sequence, it is treated
* as if it does.
*
* @throws IOException if an I/O error occurs while reading from this source or if
* {@code processor} throws an {@code IOException}
* @since 16.0
*/
@Beta
// some processors won't return a useful result
@CanIgnoreReturnValue
public <T> T readLines(LineProcessor<T> processor) throws IOException {
checkNotNull(processor);
Closer closer = Closer.create();
try {
Reader reader = closer.register(openStream());
return CharStreams.readLines(reader, processor);
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.
the class CharStreams method copy.
/**
* Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not
* close or flush either object.
*
* @param from the object to read from
* @param to the object to write to
* @return the number of characters copied
* @throws IOException if an I/O error occurs
*/
@CanIgnoreReturnValue
public static long copy(Readable from, Appendable to) throws IOException {
checkNotNull(from);
checkNotNull(to);
CharBuffer buf = createBuffer();
long total = 0;
while (from.read(buf) != -1) {
buf.flip();
to.append(buf);
total += buf.remaining();
buf.clear();
}
return total;
}
Aggregations