Example usage for com.google.common.io ByteProcessor processBytes

List of usage examples for com.google.common.io ByteProcessor processBytes

Introduction

In this page you can find the example usage for com.google.common.io ByteProcessor processBytes.

Prototype

boolean processBytes(byte[] buf, int off, int len) throws IOException;

Source Link

Document

This method will be called for each chunk of bytes in an input stream.

Usage

From source file:com.tinspx.util.io.ByteArrayChannelSource.java

@Override
public <T> T read(ByteProcessor<T> processor) throws IOException {
    final byte[] bytes = read();
    processor.processBytes(bytes, 0, bytes.length);
    return processor.getResult();
}

From source file:com.opengamma.strata.collect.io.ArrayByteSource.java

@Override
public <T> T read(ByteProcessor<T> processor) throws IOException {
    processor.processBytes(array, 0, array.length);
    return processor.getResult();
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies the as many bytes as possible from {@code from} into {@code to}.
 * Byte processing ends when all bytes have been processed or when
 * {@link ByteProcessor#processBytes(byte[], int, int) processBytes} returns
 * {@code false}./*  w  w w . ja  v a2  s .c  o m*/
 * 
 * @param from the source to read bytes from
 * @param to the ByteProcessor to process the bytes read from {@code from}
 * @return the last result of calling
 * {@link ByteProcessor#processBytes(byte[], int, int) processBytes},
 * indicating if byte processing should continue
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 */
@ThreadLocalArray(8192)
public static boolean copy(@NonNull ByteBuffer from, @NonNull ByteProcessor<?> to) throws IOException {
    if (!from.hasRemaining()) {
        return true;
    }
    if (from.hasArray()) {
        boolean p = to.processBytes(from.array(), from.arrayOffset() + from.position(), from.remaining());
        from.position(from.limit());
        return p;
    }
    final byte[] bytes = threadLocalArray8K();
    int r;
    boolean p;
    do {
        r = Math.min(from.remaining(), bytes.length);
        from.get(bytes, 0, r);
    } while ((p = to.processBytes(bytes, 0, r)) && from.hasRemaining());
    return p;
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code from} is not closed.
 * Byte processing stops on EOF or when/*from   w ww. jav a  2 s . c om*/
 * {@link ByteProcessor#processBytes(byte[], int, int) processBytes} returns
 * {@code false}.
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull @WillNotClose InputStream from, @NonNull ByteProcessor<?> to, long limit)
        throws IOException {
    checkLimit(limit);
    final byte[] buf = threadLocalArray8K();
    long total = 0;
    boolean proc = true;
    while (total < limit && proc) {
        int r = from.read(buf, 0, (int) Math.min(buf.length, limit - total));
        if (r > 0) {
            proc = to.processBytes(buf, 0, r);
            total += r;
        } else if (r < 0) {
            break;
        }
    }
    return total;
}