Example usage for org.apache.commons.io.input BoundedInputStream read

List of usage examples for org.apache.commons.io.input BoundedInputStream read

Introduction

In this page you can find the example usage for org.apache.commons.io.input BoundedInputStream read.

Prototype

@Override
public int read(byte[] b) throws IOException 

Source Link

Document

Invokes the delegate's read(byte[]) method.

Usage

From source file:org.apache.wicket.request.resource.PartWriterCallback.java

/**
 * Writes the data//from  ww w .j  ava  2 s.  c o  m
 *
 * @param attributes
 *            the attributes to get the output stream of the response
 * @throws IOException
 *             if something went wrong while writing the data to the output stream
 */
@Override
public void writeData(Attributes attributes) throws IOException {
    try {
        OutputStream outputStream = attributes.getResponse().getOutputStream();
        byte[] buffer = new byte[getBufferSize()];

        if (startbyte != null || endbyte != null) {
            // skipping the first bytes which are
            // requested to be skipped by the client
            if (startbyte != null) {
                inputStream.skip(startbyte);
            } else {
                // If no start byte has been given set it to 0
                // which means no bytes has been skipped
                startbyte = 0L;
            }

            // If there are no end bytes given read the whole stream till the end
            if (endbyte == null || Long.valueOf(-1).equals(endbyte)) {
                endbyte = contentLength;
            }

            BoundedInputStream boundedInputStream = null;
            try {
                // Stream is going to be read from the starting point next to the skipped bytes
                // till the end byte computed by the range between startbyte / endbyte
                boundedInputStream = new BoundedInputStream(inputStream, (endbyte - startbyte) + 1);

                // The original input stream is going to be closed by the end of the request
                // so set propagate close to false
                boundedInputStream.setPropagateClose(false);

                // The read bytes in the current buffer
                int readBytes;

                while ((readBytes = boundedInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
            } finally {
                IOUtils.closeQuietly(boundedInputStream);
            }
        } else {
            // No range has been given so copy the content
            // from input stream to the output stream
            Streams.copy(inputStream, outputStream, getBufferSize());
        }
    } catch (ResponseIOException e) {
        // the client has closed the connection and
        // doesn't read the stream further on
        // (in tomcats
        // org.apache.catalina.connector.ClientAbortException)
        // we ignore this case
    }
    if (close) {
        IOUtils.close(inputStream);
    }
}