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.esupportail.publisher.security.CustomSingleSignOutHandler.java

/**
 * Uncompress a logout message (base64 + deflate).
 *
 * @param originalMessage the original logout message.
 * @return the uncompressed logout message.
 *///from w w  w  .  java  2  s.  c om
private String uncompressLogoutMessage(final String originalMessage) {
    final byte[] binaryMessage = Base64.decodeBase64(originalMessage.getBytes());

    Inflater decompresser = null;
    try {
        // decompress the bytes
        decompresser = new Inflater();
        decompresser.setInput(binaryMessage);
        final byte[] result = new byte[binaryMessage.length * DECOMPRESSION_FACTOR];

        final int resultLength = decompresser.inflate(result);

        // decode the bytes into a String
        return new String(result, 0, resultLength, "UTF-8");
    } catch (final Exception e) {
        logger.error("Unable to decompress logout message", e);
        throw new RuntimeException(e);
    } finally {
        if (decompresser != null) {
            decompresser.end();
        }
    }
}

From source file:org.eclipsetrader.directa.internal.core.connector.BackfillConnector.java

protected void parseIntradayStream(BufferedInputStream in, List<OHLC> list) throws Exception {
    Calendar cal = Calendar.getInstance();

    int startTime = 9 * 60;
    int endTime = 17 * 60 + 25;
    ;//from w  ww . j  a v  a 2  s. c om

    byte[] buffer = new byte[1];
    while (in.read(buffer) == 1) {
        if (buffer[0] == '<') {
            StringBuilder sb = new StringBuilder();
            while (in.read(buffer) == 1) {
                if (buffer[0] == '>') {
                    break;
                }
                sb.append(new String(buffer));
            }
            String line = sb.toString();
            if (line.startsWith("GRA")) { //$NON-NLS-1$
                int s = line.indexOf("L=") + 2; //$NON-NLS-1$
                int e = line.indexOf(" ", s); //$NON-NLS-1$
                int uncompressLen = Integer.parseInt(line.substring(s, e));

                byte[] output = new byte[uncompressLen];
                boolean compressed = line.indexOf("LC=") != -1; //$NON-NLS-1$

                if (compressed) {
                    s = line.indexOf("LC=") + 3; //$NON-NLS-1$
                    e = line.indexOf(" ", s); //$NON-NLS-1$
                    int compressLen = Integer.parseInt(line.substring(s, e));

                    while (in.read(buffer) == 1) {
                        if (buffer[0] == 0x78) {
                            break;
                        }
                    }
                    if (buffer[0] != 0x78) {
                        break;
                    }

                    int readed = 1, len;
                    byte[] input = new byte[compressLen];
                    input[0] = buffer[0];
                    do {
                        len = in.read(input, readed, input.length - readed);
                        readed += len;
                    } while (len > 0 && readed < input.length);

                    Inflater infl = new Inflater();
                    infl.setInput(input, 0, readed);
                    infl.inflate(output);
                    infl.end();
                } else {
                    in.read(buffer);

                    int readed = 0, len;
                    do {
                        len = in.read(output, readed, output.length - readed);
                        readed += len;
                    } while (len > 0 && readed < output.length);
                }

                for (int i = 0; i < output.length; i += 28) {
                    Date date = getDate(output, i);
                    cal.setTime(date);
                    int time = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
                    if (time >= startTime && time <= endTime) {
                        float low = getFloat(output, i + 8);
                        float high = getFloat(output, i + 12);
                        float close = getFloat(output, i + 16);
                        float volume = getFloat(output, i + 20);
                        float open = getFloat(output, i + 24);
                        list.add(new OHLC(date, (double) open, (double) high, (double) low, (double) close,
                                (long) volume));
                    }
                }
            }
        }
    }
}

From source file:aarddict.Volume.java

static String decompressZlib(byte[] bytes) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(bytes);//from   w  w w. j av  a 2 s . com
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            int count = decompressor.inflate(buf);
            out.write(buf, 0, count);
        }
    } finally {
        out.close();
    }
    return utf8(out.toByteArray());
}

From source file:org.apache.xmlgraphics.image.loader.impl.imageio.ImageLoaderImageIO.java

private ICC_Profile tryToExctractICCProfileFromPNGMetadataNode(Element pngNode) {
    ICC_Profile iccProf = null;//from w w w . ja  va 2s .co  m
    Element iccpNode = ImageIOUtil.getChild(pngNode, "iCCP");
    if (iccpNode instanceof IIOMetadataNode) {
        IIOMetadataNode imn = (IIOMetadataNode) iccpNode;
        byte[] prof = (byte[]) imn.getUserObject();
        String comp = imn.getAttribute("compressionMethod");
        if ("deflate".equalsIgnoreCase(comp)) {
            Inflater decompresser = new Inflater();
            decompresser.setInput(prof);
            byte[] result = new byte[100];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            boolean failed = false;
            while (!decompresser.finished() && !failed) {
                try {
                    int resultLength = decompresser.inflate(result);
                    bos.write(result, 0, resultLength);
                    if (resultLength == 0) {
                        // this means more data or an external dictionary is
                        // needed. Both of which are not available, so we
                        // fail.
                        log.debug("Failed to deflate ICC Profile");
                        failed = true;
                    }
                } catch (DataFormatException e) {
                    log.debug("Failed to deflate ICC Profile", e);
                    failed = true;
                }
            }
            decompresser.end();
            try {
                iccProf = ICC_Profile.getInstance(bos.toByteArray());
            } catch (IllegalArgumentException e) {
                log.debug("Failed to interpret embedded ICC Profile", e);
                iccProf = null;
            }
        }
    }
    return iccProf;
}

From source file:r.base.Connections.java

public static byte[] decompress1(byte buffer[]) throws IOException, DataFormatException {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
    int outLength = in.readInt();

    Inflater inflater = new Inflater();
    inflater.setInput(buffer, 4, buffer.length - 4);

    byte[] result = new byte[outLength];
    inflater.inflate(result);// ww w .  j  a  v  a  2 s .c om
    inflater.end();

    return result;
}

From source file:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionDecoder.java

/**
 * Inflates the zipped input.//from   w w  w  .  jav  a 2s .  c om
 * 
 * @param zipinput
 *            zipped input
 * @param start
 *            start position
 * @return inflated input
 */
private byte[] inflateInput(final byte[] zipinput, final int start) {
    ByteArrayOutputStream stream;
    try {
        byte[] compressedInput = zipinput;
        Inflater decompresser = new Inflater();
        decompresser.setInput(compressedInput, start, compressedInput.length - start);

        byte[] output = new byte[1000];
        stream = new ByteArrayOutputStream();

        int cLength;
        do {
            cLength = decompresser.inflate(output);
            stream.write(output, 0, cLength);
        } while (cLength == 1000);

    } catch (DataFormatException e) {
        throw new RuntimeException(e);
    }

    return stream.toByteArray();
}

From source file:com.nary.Debug.java

public static Object loadClass(InputStream _inStream, boolean _uncompress) {
    ObjectInputStream ois;/*from w ww.j av a  2  s  . c om*/
    try {
        if (_uncompress) {
            // we need to get the input as a byte [] so we can decompress (inflate) it.  
            Inflater inflater = new Inflater();
            ByteArrayOutputStream bos;
            int bytesAvail = _inStream.available();
            if (bytesAvail > 0) {
                bos = new ByteArrayOutputStream(bytesAvail);
            } else {
                bos = new ByteArrayOutputStream();
            }

            byte[] buffer = new byte[1024];
            int read = _inStream.read(buffer);
            while (read > 0) {
                bos.write(buffer, 0, read);
                read = _inStream.read(buffer);
            }
            bos.flush();
            inflater.setInput(bos.toByteArray());

            bos.reset();
            buffer = new byte[1024];
            int inflated = inflater.inflate(buffer);
            while (inflated > 0) {
                bos.write(buffer, 0, inflated);
                inflated = inflater.inflate(buffer);
            }

            bos.flush();
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

            ois = new ObjectInputStream(bis);

        } else {
            ois = new ObjectInputStream(_inStream);
        }

        return ois.readObject();
    } catch (Exception E) {
        return null;
    } finally {
        try {
            _inStream.close();
        } catch (Exception ioe) {
        }
    }
}

From source file:org.apache.oozie.workflow.lite.LiteWorkflowAppParser.java

/**
 * Read the GlobalSectionData from Base64 string.
 * @param globalStr//  w  ww.j  a v  a2  s  . c  om
 * @return GlobalSectionData
 * @throws WorkflowException
 */
private GlobalSectionData getGlobalFromString(String globalStr) throws WorkflowException {
    GlobalSectionData globalSectionData = new GlobalSectionData();
    try {
        byte[] data = Base64.decodeBase64(globalStr);
        Inflater inflater = new Inflater();
        DataInputStream ois = new DataInputStream(
                new InflaterInputStream(new ByteArrayInputStream(data), inflater));
        globalSectionData.readFields(ois);
        ois.close();
    } catch (Exception ex) {
        throw new WorkflowException(ErrorCode.E0700, "Error while processing global section conf");
    }
    return globalSectionData;
}

From source file:org.apache.geode.management.internal.cli.CliUtil.java

public static DeflaterInflaterData uncompressBytes(byte[] output, int compressedDataLength)
        throws DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(output, 0, compressedDataLength);
    byte[] buffer = new byte[512];
    byte[] result = new byte[0];
    int bytesRead;
    while (!decompresser.needsInput()) {
        bytesRead = decompresser.inflate(buffer);
        byte[] newResult = new byte[result.length + bytesRead];
        System.arraycopy(result, 0, newResult, 0, result.length);
        System.arraycopy(buffer, 0, newResult, result.length, bytesRead);
        result = newResult;// w w w.j  av  a  2s . c o m
    }
    decompresser.end();

    return new DeflaterInflaterData(result.length, result);
}

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

public static byte[] decompress(byte[] input) throws DataFormatException, IOException {
    // Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);/*from w  w  w.j  ava2  s  .co m*/

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.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
    return bos.toByteArray();
}