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

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

Introduction

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

Prototype

public void setPropagateClose(boolean propagateClose) 

Source Link

Document

Set whether the #close() method should propagate to the underling InputStream .

Usage

From source file:com.github.horrorho.inflatabledonkey.chunk.engine.ChunkListDecrypter.java

BoundedInputStream boundedInputStream(InputStream inputStream, int length) {
    BoundedInputStream bis = new BoundedInputStream(inputStream, length); // No close() required/ not propagated.
    bis.setPropagateClose(false);
    return bis;/*from   w  ww . j  av  a  2 s.c o  m*/
}

From source file:com.joyent.manta.client.crypto.MantaEncryptedObjectInputStream.java

/**
 * Creates a new instance of a {@link CipherInputStream} based on the parameters
 * returned as HTTP headers for the object.
 *
 * @return a configured decrypting stream
 *//*from w  ww. j  a v  a 2 s. c o  m*/
private InputStream createCryptoStream() {
    final InputStream source;
    boolean isRangeRequest = (plaintextRangeLength != null && plaintextRangeLength > 0L);

    // No need to calculate HMAC because we are using a AEAD cipher
    if (this.cipherDetails.isAEADCipher()) {
        source = super.getBackingStream();
        /* Since we are doing EtM authentication with the non-GCM cipher modes,
         * we need to exclude the binary HMAC bytes from the stream that the
         * CipherInputStream is reading (otherwise it will think it is ciphertext).
         * That is why we wrap the source stream in a bounded stream that prevents
         * the closing of the underlying stream - it allows us to read the final
         * HMAC bytes upon close(). */
    } else {
        final long adjustedContentLength;
        final long hmacSize;

        if (this.hmac == null) {
            hmacSize = this.cipherDetails.getAuthenticationTagOrHmacLengthInBytes();
        } else {
            hmacSize = this.hmac.getMacSize();
        }

        if (!isRangeRequest || this.unboundedEnd) {
            adjustedContentLength = this.contentLength - hmacSize;
        } else {
            adjustedContentLength = this.contentLength;
        }

        BoundedInputStream bin = new BoundedInputStream(super.getBackingStream(), adjustedContentLength);
        bin.setPropagateClose(false);
        source = bin;
    }

    final CipherInputStream cin = new CipherInputStream(source, this.cipher);

    /* A plaintext value not null indicates that we aren't working with
     * a subset of the total object (byte range), so we can just pass back
     * the ciphertext stream without any limitations on its length. */
    if (!isRangeRequest) {
        return cin;
    }

    // If we have gotten this far, we are dealing with a byte range

    /* We adjust the maximum number of plaintext bytes that can be returned
     * as the plaintext length + skipped bytes because the plaintext length
     * already has the skipped bytes subtracted from it. */

    return new BoundedInputStream(cin, this.plaintextRangeLength + this.initialBytesToSkip);
}

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

/**
 * Writes the data/*www  .  j  a va 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);
    }
}

From source file:org.fastcatsearch.ir.document.DocumentReader.java

public Document readDocument(int docNo, boolean[] fieldSelectOption, boolean indexable) throws IOException {
    // if(docNo < baseDocNo) throw new
    // IOException("Request docNo cannot less than baseDocNo! docNo = "+docNo+", baseDocNo = "+baseDocNo);

    // baseDocNo?    .
    // docNo -= baseDocNo;

    DataInput bai = null;//from  w  w w .  ja va  2s  .  com

    if (docNo != lastDocNo) {
        long positionOffset = docNo * IOUtil.SIZE_OF_LONG;
        if (positionOffset >= positionLimit) {
            //.
            return null;
        }
        positionInput.seek(positionOffset);
        long pos = positionInput.readLong();
        // find a document block
        docInput.seek(pos);
        int len = docInput.readInt();

        //2014-11-26 ?  working ?   ? ? GC ?? OOM ? ?.
        // Stream  .
        InflaterInputStream decompressInputStream = null;
        inflaterOutput.reset();
        int count = -1;
        try {
            BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len);
            boundedInputStream.setPropagateClose(false);// docInput  .
            decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512);
            while ((count = decompressInputStream.read(workingBuffer)) != -1) {
                inflaterOutput.write(workingBuffer, 0, count);
            }
        } finally {
            decompressInputStream.close();
        }

        BytesRef bytesRef = inflaterOutput.getBytesRef();
        bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length);

        lastDocNo = docNo;
        lastBai = bai;
    } else {
        lastBai.reset();
        bai = lastBai;
    }

    Document document = new Document(fields.size());
    for (int i = 0; i < fields.size(); i++) {
        FieldSetting fs = fields.get(i);
        Field f = null;
        boolean hasValue = bai.readBoolean();
        //         logger.debug("read hasValue={}, select={}, fs={} ", hasValue, fieldSelectOption, fs);
        if (hasValue) {
            //1. fieldSelectOption ?  ? ??.
            //2. ? , true? ? ?.
            if (fieldSelectOption == null || (fieldSelectOption != null && fieldSelectOption[i])) {
                f = fs.createEmptyField();
                f.readRawFrom(bai);
            } else {
                bai.skipVIntData();
            }
            //            logger.debug("fill {} >> {}", i, f);
        } else {
            //?  ?   .
            f = fs.createEmptyField();
            //            logger.debug("fill {} >> empty", i);
        }
        if (f != null && indexable) {
            String multiValueDelimiter = fs.getMultiValueDelimiter();
            try {
                f.parseIndexable(multiValueDelimiter);
            } catch (FieldDataParseException e) {
                throw new IOException(e);
            }
        }
        document.set(i, f);
    }

    document.setDocId(docNo + baseDocNo);

    return document;
}

From source file:org.fastcatsearch.ir.document.DocumentWriter.java

public Document readDocument(int docNo) throws IOException, IRException {
    long prevPosPos = positionOutput.position();
    long docPos = -1;
    try {//from   w w  w.java2s  .co m
        long positionOffset = ((long) docNo) * IOUtil.SIZE_OF_LONG;
        positionOutput.seek(positionOffset);
        docPos = IOUtil.readLong(positionOutput.getRaf());
    } finally {
        positionOutput.seek(prevPosPos);
    }

    // find a document block
    long prevDocPos = docOutput.position();
    try {
        docOutput.seek(docPos);
        RandomAccessFile raf = docOutput.getRaf();
        int len = IOUtil.readInt(raf);
        long n = raf.getFilePointer();
        InputStream docInput = Channels.newInputStream(docOutput.getRaf().getChannel().position(n));
        //2014-11-26 ?  working ?   ? ? GC ?? OOM ? ?.
        // Stream  .
        InflaterInputStream decompressInputStream = null;
        inflaterOutput.reset();
        int count = -1;
        try {
            BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len);
            boundedInputStream.setPropagateClose(false);// docInput  .
            decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512);
            while ((count = decompressInputStream.read(workingBuffer)) != -1) {
                inflaterOutput.write(workingBuffer, 0, count);
            }
        } finally {
            decompressInputStream.close();
        }
    } finally {
        docOutput.seek(prevDocPos);
    }

    BytesRef bytesRef = inflaterOutput.getBytesRef();
    DataInput bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length);

    Document document = new Document(fields.size());
    for (int i = 0; i < fields.size(); i++) {
        FieldSetting fs = fields.get(i);
        Field f = null;
        boolean hasValue = bai.readBoolean();
        if (hasValue) {
            f = fs.createEmptyField();
            f.readRawFrom(bai);
        } else {
            f = fs.createEmptyField();
        }
        if (f != null) {
            String multiValueDelimiter = fs.getMultiValueDelimiter();
            try {
                f.parseIndexable(multiValueDelimiter);
            } catch (FieldDataParseException e) {
                throw new IOException(e);
            }
        }
        document.add(f);
    }
    document.setDocId(docNo);
    return document;
}