Search in sources :

Example 6 with CanIgnoreReturnValue

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();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google.common.annotations.Beta)

Example 7 with CanIgnoreReturnValue

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();
    }
}
Also used : BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 8 with CanIgnoreReturnValue

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();
    }
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Writer(java.io.Writer) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 9 with CanIgnoreReturnValue

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();
    }
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google.common.annotations.Beta)

Example 10 with CanIgnoreReturnValue

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;
}
Also used : CharBuffer(java.nio.CharBuffer) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Aggregations

CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 Beta (com.google.common.annotations.Beta)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 State (com.google.common.util.concurrent.Service.State)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharBuffer (java.nio.CharBuffer)2 CancellationException (java.util.concurrent.CancellationException)2 GwtIncompatible (com.google.common.annotations.GwtIncompatible)1 REUSING_EDGE (com.google.common.graph.GraphConstants.REUSING_EDGE)1 Any (com.google.protobuf.Any)1 ChannelFuture (io.netty.channel.ChannelFuture)1 Query (io.spine.client.Query)1 Subscription (io.spine.client.Subscription)1