Search in sources :

Example 21 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class ByteSource method copyTo.

/**
   * Copies the contents of this byte source to the given {@code ByteSink}.
   *
   * @return the number of bytes copied
   * @throws IOException if an I/O error occurs while reading from this source or writing to
   *     {@code sink}
   */
@CanIgnoreReturnValue
public long copyTo(ByteSink sink) throws IOException {
    checkNotNull(sink);
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(openStream());
        OutputStream out = closer.register(sink.openStream());
        return ByteStreams.copy(in, out);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 22 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class ByteSource method copyTo.

/**
   * Copies the contents of this byte source to the given {@code OutputStream}. Does not close
   * {@code output}.
   *
   * @return the number of bytes copied
   * @throws IOException if an I/O error occurs while reading from this source or writing to
   *     {@code output}
   */
@CanIgnoreReturnValue
public long copyTo(OutputStream output) throws IOException {
    checkNotNull(output);
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(openStream());
        return ByteStreams.copy(in, output);
    } 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)

Example 23 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class ByteStreams method copy.

/**
   * Copies all bytes from the readable channel to the writable channel. Does not close or flush
   * either channel.
   *
   * @param from the readable channel to read from
   * @param to the writable channel to write to
   * @return the number of bytes copied
   * @throws IOException if an I/O error occurs
   */
@CanIgnoreReturnValue
public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException {
    checkNotNull(from);
    checkNotNull(to);
    if (from instanceof FileChannel) {
        FileChannel sourceChannel = (FileChannel) from;
        long oldPosition = sourceChannel.position();
        long position = oldPosition;
        long copied;
        do {
            copied = sourceChannel.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
            position += copied;
            sourceChannel.position(position);
        } while (copied > 0 || position < sourceChannel.size());
        return position - oldPosition;
    }
    ByteBuffer buf = ByteBuffer.wrap(createBuffer());
    long total = 0;
    while (from.read(buf) != -1) {
        buf.flip();
        while (buf.hasRemaining()) {
            total += to.write(buf);
        }
        buf.clear();
    }
    return total;
}
Also used : FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 24 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class CharSource method copyTo.

/**
   * Appends the contents of this source to the given {@link Appendable} (such as a {@link Writer}).
   * Does not close {@code appendable} if it is {@code Closeable}.
   *
   * @return the number of characters copied
   * @throws IOException if an I/O error occurs while reading from this source or writing to
   *     {@code appendable}
   */
@CanIgnoreReturnValue
public long copyTo(Appendable appendable) throws IOException {
    checkNotNull(appendable);
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        return CharStreams.copy(reader, appendable);
    } 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)

Example 25 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class CharStreams method exhaust.

/**
   * Reads and discards data from the given {@code Readable} until the end of the stream is
   * reached. Returns the total number of chars read. Does not close the stream.
   *
   * @since 20.0
   */
@CanIgnoreReturnValue
public static long exhaust(Readable readable) throws IOException {
    long total = 0;
    long read;
    CharBuffer buf = createBuffer();
    while ((read = readable.read(buf)) != -1) {
        total += read;
        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