Example usage for java.util.zip InflaterInputStream close

List of usage examples for java.util.zip InflaterInputStream close

Introduction

In this page you can find the example usage for java.util.zip InflaterInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

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  .  ja  v a2s. c  o 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;
}

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;/*  www . ja  v a 2s  .c o m*/

    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:com.qut.middleware.esoe.sso.plugins.redirect.handler.impl.RedirectLogicImpl.java

private AuthnRequest getRedirectRequest(SSOProcessorData data, RedirectBindingData bindingData)
        throws RedirectBindingException {
    InflaterInputStream inflaterStream = null;
    ByteArrayOutputStream inflatedByteStream = null;
    byte[] chunk = new byte[TMP_BUFFER_SIZE];

    boolean signed = (bindingData.getSignature() != null && bindingData.getSignature().length() > 0);

    SSOProcessor ssoProcessor = data.getSSOProcessor();
    String remoteAddress = data.getRemoteAddress();

    try {/*  w w  w. j av  a 2s.  c o  m*/
        if (bindingData.getSAMLRequestString() == null || bindingData.getSAMLRequestString().length() <= 0) {
            ssoProcessor.createStatusAuthnResponse(data, StatusCodeConstants.requester, null,
                    "No AuthnRequest document was supplied to the Redirect binding.", true);
            this.logger.error(
                    "[SSO for {}] Redirect binding failed: No AuthnRequest document was supplied in the request.",
                    remoteAddress);
            throw new RedirectBindingException(
                    "Redirect binding failed as no AuthnRequest document was supplied in the request.");
        }

        if (bindingData.getRequestEncoding() != null
                && !bindingData.getRequestEncoding().equals(BindingConstants.deflateEncoding)) {
            ssoProcessor.createStatusAuthnResponse(data, StatusCodeConstants.requester, null,
                    "The given SAML Request encoding is not supported in this implementation.", true);
            this.logger.error(
                    "[SSO for {}] Redirect binding failed: SAML Request encoding '{}' is not supported in the current implementation.",
                    new Object[] { remoteAddress, bindingData.getRequestEncoding() });
            throw new RedirectBindingException(
                    "Redirect binding failed as the given SAML Request encoding is not supported in the current implementation.");
        }

        if (bindingData.getSignature() != null) {
            ssoProcessor.createStatusAuthnResponse(data, StatusCodeConstants.requester, null,
                    "Signed Redirect binding documents are not supported in this implementation.", true);
            this.logger.error(
                    "[SSO for {}] Redirect binding failed: Signed Redirect binding documents are not supported in the current implementation.",
                    remoteAddress);
            throw new RedirectBindingException(
                    "Redirect binding failed as Signed Redirect binding documents are not supported in the current implementation.");
        }
    } catch (SSOException e) {
        this.logger.error("[SSO for {}] Redirect binding failed to generate an error response. Error was: {}",
                new Object[] { remoteAddress, e.getMessage() });
        throw new RedirectBindingException(
                "Redirect binding failed to generate an error reponse. Original error follows", e);
    }

    try {
        /*
         * Retrieves the AuthnRequest from the encoded and compressed String extracted from the request of SAML HTTP
         * Redirect. The AuthnRequest XML is retrieved in the following order: 1. Base64 decode, 2. Inflate
         */
        byte[] decodedBytes = Base64.decodeBase64(bindingData.getSAMLRequestString().getBytes());
        ByteArrayInputStream decodedByteStream = new ByteArrayInputStream(decodedBytes);
        inflaterStream = new InflaterInputStream(decodedByteStream, new Inflater(true));
        inflatedByteStream = new ByteArrayOutputStream();

        int writeCount = 0;
        int count = 0;
        // Inflate and dump in the output stream to build a byte array.
        while ((count = inflaterStream.read(chunk)) >= 0) {
            inflatedByteStream.write(chunk, 0, count);
            writeCount = writeCount + count;
        }

        byte[] samlRequestDocument = inflatedByteStream.toByteArray();

        AuthnRequest authnRequest = ssoProcessor.unmarshallRequest(samlRequestDocument, signed);
        this.logger.debug("[SSO for {}] AuthnRequest was unmarshalled successfully by the SSO Processor",
                remoteAddress);

        return authnRequest;
    } catch (IOException e) {
        this.logger.error(
                "[SSO for {}] IO exception occurred while inflating the request document. Error was: {}",
                new Object[] { remoteAddress, e.getMessage() });
        throw new RedirectBindingException("IO exception occurred while inflating the request document.");
    } catch (SignatureValueException e) {
        this.logger.error(
                "[SSO for {}] Signature value exception occurred while trying to unmarshal the redirect request. Error was: {}",
                new Object[] { remoteAddress, e.getMessage() });
        this.logger.debug(
                "[SSO for {}] Signature value exception occurred while trying to unmarshal the redirect request. Exception follows",
                remoteAddress, e);
        throw new RedirectBindingException(
                "Signature value exception occurred while trying to unmarshal the redirect request.");
    } catch (ReferenceValueException e) {
        this.logger.error(
                "[SSO for {}] Reference value exception occurred while unmarshalling the redirect request. Error was: {}",
                new Object[] { remoteAddress, e.getMessage() });
        this.logger.debug(
                "[SSO for {}] Reference value exception occurred while unmarshalling the redirect request. Exception follows",
                remoteAddress, e);
        throw new RedirectBindingException(
                "Reference value exception occurred while unmarshalling the redirect request.");
    } catch (UnmarshallerException e) {
        this.logger.error(
                "[SSO for {}] Unmarshaller exception occurred while unmarshalling the redirect request. Error was: {}",
                new Object[] { remoteAddress, e.getMessage() });
        this.logger.debug(
                "[SSO for {}] Unmarshaller exception occurred while unmarshalling the redirect request. Exception follows",
                remoteAddress, e);
        throw new RedirectBindingException(
                "Unmarshaller exception occurred while unmarshalling the redirect request.");
    } finally {
        try {
            if (inflatedByteStream != null) {
                inflatedByteStream.reset();
                inflatedByteStream.close();
            }

            if (inflaterStream != null)
                inflaterStream.close();
        } catch (IOException e) {
            this.logger.error("Unable to close stream correctly - " + e.getLocalizedMessage());
            this.logger.debug(e.getLocalizedMessage(), e);
        }
    }
}