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:com.qatickets.common.ZIPHelper.java

public static byte[] decompressAsBytes(byte[] data) {

    if (data == null) {
        return null;
    }//from w w  w  .  j ava 2s.c  o m

    // Create the decompressor and give it the data to compress
    final Inflater decompressor = new Inflater();
    decompressor.setInput(data);

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

    // Decompress the data
    final byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            final int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

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

    decompressor.end();

    return decompressedData;
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Decode lz byte [ ]./*from  ww  w  .  java 2s . co m*/
 *
 * @param data       the data
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] decodeLZ(byte[] data, String dictionary) {
    try {
        Inflater decompresser = new Inflater();
        decompresser.setInput(data, 0, data.length);
        byte[] result = new byte[data.length * 32];
        int resultLength = 0;
        if (!dictionary.isEmpty()) {
            resultLength = decompresser.inflate(result);
            assert (0 == resultLength);
            if (decompresser.needsDictionary()) {
                byte[] bytes = dictionary.getBytes("UTF-8");
                decompresser.setDictionary(bytes);
            }
        }
        resultLength = decompresser.inflate(result);
        decompresser.end();
        return Arrays.copyOfRange(result, 0, resultLength);
    } catch (DataFormatException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.atricore.idbus.capabilities.sso.main.binding.SamlR2HttpRedirectBinding.java

public static String inflateFromRedirect(String redirStr, boolean decode) throws Exception {

    if (redirStr == null || redirStr.length() == 0) {
        throw new RuntimeException("Redirect string cannot be null or empty");
    }/* w w w  . j  a v a2 s .c om*/

    byte[] redirBin = null;
    if (decode)
        redirBin = new Base64().decode(removeNewLineChars(redirStr).getBytes());
    else
        redirBin = redirStr.getBytes();

    // Decompress the bytes
    Inflater inflater = new Inflater(true);
    inflater.setInput(redirBin);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);

    try {
        int resultLength = 0;
        int buffSize = 1024;
        byte[] buff = new byte[buffSize];
        while (!inflater.finished()) {
            resultLength = inflater.inflate(buff);
            baos.write(buff, 0, resultLength);
        }

    } catch (DataFormatException e) {
        throw new RuntimeException("Cannot inflate SAML message : " + e.getMessage(), e);
    }

    inflater.end();

    // Decode the bytes into a String
    String outputString = null;
    try {
        outputString = new String(baos.toByteArray(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Cannot convert byte array to string " + e.getMessage(), e);
    }
    return outputString;
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Read a potentially compressed string from the data input.
 *
 * @param in//from w ww.  ja v  a  2 s . c om
 * @return
 * @throws IOException
 */
private static String readContent(DataInput in) throws IOException {
    int mode = in.readByte();

    if (mode == MODE_COMPRESSED) {
        try {
            int strlen = in.readInt();
            int buflen = in.readInt();

            byte[] buffer = new byte[buflen];
            in.readFully(buffer);

            Inflater decompressor = new Inflater(true);
            decompressor.setInput(buffer);

            byte[] data = new byte[strlen];
            decompressor.inflate(data);
            decompressor.end();

            return new String(data, "UTF-8");
        } catch (DataFormatException ex) {
            throw new IllegalStateException("input data is not valid", ex);
        }
    } else {
        return DataIO.readString(in);
    }
}

From source file:org.wso2.carbon.identity.saml.inbound.util.SAMLSSOUtil.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq/*  w  w w.j  ava 2 s. co m*/
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws IdentityException {
    try {
        org.apache.commons.codec.binary.Base64 base64Decoder = new org.apache.commons.codec.binary.Base64();
        byte[] xmlBytes = encodedStr.getBytes(StandardCharsets.UTF_8.name());
        byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

        try {
            Inflater inflater = new Inflater(true);
            inflater.setInput(base64DecodedByteArray);
            byte[] xmlMessageBytes = new byte[5000];
            int resultLength = inflater.inflate(xmlMessageBytes);

            if (!inflater.finished()) {
                throw new RuntimeException("End of the compressed data stream has NOT been reached");
            }

            inflater.end();
            String decodedString = new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8.name());
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedString);
            }
            return decodedString;

        } catch (DataFormatException e) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64DecodedByteArray);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            InflaterInputStream iis = new InflaterInputStream(byteArrayInputStream);
            byte[] buf = new byte[1024];
            int count = iis.read(buf);
            while (count != -1) {
                byteArrayOutputStream.write(buf, 0, count);
                count = iis.read(buf);
            }
            iis.close();
            String decodedStr = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedStr, e);
            }
            return decodedStr;
        }
    } catch (IOException e) {
        throw IdentityException.error("Error when decoding the SAML Request.", e);
    }

}

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;//from ww w  . j  a  v  a  2  s  .  c o m
    }
    decompresser.end();

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

From source file:org.jasig.cas.web.flow.FrontChannelLogoutActionTests.java

@Test
public void verifyLogoutOneLogoutRequestNotAttempted() throws Exception {
    final String fakeUrl = "http://url";
    final LogoutRequest logoutRequest = new LogoutRequest(TICKET_ID,
            new SimpleWebApplicationServiceImpl(fakeUrl));
    WebUtils.putLogoutRequests(this.requestContext, Arrays.asList(logoutRequest));
    this.requestContext.getFlowScope().put(FrontChannelLogoutAction.LOGOUT_INDEX, 0);
    final Event event = this.frontChannelLogoutAction.doExecute(this.requestContext);
    assertEquals(FrontChannelLogoutAction.REDIRECT_APP_EVENT, event.getId());
    final List<LogoutRequest> list = WebUtils.getLogoutRequests(this.requestContext);
    assertEquals(1, list.size());/*from w ww . j av a  2s .  com*/
    final String url = (String) event.getAttributes().get("logoutUrl");
    assertTrue(url.startsWith(fakeUrl + "?SAMLRequest="));
    final byte[] samlMessage = CompressionUtils.decodeBase64ToByteArray(
            URLDecoder.decode(StringUtils.substringAfter(url, "?SAMLRequest="), "UTF-8"));
    final Inflater decompresser = new Inflater();
    decompresser.setInput(samlMessage);
    final byte[] result = new byte[1000];
    decompresser.inflate(result);
    decompresser.end();
    final String message = new String(result);
    assertTrue(message
            .startsWith("<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\""));
    assertTrue(message.indexOf("<samlp:SessionIndex>" + TICKET_ID + "</samlp:SessionIndex>") >= 0);
}

From source file:org.dragonet.proxy.protocol.packet.LoginPacket.java

@Override
public void decode() {
    try {//ww w .ja  va 2  s.  c  o m
        PEBinaryReader reader = new PEBinaryReader(new ByteArrayInputStream(this.getData()));
        reader.readByte(); //PID
        this.protocol = reader.readInt();
        this.gameEdition = reader.readByte();

        byte[] buff = new byte[40960];
        int len = reader.readUnsignedVarInt();
        Inflater inf = new Inflater();
        inf.setInput(reader.read(len));
        int out = inf.inflate(buff);
        inf.end();
        buff = ArrayUtils.subarray(buff, 0, out);
        String strJsonData;
        String strMetaData;
        {
            PEBinaryReader readerPayload = new PEBinaryReader(new ByteArrayInputStream(buff));
            readerPayload.switchEndianness();
            int jsonLen = readerPayload.readInt();
            readerPayload.switchEndianness();
            strJsonData = new String(readerPayload.read(jsonLen), "UTF-8");
            readerPayload.switchEndianness();
            int restLen = readerPayload.readInt();
            readerPayload.switchEndianness();
            strMetaData = new String(readerPayload.read(restLen), "UTF-8");
        }

        // Decode basic info
        {
            JSONObject data = new JSONObject(strJsonData);
            if (data.length() <= 0 || !data.has("chain") || data.optJSONArray("chain") == null) {
                return;
            }
            String[] chains = decodeJsonStringArray(data.getJSONArray("chain"));
            //System.out.println("Chain count: " + chains.length);
            for (String token : chains) {
                //System.out.println(" -- processing chain: " + token);
                JSONObject map = decodeToken(token);

                if (map == null || map.length() == 0) {
                    continue;
                }

                if (map.has("extraData")) {
                    JSONObject extras = map.getJSONObject("extraData");
                    if (extras.has("displayName")) {
                        username = extras.getString("displayName");
                    }
                    if (extras.has("identity")) {
                        this.clientUuid = UUID.fromString(extras.getString("identity"));
                    }
                }
                if (map.has("identityPublicKey")) {
                    publicKey = map.getString("identityPublicKey");
                }
            }
        }

        // Decode user metadata
        {
            JSONObject map = decodeToken(strMetaData);
            if (map.has("ClientRandomId"))
                clientID = map.getLong("ClientRandomId");
            if (map.has("ServerAddress"))
                serverAddress = map.getString("ServerAddress");
            if (map.has("SkinId"))
                skinName = map.getString("SkinId");
            if (map.has("SkinData"))
                skin = new MCPESkin(map.getString("SkinData"), skinName);
        }
    } catch (IOException | DataFormatException | JSONException e) {
        Logger.getLogger(LoginPacket.class.getName()).log(Level.SEVERE, null, e);
    }
}

From source file:com.alibaba.citrus.service.requestcontext.session.valueencoder.AbstractSessionValueEncoder.java

private byte[] decompress(byte[] data) throws SessionValueEncoderException {
    if (!doCompress()) {
        return data;
    }//from  ww w . j a v a  2 s.  com

    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    Inflater inf = new Inflater(false);
    InflaterInputStream iis = new InflaterInputStream(bais, inf);

    try {
        return StreamUtil.readBytes(iis, true).toByteArray();
    } catch (Exception e) {
        throw new SessionValueEncoderException(e);
    } finally {
        inf.end();
    }
}