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

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

Introduction

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

Prototype

public synchronized int getCount() 

Source Link

Document

The number of bytes that have passed through this stream.

Usage

From source file:jp.go.nict.langrid.foundation.servlet.ResponseProcessor.java

static ServiceResponse processTemplate(LangridException exception, String hostName, OutputStream out)
        throws IOException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("escapeUtils", StringEscapeUtils.class);
    map.put("exception", exception);
    map.put("hostName", hostName);
    String cn = exception.getClass().getName();
    map.put("nsSuffix", "");

    List<Attribute> properties = new ArrayList<Attribute>();
    for (Method m : exception.getClass().getDeclaredMethods()) {
        String methodName = m.getName();
        if (!methodName.startsWith("get"))
            continue;

        try {/*  www .ja va 2s.c  o  m*/
            String name = m.getName().substring("get".length());
            name = name.substring(0, 1).toLowerCase() + name.substring(1);
            String value = (String) m.invoke(exception);
            if (value == null)
                continue;
            properties.add(new Attribute(name, value));
        } catch (ClassCastException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
    }
    map.put("properties", properties);

    if (cn.startsWith("jp.go.nict.langrid.service_1_2.")) {
        String prefix = cn.substring("jp.go.nict.langrid.service_1_2.".length());
        int i = prefix.lastIndexOf(".");
        if (i != -1) {
            map.put("nsSuffix", prefix.substring(0, i).replace(".", "/") + "/");
        }
    }
    CountingOutputStream co = new CountingOutputStream(out);
    OutputStreamWriter w = new OutputStreamWriter(co);
    exceptionTemplate.make(map).writeTo(w);
    w.flush();

    return new ServiceResponse(500, co.getCount(), "soapenv:Server.userException", exception.getDescription());
}

From source file:com.discursive.jccook.io.CountingOutExample.java

public void start() {

    File test = new File("test.dat");
    CountingOutputStream countStream = null;

    try {//from   w ww .j a  v a  2s .co  m
        FileOutputStream fos = new FileOutputStream(test);
        countStream = new CountingOutputStream(fos);
        countStream.write("Hello".getBytes());
    } catch (IOException ioe) {
        System.out.println("Error writing bytes to file.");
    } finally {
        IOUtils.closeQuietly(countStream);
    }

    if (countStream != null) {
        int bytesWritten = countStream.getCount();
        System.out.println("Wrote " + bytesWritten + " bytes to test.dat");
    }

}

From source file:com.datatorrent.contrib.hdht.MockFileAccess.java

@Override
public FileWriter getWriter(final long bucketKey, final String fileName) throws IOException {
    final DataOutputStream dos = getOutputStream(bucketKey, fileName);
    final CountingOutputStream cos = new CountingOutputStream(dos);
    final Output out = new Output(cos);

    return new FileWriter() {
        @Override//from   w w w.  j a  v  a2s  .c  o m
        public void close() throws IOException {
            out.close();
            cos.close();
            dos.close();
        }

        @Override
        public void append(byte[] key, byte[] value) throws IOException {
            kryo.writeObject(out, key);
            kryo.writeObject(out, value);
        }

        @Override
        public long getBytesWritten() {
            return cos.getCount() + out.position();
        }

    };

}

From source file:hudson.model.LargeText.java

/**
 * Writes the tail portion of the file to the {@link Writer}.
 *
 * <p>/*from   w  ww.  j a  v  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.apache.flex.swf.io.SWFWriter.java

@Override
public int writeTo(File outputFile) throws FileNotFoundException, IOException {
    // Ensure that the directory for the SWF exists.
    final File outputDirectory = new File(outputFile.getAbsoluteFile().getParent());
    outputDirectory.mkdirs();/*  ww  w . j a  va2  s.  c  o  m*/

    // Write out the SWF, counting how many bytes were written.
    final CountingOutputStream output = new CountingOutputStream(
            new BufferedOutputStream(new FileOutputStream(outputFile)));
    writeTo(output);
    output.flush();
    output.close();
    close();

    final int swfSize = output.getCount();
    return swfSize;
}

From source file:org.apache.flex.swf.io.SWFWriterAndSizeReporter.java

@Override
public void writeTo(OutputStream output) {
    CountingOutputStream countingOutput = new CountingOutputStream(output);
    super.writeTo(countingOutput);

    report.setCompressedSize(countingOutput.getCount());

    writeSizeReport();//from   www  . jav  a 2  s.co m
}

From source file:org.apache.fop.pdf.AbstractPDFStream.java

/**
 * Encodes and writes a stream directly to an OutputStream. The length of
 * the stream, in this case, is set on a PDFNumber object that has to be
 * prepared beforehand.//from w  w w .java 2  s .c o  m
 * @param out OutputStream to write to
 * @param refLength PDFNumber object to receive the stream length
 * @return number of bytes written (header and trailer included)
 * @throws IOException in case of an I/O problem
 */
protected int encodeAndWriteStream(OutputStream out, PDFNumber refLength) throws IOException {
    int bytesWritten = 0;
    //Stream header
    byte[] buf = encode("stream\n");
    out.write(buf);
    bytesWritten += buf.length;

    //Stream contents
    CloseBlockerOutputStream cbout = new CloseBlockerOutputStream(out);
    CountingOutputStream cout = new CountingOutputStream(cbout);
    OutputStream filteredOutput = getFilterList().applyFilters(cout);
    outputRawStreamData(filteredOutput);
    filteredOutput.close();
    refLength.setNumber(Integer.valueOf(cout.getCount()));
    bytesWritten += cout.getCount();

    //Stream trailer
    buf = encode("\nendstream");
    out.write(buf);
    bytesWritten += buf.length;

    return bytesWritten;
}

From source file:org.apache.fop.pdf.AbstractPDFStream.java

/**
 * Overload the base object method so we don't have to copy
 * byte arrays around so much//from w  ww  .  j  a va2  s. c o  m
 * {@inheritDoc}
 */
@Override
public int output(OutputStream stream) throws IOException {
    setupFilterList();

    CountingOutputStream cout = new CountingOutputStream(stream);
    StringBuilder textBuffer = new StringBuilder(64);

    StreamCache encodedStream = null;
    PDFNumber refLength = null;
    final Object lengthEntry;
    if (encodeOnTheFly) {
        refLength = new PDFNumber();
        getDocumentSafely().registerObject(refLength);
        lengthEntry = refLength;
    } else {
        encodedStream = encodeStream();
        lengthEntry = Integer.valueOf(encodedStream.getSize() + 1);
    }

    populateStreamDict(lengthEntry);
    dictionary.writeDictionary(cout, textBuffer);

    //Send encoded stream to target OutputStream
    PDFDocument.flushTextBuffer(textBuffer, cout);
    if (encodedStream == null) {
        encodeAndWriteStream(cout, refLength);
    } else {
        outputStreamData(encodedStream, cout);
        encodedStream.clear(); //Encoded stream can now be discarded
    }

    PDFDocument.flushTextBuffer(textBuffer, cout);
    return cout.getCount();
}

From source file:org.apache.fop.pdf.PDFArray.java

/** {@inheritDoc} */
@Override//  w w w .ja v  a  2s.c  o  m
public int output(OutputStream stream) throws IOException {
    CountingOutputStream cout = new CountingOutputStream(stream);
    StringBuilder textBuffer = new StringBuilder(64);
    textBuffer.append('[');
    for (int i = 0; i < values.size(); i++) {
        if (i > 0) {
            textBuffer.append(' ');
        }
        Object obj = this.values.get(i);
        formatObject(obj, cout, textBuffer);
    }
    textBuffer.append(']');
    PDFDocument.flushTextBuffer(textBuffer, cout);
    return cout.getCount();
}

From source file:org.apache.fop.pdf.PDFDestination.java

@Override
public int output(OutputStream stream) throws IOException {
    CountingOutputStream cout = new CountingOutputStream(stream);
    StringBuilder textBuffer = new StringBuilder(64);

    formatObject(getIDRef(), cout, textBuffer);
    textBuffer.append(' ');
    formatObject(goToReference, cout, textBuffer);

    PDFDocument.flushTextBuffer(textBuffer, cout);
    return cout.getCount();
}