Example usage for java.util.zip Inflater setInput

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

Introduction

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

Prototype

public void setInput(byte[] input, int off, int len) 

Source Link

Document

Sets input data for decompression.

Usage

From source file:Main.java

private static byte[] expand(byte[] bytes, int skip) {
    byte[] newBytes = new byte[bytes.length - skip];
    Inflater inflater = new Inflater();

    inflater.setInput(bytes, skip, newBytes.length);
    try {//from  ww  w.j  a va 2 s  .  c  o m
        int outCount = inflater.inflate(newBytes);
        System.arraycopy(newBytes, 0, bytes, skip, outCount);
        Arrays.fill(bytes, skip + outCount, bytes.length, (byte) 0);
        return bytes;
    } catch (DataFormatException e) {
    }

    return null;
}

From source file:Main.java

public static byte[] decompressInZlib(byte[] compressData, int offset, int length) throws Exception {

    Inflater decompresser = new Inflater();
    decompresser.setInput(compressData, 0, compressData.length);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    int count;/*w  w  w.java2  s.  co  m*/
    byte[] buf = new byte[1024];
    while (!decompresser.finished()) {
        count = decompresser.inflate(buf);
        bos.write(buf, 0, count);
    }

    byte[] originalData = bos.toByteArray();
    decompresser.end();

    return originalData;
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {

    byte[] output = null;

    Inflater decompresser = new Inflater();
    decompresser.reset();/*w w w  .  jav  a 2  s. c  o  m*/
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    try {
        byte[] result = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(result);
            out.write(result, 0, i);
        }
        output = out.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        decompresser.end();
    }
    return output;
}

From source file:Main.java

public static void decompress(byte[] data, int off, int len, OutputStream out) {
    Inflater decompresser = new Inflater();
    decompresser.reset();/*from w w  w .  j a v a 2 s.  c o  m*/
    decompresser.setInput(data, off, len);
    byte[] buf = new byte[1024];

    try {
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            out.write(buf, 0, i);
            out.flush();
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        decompresser.end();
    }
}

From source file:v7db.files.Compression.java

/**
 * assumes that the result buffer has exactly the needed size
 * //from w ww . j a v a  2  s . com
 * @throws DataFormatException
 */
static void inflate(byte[] data, int off, int len, byte[] out) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(data, off, len);
    int size = inflater.inflate(out);
    if (size != out.length)
        throw new DataFormatException(
                "unexpected size of deflated data: " + size + " instead of " + out.length);
}

From source file:Main.java

public static byte[] zlibDecompress(byte[] data, int offset, int length) {
    byte[] output = null;

    Inflater decompresser = new Inflater();
    decompresser.reset();/*w  w  w .  jav a  2  s  .co m*/
    try {
        decompresser.setInput(data, offset, length);
    } catch (Exception e) {
        return null;
    }

    ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    decompresser.end();
    return output;
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  w  w w  .  j a  v  a  2 s .co m*/
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            o.close();
            decompresser.end();
        } catch (Exception e) {
        }
    }

    return output;
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len, int srcLen) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();/* ww w . j  a va  2  s .c o m*/
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream o = new ByteArrayOutputStream(srcLen);
    try {
        o.reset();
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            o.close();
            decompresser.end();
        } catch (Exception e) {
        }
    }

    return output;
}

From source file:org.jahia.utils.Url.java

/**
 * Decode facet filter URL parameter/*from w ww.  j  a va  2 s.  c om*/
 * @param inputString enocded facet filter URL query parameter
 * @return decoded facet filter parameter
 */
public static String decodeUrlParam(String inputString) {
    if (StringUtils.isEmpty(inputString)) {
        return inputString;
    }
    byte[] input = Base64.decodeBase64(inputString);
    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(input, 0, input.length);
    byte[] result = new byte[2048];
    String outputString = "";
    try {
        int resultlength = decompresser.inflate(result);
        decompresser.end();
        outputString = new String(result, 0, resultlength, "UTF-8");
    } catch (DataFormatException e) {
        logger.warn("Not able to decode facet URL: " + inputString, e);
    } catch (UnsupportedEncodingException e) {
        logger.warn("Not able to decode facet URL: " + inputString, e);
    }
    return outputString;
}

From source file:Main.java

/**
 * <p>Decompresses a the content of a file from {@code startPosition} of {@code compressedSize}
 * and return the bytes of it. If a {@code compressedSize} can be provided if it is known
 * how large the decompressed content will be.</p>
 *
 * @param file             the file//  w  w w  . j  a v a 2  s .  co m
 * @param startPosition    the start position
 * @param compressedSize   the compresse
 * @param decompressedSize the decompressed size
 * @return the decompressed bytes
 * @throws IOException         if there was any io issues
 * @throws DataFormatException if there was an issue decompressing content
 */
public static byte[] decompress(File file, int startPosition, int compressedSize, int decompressedSize)
        throws IOException, DataFormatException {

    if (decompressedSize == 0) {
        return decompress(file, startPosition, compressedSize);
    }

    try (FileChannel fileChannel = FileChannel.open(file.toPath())) {
        ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startPosition, compressedSize);
        Inflater inflater = new Inflater();
        byte[] compressedBytes = new byte[compressedSize];
        byte[] bytes = new byte[decompressedSize];

        buffer.get(compressedBytes);
        inflater.setInput(compressedBytes, 0, compressedSize);
        inflater.inflate(bytes);
        inflater.end();

        return bytes;
    }
}