Example usage for java.util.zip Inflater Inflater

List of usage examples for java.util.zip Inflater Inflater

Introduction

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

Prototype

public Inflater() 

Source Link

Document

Creates a new decompressor.

Usage

From source file:org.barcelonamedia.uima.tools.docanalyzer.DBAnnotationViewerDialog.java

public void launchThatViewer(String xmi_id, TypeSystem typeSystem, final String[] aTypesToDisplay,
        boolean javaViewerRBisSelected, boolean javaViewerUCRBisSelected, boolean xmlRBisSelected,
        File styleMapFile, File viewerDirectory) {

    try {/*  w w w .java  2s. c  o  m*/

        InputStream xmiDocument = this.xmiDAO.getXMI(xmi_id);

        // create a new CAS
        CAS cas = CasCreationUtils.createCas(Collections.EMPTY_LIST, typeSystem,
                UIMAFramework.getDefaultPerformanceTuningProperties());

        if (this.med1.isUmcompress()) {

            //Descomprime el XMI

            //Create the decompressor and give it the data to compress
            Inflater decompressor = new Inflater();
            byte[] documentDataByteArray = IOUtils.toByteArray(xmiDocument);

            decompressor.setInput(documentDataByteArray);

            //Create an expandable byte array to hold the decompressed data
            ByteArrayOutputStream bos = new ByteArrayOutputStream(documentDataByteArray.length);

            //Decompress the data
            byte[] buf = new byte[1024];

            while (!decompressor.finished()) {

                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }

            bos.close();

            //Get the decompressed data
            byte[] decompressedData = bos.toByteArray();

            XmiCasDeserializer.deserialize(new ByteArrayInputStream(decompressedData), cas, true);
        } else {

            XmlCasDeserializer.deserialize(xmiDocument, cas, true);
        }

        //get the specified view
        cas = cas.getView(this.defaultCasViewName);

        //launch appropriate viewer
        if (javaViewerRBisSelected || javaViewerUCRBisSelected) { // JMP

            // record preference for next time
            med1.setViewType(javaViewerRBisSelected ? "Java Viewer" : "JV User Colors");

            //create tree viewer component
            CasAnnotationViewer viewer = new CasAnnotationViewer();
            viewer.setDisplayedTypes(aTypesToDisplay);

            if (javaViewerUCRBisSelected)
                getColorsForTypesFromFile(viewer, styleMapFile);
            else
                viewer.setHiddenTypes(new String[] { "uima.cpm.FileLocation" });

            // launch viewer in a new dialog
            viewer.setCAS(cas);
            JDialog dialog = new JDialog(DBAnnotationViewerDialog.this,
                    "Annotation Results for " + xmi_id + " in " + inputDirPath); // JMP
            dialog.getContentPane().add(viewer);
            dialog.setSize(850, 630);
            dialog.pack();
            dialog.show();
        } else {

            CAS defaultView = cas.getView(CAS.NAME_DEFAULT_SOFA);

            if (defaultView.getDocumentText() == null) {
                displayError(
                        "The HTML and XML Viewers can only view the default text document, which was not found in this CAS.");
                return;
            }

            // generate inline XML
            File inlineXmlFile = new File(viewerDirectory, "inline.xml");
            String xmlAnnotations = new CasToInlineXml().generateXML(defaultView);
            FileOutputStream outStream = new FileOutputStream(inlineXmlFile);
            outStream.write(xmlAnnotations.getBytes("UTF-8"));
            outStream.close();

            if (xmlRBisSelected) // JMP passed in
            {
                // record preference for next time
                med1.setViewType("XML");

                BrowserUtil.openUrlInDefaultBrowser(inlineXmlFile.getAbsolutePath());
            } else
            // HTML view
            {
                med1.setViewType("HTML");
                // generate HTML view
                // first process style map if not done already
                if (!processedStyleMap) {
                    if (!styleMapFile.exists()) {
                        annotationViewGenerator.autoGenerateStyleMapFile(
                                promptForAE().getAnalysisEngineMetaData(), styleMapFile);
                    }
                    annotationViewGenerator.processStyleMap(styleMapFile);
                    processedStyleMap = true;
                }
                annotationViewGenerator.processDocument(inlineXmlFile);
                File genFile = new File(viewerDirectory, "index.html");
                // open in browser
                BrowserUtil.openUrlInDefaultBrowser(genFile.getAbsolutePath());
            }
        }

        // end LTV here

    } catch (Exception ex) {

        displayError(ex);
    }
}

From source file:org.apache.uima.tools.docanalyzer.DBAnnotationViewerDialog.java

public void launchThatViewer(String xmi_id, TypeSystem typeSystem, final String[] aTypesToDisplay,
        boolean javaViewerRBisSelected, boolean javaViewerUCRBisSelected, boolean xmlRBisSelected,
        File styleMapFile, File viewerDirectory) {

    try {//from w w w . j a  va  2 s.  co m

        InputStream xmiDocument = this.xmiDAO.getXMI(xmi_id);

        // create a new CAS
        CAS cas = CasCreationUtils.createCas(Collections.EMPTY_LIST, typeSystem,
                UIMAFramework.getDefaultPerformanceTuningProperties());

        if (this.med1.isUmcompress()) {

            //Descomprime el XMI
            System.out.println("con compresin");

            //Create the decompressor and give it the data to compress
            Inflater decompressor = new Inflater();
            byte[] documentDataByteArray = IOUtils.toByteArray(xmiDocument);

            decompressor.setInput(documentDataByteArray);

            //Create an expandable byte array to hold the decompressed data
            ByteArrayOutputStream bos = new ByteArrayOutputStream(documentDataByteArray.length);

            //Decompress the data
            byte[] buf = new byte[1024];

            while (!decompressor.finished()) {

                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }

            bos.close();

            //Get the decompressed data
            byte[] decompressedData = bos.toByteArray();

            XmiCasDeserializer.deserialize(new ByteArrayInputStream(decompressedData), cas, true);
        } else {

            System.out.println("sin compresin");
            XmlCasDeserializer.deserialize(xmiDocument, cas, true);
        }

        //get the specified view
        cas = cas.getView(this.defaultCasViewName);

        //launch appropriate viewer
        if (javaViewerRBisSelected || javaViewerUCRBisSelected) { // JMP

            // record preference for next time
            med1.setViewType(javaViewerRBisSelected ? "Java Viewer" : "JV User Colors");

            //create tree viewer component
            CasAnnotationViewer viewer = new CasAnnotationViewer();
            viewer.setDisplayedTypes(aTypesToDisplay);

            if (javaViewerUCRBisSelected)
                getColorsForTypesFromFile(viewer, styleMapFile);
            else
                viewer.setHiddenTypes(new String[] { "uima.cpm.FileLocation" });

            // launch viewer in a new dialog
            viewer.setCAS(cas);
            JDialog dialog = new JDialog(DBAnnotationViewerDialog.this,
                    "Annotation Results for " + xmi_id + " in " + inputDirPath); // JMP
            dialog.getContentPane().add(viewer);
            dialog.setSize(850, 630);
            dialog.pack();
            dialog.show();
        } else {

            CAS defaultView = cas.getView(CAS.NAME_DEFAULT_SOFA);

            if (defaultView.getDocumentText() == null) {
                displayError(
                        "The HTML and XML Viewers can only view the default text document, which was not found in this CAS.");
                return;
            }

            // generate inline XML
            File inlineXmlFile = new File(viewerDirectory, "inline.xml");
            String xmlAnnotations = new CasToInlineXml().generateXML(defaultView);
            FileOutputStream outStream = new FileOutputStream(inlineXmlFile);
            outStream.write(xmlAnnotations.getBytes("UTF-8"));
            outStream.close();

            if (xmlRBisSelected) // JMP passed in
            {
                // record preference for next time
                med1.setViewType("XML");

                BrowserUtil.openUrlInDefaultBrowser(inlineXmlFile.getAbsolutePath());
            } else
            // HTML view
            {
                med1.setViewType("HTML");
                // generate HTML view
                // first process style map if not done already
                if (!processedStyleMap) {
                    if (!styleMapFile.exists()) {
                        annotationViewGenerator.autoGenerateStyleMapFile(
                                promptForAE().getAnalysisEngineMetaData(), styleMapFile);
                    }
                    annotationViewGenerator.processStyleMap(styleMapFile);
                    processedStyleMap = true;
                }
                annotationViewGenerator.processDocument(inlineXmlFile);
                File genFile = new File(viewerDirectory, "index.html");
                // open in browser
                BrowserUtil.openUrlInDefaultBrowser(genFile.getAbsolutePath());
            }
        }

        // end LTV here

    } catch (Exception ex) {

        displayError(ex);
    }
}

From source file:org.ajax4jsf.resource.ResourceBuilderImpl.java

protected byte[] decrypt(byte[] src) {
    try {/*  w w w.  j a  v  a2 s  .  co  m*/
        byte[] zipsrc = codec.decode(src);
        Inflater decompressor = new Inflater();
        byte[] uncompressed = new byte[zipsrc.length * 5];
        decompressor.setInput(zipsrc);
        int totalOut = decompressor.inflate(uncompressed);
        byte[] out = new byte[totalOut];
        System.arraycopy(uncompressed, 0, out, 0, totalOut);
        decompressor.end();
        return out;
    } catch (Exception e) {
        throw new FacesException("Error decode resource data", e);
    }
}

From source file:nl.nn.adapterframework.util.JdbcUtil.java

public static String getBlobAsString(Blob blob, String column, String charset, boolean xmlEncode,
        boolean blobIsCompressed, boolean blobSmartGet, boolean encodeBlobBase64)
        throws IOException, JdbcException, SQLException, JMSException {
    if (encodeBlobBase64) {
        InputStream blobStream = JdbcUtil.getBlobInputStream(blob, column, blobIsCompressed);
        return Misc.streamToString(new Base64InputStream(blobStream, true), null, false);
    }/*from ww w  .j  a va  2s .  c o  m*/
    if (blobSmartGet) {
        if (blob == null) {
            log.debug("no blob found in column [" + column + "]");
            return null;
        }
        int bl = (int) blob.length();

        InputStream is = blob.getBinaryStream();
        byte[] buf = new byte[bl];
        int bl1 = is.read(buf);

        Inflater decompressor = new Inflater();
        decompressor.setInput(buf);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);
        byte[] bufDecomp = new byte[1024];
        boolean decompresOK = true;
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(bufDecomp);
                if (count == 0) {
                    break;
                }
                bos.write(bufDecomp, 0, count);
            } catch (DataFormatException e) {
                log.debug("message in column [" + column + "] is not compressed");
                decompresOK = false;
                break;
            }
        }
        bos.close();
        if (decompresOK)
            buf = bos.toByteArray();

        Object result = null;
        ObjectInputStream ois = null;
        boolean objectOK = true;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(buf);
            ois = new ObjectInputStream(bis);
            result = ois.readObject();
        } catch (Exception e) {
            log.debug("message in column [" + column + "] is probably not a serialized object: "
                    + e.getClass().getName());
            objectOK = false;
        }
        if (ois != null)
            ois.close();

        String rawMessage;
        if (objectOK) {
            if (result instanceof IMessageWrapper) {
                rawMessage = ((IMessageWrapper) result).getText();
            } else if (result instanceof TextMessage) {
                rawMessage = ((TextMessage) result).getText();
            } else {
                rawMessage = (String) result;
            }
        } else {
            rawMessage = new String(buf, charset);
        }

        String message = XmlUtils.encodeCdataString(rawMessage);
        return message;
    }
    return Misc.readerToString(getBlobReader(blob, column, charset, blobIsCompressed), null, xmlEncode);
}

From source file:org.opentestsystem.authoring.testspecbank.service.impl.TestSpecificationServiceImpl.java

private final byte[] decompress(final byte[] testSpecificationXml) {
    final Inflater inflater = new Inflater();
    inflater.setInput(testSpecificationXml);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(testSpecificationXml.length);

    byte[] outBytes = null;

    try {/*from ww w. j a  va  2  s  . c om*/
        final byte[] buffer = new byte[BUFFER_SIZE];
        while (!inflater.finished()) {
            final int count = inflater.inflate(buffer);
            baos.write(buffer, 0, count);
        }
        baos.close();
        outBytes = baos.toByteArray();

    } catch (final IOException | DataFormatException e) {
        throw new LocalizedException("testspec.xml.compress.error", e);
    }

    return outBytes;
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

private void parse_IEND_chunk(final PNGChunk chunk) {
    // Store text strings
    final int textLen = this.textKeys.size();
    final String[] textArray = new String[2 * textLen];
    for (int i = 0; i < textLen; ++i) {
        final String key = this.textKeys.get(i);
        final String val = this.textStrings.get(i);
        textArray[2 * i] = key;/*from ww  w  . ja  v a  2  s.c  o  m*/
        textArray[2 * i + 1] = val;
        if (this.emitProperties) {
            final String uniqueKey = "text_" + i + ':' + key;
            this.properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (this.encodeParam != null) {
        this.encodeParam.setText(textArray);
    }

    // Store compressed text strings
    final int ztextLen = this.ztextKeys.size();
    final String[] ztextArray = new String[2 * ztextLen];
    for (int i = 0; i < ztextLen; ++i) {
        final String key = this.ztextKeys.get(i);
        final String val = this.ztextStrings.get(i);
        ztextArray[2 * i] = key;
        ztextArray[2 * i + 1] = val;
        if (this.emitProperties) {
            final String uniqueKey = "ztext_" + i + ':' + key;
            this.properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (this.encodeParam != null) {
        this.encodeParam.setCompressedText(ztextArray);
    }

    // Parse prior IDAT chunks
    final InputStream seqStream = new SequenceInputStream(Collections.enumeration(this.streamVec));
    final InputStream infStream = new InflaterInputStream(seqStream, new Inflater());
    this.dataStream = new DataInputStream(infStream);

    // Create an empty WritableRaster
    int depth = this.bitDepth;
    if (this.colorType == PNG_COLOR_GRAY && this.bitDepth < 8 && this.output8BitGray) {
        depth = 8;
    }
    if (this.colorType == PNG_COLOR_PALETTE && this.expandPalette) {
        depth = 8;
    }
    final int bytesPerRow = (this.outputBands * this.width * depth + 7) / 8;
    final int scanlineStride = depth == 16 ? bytesPerRow / 2 : bytesPerRow;

    this.theTile = createRaster(this.width, this.height, this.outputBands, scanlineStride, depth);

    if (this.performGammaCorrection && this.gammaLut == null) {
        initGammaLut(this.bitDepth);
    }
    if (this.postProcess == POST_GRAY_LUT || this.postProcess == POST_GRAY_LUT_ADD_TRANS
            || this.postProcess == POST_GRAY_LUT_ADD_TRANS_EXP) {
        initGrayLut(this.bitDepth);
    }

    decodeImage(this.interlaceMethod == 1);
    this.sampleModel = this.theTile.getSampleModel();

    if (this.colorType == PNG_COLOR_PALETTE && !this.expandPalette) {
        if (this.outputHasAlphaPalette) {
            this.colorModel = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette,
                    this.greenPalette, this.bluePalette, this.alphaPalette);
        } else {
            this.colorModel = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette,
                    this.greenPalette, this.bluePalette);
        }
    } else if (this.colorType == PNG_COLOR_GRAY && this.bitDepth < 8 && !this.output8BitGray) {
        final byte[] palette = this.expandBits[this.bitDepth];
        this.colorModel = new IndexColorModel(this.bitDepth, palette.length, palette, palette, palette);
    } else {
        this.colorModel = createComponentColorModel(this.sampleModel);
    }
}

From source file:net.dv8tion.jda.core.requests.WebSocketClient.java

@Override
public void onBinaryMessage(WebSocket websocket, byte[] binary)
        throws UnsupportedEncodingException, DataFormatException {
    //Thanks to ShadowLordAlpha for code and debugging.
    //Get the compressed message and inflate it
    StringBuilder builder = new StringBuilder();
    Inflater decompresser = new Inflater();
    decompresser.setInput(binary, 0, binary.length);
    byte[] result = new byte[128];
    while (!decompresser.finished()) {
        int resultLength = decompresser.inflate(result);
        builder.append(new String(result, 0, resultLength, "UTF-8"));
    }//from   w w  w  .  j a v a  2  s  . c o m
    decompresser.end();

    // send the inflated message to the TextMessage method
    onTextMessage(websocket, builder.toString());
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGRed.java

private void parse_IEND_chunk(final PNGChunk chunk) throws IOException {
    // Store text strings
    final int textLen = this.textKeys.size();
    final String[] textArray = new String[2 * textLen];
    for (int i = 0; i < textLen; ++i) {
        final String key = this.textKeys.get(i);
        final String val = this.textStrings.get(i);
        textArray[2 * i] = key;//from  w  ww . j a v a 2s  .c  om
        textArray[2 * i + 1] = val;
        if (this.emitProperties) {
            final String uniqueKey = "text_" + i + ':' + key;
            this.properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (this.encodeParam != null) {
        this.encodeParam.setText(textArray);
    }

    // Store compressed text strings
    final int ztextLen = this.ztextKeys.size();
    final String[] ztextArray = new String[2 * ztextLen];
    for (int i = 0; i < ztextLen; ++i) {
        final String key = this.ztextKeys.get(i);
        final String val = this.ztextStrings.get(i);
        ztextArray[2 * i] = key;
        ztextArray[2 * i + 1] = val;
        if (this.emitProperties) {
            final String uniqueKey = "ztext_" + i + ':' + key;
            this.properties.put(uniqueKey.toLowerCase(), val);
        }
    }
    if (this.encodeParam != null) {
        this.encodeParam.setCompressedText(ztextArray);
    }

    // Parse prior IDAT chunks
    final InputStream seqStream = new SequenceInputStream(Collections.enumeration(this.streamVec));
    final InputStream infStream = new InflaterInputStream(seqStream, new Inflater());
    this.dataStream = new DataInputStream(infStream);

    // Create an empty WritableRaster
    int depth = this.bitDepth;
    if (this.colorType == PNG_COLOR_GRAY && this.bitDepth < 8 && this.output8BitGray) {
        depth = 8;
    }
    if (this.colorType == PNG_COLOR_PALETTE && this.expandPalette) {
        depth = 8;
    }
    final int width = this.bounds.width;
    final int height = this.bounds.height;

    final int bytesPerRow = (this.outputBands * width * depth + 7) / 8;
    final int scanlineStride = depth == 16 ? bytesPerRow / 2 : bytesPerRow;

    this.theTile = createRaster(width, height, this.outputBands, scanlineStride, depth);

    if (this.performGammaCorrection && this.gammaLut == null) {
        initGammaLut(this.bitDepth);
    }
    if (this.postProcess == POST_GRAY_LUT || this.postProcess == POST_GRAY_LUT_ADD_TRANS
            || this.postProcess == POST_GRAY_LUT_ADD_TRANS_EXP) {
        initGrayLut(this.bitDepth);
    }

    decodeImage(this.interlaceMethod == 1);

    // Free resources associated with compressed data.
    this.dataStream.close();
    infStream.close();
    seqStream.close();
    this.streamVec = null;

    final SampleModel sm = this.theTile.getSampleModel();
    ColorModel cm;

    if (this.colorType == PNG_COLOR_PALETTE && !this.expandPalette) {
        if (this.outputHasAlphaPalette) {
            cm = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette, this.greenPalette,
                    this.bluePalette, this.alphaPalette);
        } else {
            cm = new IndexColorModel(this.bitDepth, this.paletteEntries, this.redPalette, this.greenPalette,
                    this.bluePalette);
        }
    } else if (this.colorType == PNG_COLOR_GRAY && this.bitDepth < 8 && !this.output8BitGray) {
        final byte[] palette = this.expandBits[this.bitDepth];
        cm = new IndexColorModel(this.bitDepth, palette.length, palette, palette, palette);
    } else {
        cm = createComponentColorModel(sm);
    }

    init((CachableRed) null, this.bounds, cm, sm, 0, 0, this.properties);
}

From source file:org.jwebsocket.util.Tools.java

/**
 * Inflate a byte array with Zip compression
 *
 * @param aCompressedData//  w  w  w .  jav  a2s  . com
 * @return
 * @throws Exception
 */
public static byte[] inflate(byte[] aCompressedData) throws Exception {
    Inflater lInflater = new Inflater();
    lInflater.setInput(aCompressedData);
    byte[] lOut = new byte[1024 * 1000 * 5];
    int lWritten = lInflater.inflate(lOut);
    byte[] lResult = new byte[lWritten];

    System.arraycopy(lOut, 0, lResult, 0, lWritten);

    return lResult;
}

From source file:edu.umass.cs.gigapaxos.SQLPaxosLogger.java

/**
 * @param buf/*from  w  w w .  j a  v  a2  s  .com*/
 * @return Uncompressed form.
 * @throws IOException
 */
public static byte[] inflate(byte[] buf) throws IOException {
    if (!DB_COMPRESSION)
        return buf;
    Inflater inflator = new Inflater();
    inflator.setInput(buf);
    byte[] decompressed = new byte[buf.length];
    ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
    try {
        while (!inflator.finished()) {
            int count = inflator.inflate(decompressed);
            if (count == 0)
                break;
            baos.write(decompressed, 0, count);
        }
        baos.close();
        inflator.end();
    } catch (DataFormatException e) {
        PaxosManager.getLogger()
                .severe("DataFormatException while decompressing buffer of length " + buf.length);
        e.printStackTrace();
        return buf;
    }
    return baos.toByteArray();
}