Search in sources :

Example 6 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

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 in the process of 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_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 7 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

the class ByteSink method writeFrom.

/**
   * Writes all the bytes from the given {@code InputStream} to this sink. Does not close
   * {@code input}.
   *
   * @return the number of bytes written
   * @throws IOException if an I/O occurs in the process of reading from {@code input} or writing to
   *     this sink
   */
@CanIgnoreReturnValue
public long writeFrom(InputStream input) throws IOException {
    checkNotNull(input);
    Closer closer = Closer.create();
    try {
        OutputStream out = closer.register(openStream());
        long written = ByteStreams.copy(input, 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 : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 8 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

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_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 9 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

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_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 10 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

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 in the process of 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_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Aggregations

CanIgnoreReturnValue (com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)20 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_voltpatches.common.annotations.Beta)2 Preconditions.checkState (com.google_voltpatches.common.base.Preconditions.checkState)2 State (com.google_voltpatches.common.util.concurrent.Service.State)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharBuffer (java.nio.CharBuffer)2 REUSING_EDGE (com.google_voltpatches.common.graph.GraphConstants.REUSING_EDGE)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 CancellationException (java.util.concurrent.CancellationException)1