Search in sources :

Example 1 with Beta

use of com.google_voltpatches.common.annotations.Beta in project voltdb by VoltDB.

the class CharSource method length.

/**
   * Returns the length of this source in chars, even if doing so requires opening and traversing an
   * entire stream. To avoid a potentially expensive operation, see {@link #lengthIfKnown}.
   *
   * <p>The default implementation calls {@link #lengthIfKnown} and returns the value if present. If
   * absent, it will fall back to a heavyweight operation that will open a stream,
   * {@link Reader#skip(long) skip} to the end of the stream, and return the total number of chars
   * that were skipped.
   *
   * <p>Note that for sources that implement {@link #lengthIfKnown} to provide a more efficient
   * implementation, it is <i>possible</i> that this method will return a different number of chars
   * than would be returned by reading all of the chars.
   *
   * <p>In either case, for mutable sources such as files, a subsequent read may return a different
   * number of chars if the contents are changed.
   *
   * @throws IOException if an I/O error occurs in the process of reading the length of this source
   * @since 19.0
   */
@Beta
public long length() throws IOException {
    Optional<Long> lengthIfKnown = lengthIfKnown();
    if (lengthIfKnown.isPresent()) {
        return lengthIfKnown.get();
    }
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        return countBySkipping(reader);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Beta(com.google_voltpatches.common.annotations.Beta)

Example 2 with Beta

use of com.google_voltpatches.common.annotations.Beta in project voltdb by VoltDB.

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}, this method breaks lines on any of {@code \n}, {@code \r} or
   * {@code \r\n}, does not include the line separator in the lines passed to the {@code processor}
   * and does not consider there to be an extra empty line at the end if the content is terminated
   * with a line separator.
   *
   * @throws IOException if an I/O error occurs in the process of 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_voltpatches.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google_voltpatches.common.annotations.Beta)

Example 3 with Beta

use of com.google_voltpatches.common.annotations.Beta in project voltdb by VoltDB.

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 in the process of 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_voltpatches.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google_voltpatches.common.annotations.Beta)

Aggregations

Beta (com.google_voltpatches.common.annotations.Beta)3 CanIgnoreReturnValue (com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)2 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1