Example usage for org.apache.commons.io.output CountingOutputStream write

List of usage examples for org.apache.commons.io.output CountingOutputStream write

Introduction

In this page you can find the example usage for org.apache.commons.io.output CountingOutputStream write.

Prototype

public void write(byte[] b, int off, int len) throws IOException 

Source Link

Document

Writes a portion of the specified byte array to this output stream keeping count of the number of bytes written.

Usage

From source file:hudson.model.LargeText.java

/**
 * Writes the tail portion of the file to the {@link Writer}.
 *
 * <p>/*w  w  w  . j  av  a 2 s .c o  m*/
 * The text file is assumed to be in the system default encoding.
 *
 * @param start
 *      The byte offset in the input file where the write operation starts.
 *
 * @return
 *      if the file is still being written, this method writes the file
 *      until the last newline character and returns the offset to start
 *      the next write operation.
 */
public long writeLogTo(long start, Writer w) throws IOException {
    CountingOutputStream os = new CountingOutputStream(new WriterOutputStream(w));

    Session f = source.open();
    f.skip(start);

    if (completed) {
        // write everything till EOF
        byte[] buf = new byte[1024];
        int sz;
        while ((sz = f.read(buf)) >= 0)
            os.write(buf, 0, sz);
    } else {
        ByteBuf buf = new ByteBuf(null, f);
        HeadMark head = new HeadMark(buf);
        TailMark tail = new TailMark(buf);

        while (tail.moveToNextLine(f)) {
            head.moveTo(tail, os);
        }
        head.finish(os);
    }

    f.close();
    os.flush();

    return os.getCount() + start;
}

From source file:org.jenkinsci.plugins.fabric8.support.hack.LargeText.java

/**
 * Writes the tail portion of the file to the {@link OutputStream}.
 *
 * @param start//from w  w w .  j a v a 2 s .c om
 *      The byte offset in the input file where the write operation starts.
 *
 * @return
 *      if the file is still being written, this method writes the file
 *      until the last newline character and returns the offset to start
 *      the next write operation.
 */
public long writeLogTo(long start, OutputStream out) throws IOException {
    CountingOutputStream os = new CountingOutputStream(out);

    Session f = source.open();
    f.skip(start);

    if (completed) {
        // write everything till EOF
        byte[] buf = new byte[1024];
        int sz;
        while ((sz = f.read(buf)) >= 0)
            os.write(buf, 0, sz);
    } else {
        ByteBuf buf = new ByteBuf(null, f);
        HeadMark head = new HeadMark(buf);
        TailMark tail = new TailMark(buf);
        buf = null;

        int readLines = 0;
        while (tail.moveToNextLine(f) && readLines++ < MAX_LINES_READ) {
            head.moveTo(tail, os);
        }
        head.finish(os);
    }

    f.close();
    os.flush();

    return os.getCount() + start;
}

From source file:org.jenkinsci.plugins.fabric8.support.hack.LargeText.java

/**
 * Writes the section of the file {@link OutputStream}.
 *
 * @param start//w w w .  j  a v  a2 s . com
 *      The byte offset in the input file where the write operation starts.
 *
 * @return
 *      if the file is still being written, this method writes the file
 *      until the last newline character and returns the offset to start
 *      the next write operation.
 */
public long writeLogTo(long start, int size, OutputStream out) throws IOException {
    if (size <= 0) {
        return 0;
    }

    CountingOutputStream os = new CountingOutputStream(out);

    Session f = source.open();
    f.skip(start);

    long end = start + size;

    byte[] buf = new byte[size];
    int sz;
    if ((sz = f.read(buf)) >= 0) {
        os.write(buf, 0, sz);
    }
    /*
            if(completed) {
            } else {
    ByteBuf buf = new ByteBuf(null,f, size);
    HeadMark head = new HeadMark(buf);
    TailMark tail = new TailMark(buf);
            
    int readLines = 0;
    while(tail.moveToNextLine(f) && readLines++ < MAX_LINES_READ) {
        head.moveTo(tail, os);
        if (buf.isFull() || os.getCount() >= end) {
            break;
        }
    }
    head.finish(os);
            }
    */

    f.close();
    os.flush();

    return os.getCount() + start;
}