Example usage for java.util.zip Inflater end

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

Introduction

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

Prototype

public void end() 

Source Link

Document

Closes the decompressor and discards any unprocessed input.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception { // main method
    // Encode a String into bytes
    String inputString = "this is a test";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output1 = new byte[input.length];
    Deflater compresser = new Deflater();
    compresser.setInput(input);//  www . ja  v a  2 s.  c om
    compresser.finish();
    int compressedDataLength = compresser.deflate(output1);
    compresser.end();

    String str = new String(Base64.getEncoder().encode(output1));
    System.out.println("Deflated String:" + str);

    byte[] output2 = Base64.getDecoder().decode(str);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output2);
    byte[] result = str.getBytes();
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    System.out.println("Deflated String:" + outputString);

}

From source file:org.samlsnort.util.EncodingTool.java

public static String inflate(byte[] deflated) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(deflated);//from ww  w.j a  v  a2s. co  m
    byte[] inflatedBytes = new byte[4096];
    int len = inflater.inflate(inflatedBytes);
    inflater.setInput(new byte[0]);
    len += inflater.inflate(inflatedBytes, len, 1);
    inflater.end();
    return new String(inflatedBytes, 0, len, CHARSET);
}

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 ava2 s .c o 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;
    }
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);/*from  ww w.  j  a v a 2  s .c om*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!decompresser.finished()) {
        int count = decompresser.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    decompresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

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

/**
 * Decode facet filter URL parameter//from   www .  j a va2  s .c  o m
 * @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

public static byte[] decompress(byte[] input, boolean GZIPFormat) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater(GZIPFormat);
    decompressor.setInput(input);//w w w  .j  av a  2 s  .  c o  m
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;

    while (!decompressor.finished()) {
        readCount = decompressor.inflate(readBuffer);
        if (readCount > 0) {
            bao.write(readBuffer, 0, readCount);
        }
    }
    decompressor.end();
    return bao.toByteArray();
}

From source file:Main.java

private static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from w ww .  j a  v  a 2 s  .c  om

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    inflater.end();

    // System.out.println("Original: " + data.length + " bytes.");
    // System.out.println("Decompressed: " + output.length + " bytes.");
    return output;
}

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;/*from   w  w w . ja va2  s  .  c  om*/
    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

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes//from  w  w w  .  j a  v a 2  s .c o m
 * @return the array as a string with {@code UTF-8} encoding
 */
public static String inflate(final byte[] bytes) {
    final Inflater inflater = new Inflater(true);
    final byte[] xmlMessageBytes = new byte[INFLATED_ARRAY_LENGTH];

    final byte[] extendedBytes = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length);
    extendedBytes[bytes.length] = 0;

    inflater.setInput(extendedBytes);

    try {
        final int resultLength = inflater.inflate(xmlMessageBytes);
        inflater.end();

        if (!inflater.finished()) {
            throw new RuntimeException("buffer not large enough.");
        }

        inflater.end();
        return new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8);
    } catch (final DataFormatException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//w  w w  .j a  v  a  2 s . com
    inflater.finished();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();

    byte[] output = outputStream.toByteArray();
    inflater.end();
    return output;
}