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:com.eryansky.common.utils.SysUtils.java

/**
  * // ww  w . j  a v a  2s.c  om
  * 
  * @param in
  *            ?
  * @return 
  */
 public static String unZip_Str(byte[] in) {
     Inflater decompresser = new Inflater();
     decompresser.setInput(in);
     ArrayList<Byte> al = new ArrayList<Byte>();
     byte[] result;
     int count = 0;
     for (; !decompresser.finished();) {
         result = new byte[100];
         try {
             count += decompresser.inflate(result);
         } catch (DataFormatException e) {
             e.printStackTrace();
         }
         for (int i = 0; i < result.length; i++) {
             al.add(new Byte(result[i]));
         }
     }
     result = new byte[al.size()];
     for (int i = 0; i < al.size(); i++) {
         result[i] = (al.get(i)).byteValue();
     }
     decompresser.end();

     try {
         return new String(result, 0, count, "UTF-8");
     } catch (UnsupportedEncodingException e) {
         return "";
     }
 }

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static byte[] decompress(byte[] compressedData, int off, int len)
        throws IOException, DataFormatException {
    // Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData, off, len);

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

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);/*www .  j  a  va2 s . c  o m*/
    }
    bos.close();

    // Get the decompressed data
    return bos.toByteArray();
}

From source file:co.cask.hydrator.transforms.CSVParser2.java

private byte[] unzip(byte[] body) throws IOException {
    Inflater inf = new Inflater();
    ByteArrayInputStream bytein = new ByteArrayInputStream(body);
    ZipInputStream gzin = new ZipInputStream(bytein);
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();

    int res = 0;/*from w  w w .j  a va  2  s  . co m*/
    byte buf[] = new byte[1024];
    while (res >= 0) {
        res = gzin.read(buf, 0, buf.length);
        if (res > 0) {
            byteout.write(buf, 0, res);
        }
    }
    byte uncompressed[] = byteout.toByteArray();
    return uncompressed;
}

From source file:MSUmpire.SpectrumParser.mzXMLReadUnit.java

public byte[] ZlibUncompressBuffer(byte[] compressed) throws IOException, DataFormatException {

    Inflater decompressor = new Inflater();
    decompressor.setInput(compressed);//from w  ww. j a  v a2  s.c  o m

    ByteArrayOutputStream bos = null;
    try {

        bos = new ByteArrayOutputStream(compressed.length);

        // Decompress the data
        byte[] buf = new byte[decompressor.getRemaining() * 2];
        while (decompressor.getRemaining() > 0) {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }

    } finally {
        try {
            bos.close();
        } catch (Exception nope) {
            /* This exception doesn't matter */ }
    }
    decompressor.end();
    compressed = null;
    decompressor = null;
    byte[] result = bos.toByteArray();
    bos = null;
    return result;
}

From source file:gov.niem.ws.util.SecurityUtil.java

/**
 * Decode data by Base64 decoding, then decompressing with the DEFLATE
 * algorithm; reverses encodeHeader./*from  w w w  .ja  va 2  s  .co  m*/
 * 
 * @param encoded
 * @return the decoded, decompressed data
 * @throws DataFormatException
 */
public static String decodeHeader(byte[] encoded) throws DataFormatException {
    // TODO: length limit on encoded?
    byte[] compressedBytes = Base64.decodeBase64(encoded);
    ByteArrayOutputStream out = new ByteArrayOutputStream(compressedBytes.length);
    Inflater inflater = new Inflater();
    inflater.setInput(compressedBytes);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        if (count == 0)
            break;
        out.write(buffer, 0, count);
    }

    try {
        inflater.end();
        out.close();
    } catch (IOException e) {
    }

    return new String(out.toByteArray());
}

From source file:org.apache.fop.render.pdf.ImageRawPNGAdapter.java

/** {@inheritDoc} */
public void outputContents(OutputStream out) throws IOException {
    InputStream in = ((ImageRawStream) image).createInputStream();

    try {/*from  w  ww  .  jav a  2 s .  c o m*/
        if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) {
            // means we have Gray, RGB, or Palette
            IOUtils.copy(in, out);
        } else {
            // means we have Gray + alpha or RGB + alpha
            // TODO: since we have alpha here do this when the alpha channel is extracted
            int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB
            int numColumns = image.getSize().getWidthPx();
            InflaterInputStream infStream = new InflaterInputStream(in, new Inflater());
            DataInputStream dataStream = new DataInputStream(infStream);
            int offset = 0;
            int bytesPerRow = numberOfInterleavedComponents * numColumns;
            int filter;
            // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha
            // channel and then deflate the RGB channels back again
            DeflaterOutputStream dos = new DeflaterOutputStream(out, new Deflater());
            while ((filter = dataStream.read()) != -1) {
                byte[] bytes = new byte[bytesPerRow];
                dataStream.readFully(bytes, 0, bytesPerRow);
                dos.write((byte) filter);
                for (int j = 0; j < numColumns; j++) {
                    dos.write(bytes, offset, numBytes);
                    offset += numberOfInterleavedComponents;
                }
                offset = 0;
            }
            dos.close();
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:de.btobastian.javacord.utils.DiscordWebsocketAdapter.java

@Override
public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
    Inflater decompressor = new Inflater();
    decompressor.setInput(binary);// www.  j av a  2  s  .c om
    ByteArrayOutputStream bos = new ByteArrayOutputStream(binary.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count;
        try {
            count = decompressor.inflate(buf);
        } catch (DataFormatException e) {
            logger.warn("An error occurred while decompressing data", e);
            return;
        }
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException ignored) {
    }
    byte[] decompressedData = bos.toByteArray();
    try {
        onTextMessage(websocket, new String(decompressedData, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        logger.warn("An error occurred while decompressing data", e);
    }
}

From source file:org.jasig.cas.client.session.SingleSignOutHandler.java

/**
 * Uncompress a logout message (base64 + deflate).
 * // w  ww  . j  a  v a2 s  .com
 * @param originalMessage the original logout message.
 * @return the uncompressed logout message.
 */
private String uncompressLogoutMessage(final String originalMessage) {
    final byte[] binaryMessage = Base64.decodeBase64(originalMessage);

    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:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

/**
 * Decompresses a string./*www. j  a v a 2  s .  com*/
 * 
 * @param s
 *            The string to decompress
 * @return The decompressed string
 */
public static String decompress(String s) {
    ByteArrayOutputStream baos = null;
    try {
        Inflater ifl = new Inflater();
        ifl.setInput(Base64.decodeBase64(s));

        baos = new ByteArrayOutputStream();
        while (!ifl.finished()) {
            byte[] buff = new byte[1024];
            int count = ifl.inflate(buff);
            baos.write(buff, 0, count);
        }
        baos.flush();
        byte[] output = baos.toByteArray();

        return new String(output, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } catch (DataFormatException e) {
        logger.error(e.getMessage(), e);
    } finally {
        ClosingUtils.close(baos);
    }
    return "";
}

From source file:org.barcelonamedia.uima.reader.DBXMIReader.DBXMICollectionReader.java

public void getNext(CAS aCAS) throws IOException, CollectionException {

    try {/* ww w  .j av  a2s  .  co  m*/

        if (this.do_decompression) {

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

            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()) {

                try {

                    int count = decompressor.inflate(buf);
                    bos.write(buf, 0, count);
                } catch (DataFormatException e) {

                    System.err.println("ERROR in Collection Reader " + e.getClass() + ": " + e.getMessage());
                    throw new IOException();
                }
            }

            try {

                bos.close();
            } catch (IOException e) {

                System.err.println("ERROR in Collection Reader " + e.getClass() + ": " + e.getMessage());
                throw new IOException();
            }

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

            XmiCasDeserializer.deserialize(new ByteArrayInputStream(decompressedData), aCAS,
                    !this.mFailOnUnknownType);
        } else {

            XmiCasDeserializer.deserialize(this.documentData, aCAS, !this.mFailOnUnknownType);
        }

        this.currentIndex += 1;
    } catch (SAXException e) {

        System.err.println("ERROR in Collection Reader " + e.getClass() + ": " + e.getMessage());
        throw new CollectionException(e);
    }
}