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.getspout.spout.packet.PacketAddonData.java

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(data);//from   w  w  w.j a  v  a2  s  .c  o m

        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

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

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

        }

        data = bos.toByteArray();
        compressed = false;
    }
}

From source file:com.marklogic.tree.CompressedTreeDecoder.java

public ExpandedTree decode(byte[] buf, int len) throws IOException {
    String bad;//from ww  w .ja v a2  s . c  o m
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    BiendianDataInputStream is = new BiendianDataInputStream(bis);
    Decoder decoder = new Decoder(is);
    ExpandedTree rep = new ExpandedTree();

    rep.uriKey = decoder.decode64bits();
    rep.uniqKey = decoder.decode64bits();
    rep.linkKey = decoder.decode64bits();

    if (rep.linkKey == -1) {
        rep.linkKey = decoder.decode64bits();
        Inflater decompresser = new Inflater();
        decompresser.setInput(buf, 32, len - 32);

        int resultLength = 0;
        int offset = 0;
        int buflen = Math.min((pow2ceil(len) * 2), (1 << 28));
        byte[] result = new byte[buflen];
        try {
            for (;;) {
                resultLength += decompresser.inflate(result, offset, buflen - offset);
                if (decompresser.finished()) {
                    break;
                }
                offset = buflen;
                buflen += Math.min(buflen, (1 << 28));
                result = java.util.Arrays.copyOf(result, buflen);
            }
        } catch (java.util.zip.DataFormatException ex) {
            throw new IOException("zip inflate failed");
        }
        bis = new ByteArrayInputStream(result, 0, resultLength);
        is = new BiendianDataInputStream(bis);
        decoder = new Decoder(is);
    }

    rep.numKeys = decoder.decodeUnsigned();

    if (rep.numKeys == 0)
        rep.keys = null;
    else {
        rep.keys = new long[rep.numKeys];
        for (int i = 0; i < rep.numKeys; ++i) {
            rep.keys[i] = decoder.decode64bits();
        }
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace(String.format("uriKey  %016x", rep.uriKey));
        LOG.trace(String.format("uniqKey %016x", rep.uniqKey));
        LOG.trace(String.format("linkKey %016x", rep.linkKey));
        for (int i = 0; i < rep.numKeys; ++i) {
            LOG.trace(String.format("  key[%d] %016x", i, rep.keys[i]));
        }
    }

    // atoms
    int numAtomDataWords = decoder.decodeUnsigned();
    if (LOG.isTraceEnabled())
        LOG.trace(String.format("numAtomDataWords %d", numAtomDataWords));
    if (numAtomDataWords == 0)
        rep.atomData = null;
    else {
        rep.atomData = new byte[numAtomDataWords * 4];
        for (int i = 0, j = 0; i < numAtomDataWords; ++i) {
            int word = decoder.decode32bits();
            rep.atomData[j++] = (byte) (word & 0xff);
            rep.atomData[j++] = (byte) ((word >> 8) & 0xff);
            rep.atomData[j++] = (byte) ((word >> 16) & 0xff);
            rep.atomData[j++] = (byte) ((word >> 24) & 0xff);
            if (LOG.isTraceEnabled()) {
                LOG.trace(String.format("  atomData[%d] %08x", i, word));
                LOG.trace(String.format("  atomData[%d] %02x %02x %02x %02x", i, rep.atomData[i * 4],
                        rep.atomData[i * 4 + 1], rep.atomData[i * 4 + 2], rep.atomData[i * 4 + 3]));
            }
        }
    }
    rep.atomLimit = decoder.decodeUnsigned();
    if (LOG.isTraceEnabled()) {
        LOG.trace(String.format("atomLimit %d", rep.atomLimit));
    }

    if (rep.atomLimit == 0) {
        rep.atomIndex = null;
    } else {
        rep.atomIndex = new int[rep.atomLimit + 1];
        int j = 0;
        for (int i = 0; i < rep.atomLimit; ++i) {
            rep.atomIndex[i] = j;
            if (LOG.isTraceEnabled())
                LOG.trace(String.format("  atomIndex[%d] %08x", i, rep.atomIndex[i]));
            if (rep.atomData != null)
                while (rep.atomData[j++] != 0)
                    ;
        }
        rep.atomIndex[rep.atomLimit] = j;
    }
    for (int i = 0; i < rep.atomLimit; ++i) {
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("  atomString[%d] %s", i, rep.atomString(i)));
    }
    // node names
    int numNodeNameReps = decoder.decodeUnsigned();
    if (LOG.isTraceEnabled())
        LOG.trace(String.format("numNodeNameReps %d", numNodeNameReps));
    if (numNodeNameReps == 0) {
        rep.nodeNameNameAtom = null;
        rep.nodeNameNamespaceAtom = null;
    } else {
        rep.nodeNameNameAtom = new int[numNodeNameReps];
        rep.nodeNameNamespaceAtom = new int[numNodeNameReps];
    }
    int xmlSpaceNodeNameRepID = Integer.MAX_VALUE;
    int xmlLangNodeNameRepID = Integer.MAX_VALUE;
    int xmlBaseNodeNameRepID = Integer.MAX_VALUE;
    int xsiTypeNodeNameRepID = Integer.MAX_VALUE;
    for (int j = 0; j < numNodeNameReps; j++) {
        rep.nodeNameNameAtom[j] = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("  nodeNameNameAtom[%d] %d", j, rep.nodeNameNameAtom[j]));
        assert (rep.nodeNameNameAtom[j] < rep.atomLimit);
        rep.nodeNameNamespaceAtom[j] = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("  nodeNameNamespaceAtom[%d] %d", j, rep.nodeNameNamespaceAtom[j]));
        assert (rep.nodeNameNamespaceAtom[j] < rep.atomLimit);
        if (rep.atomEquals(rep.nodeNameNamespaceAtom[j], xmlURIBytes)) {
            if (rep.atomEquals(rep.nodeNameNameAtom[j], spaceBytes))
                xmlSpaceNodeNameRepID = j;
            else if (rep.atomEquals(rep.nodeNameNameAtom[j], langBytes)) {
                xmlLangNodeNameRepID = j;
            } else if (rep.atomEquals(rep.nodeNameNameAtom[j], baseBytes))
                xmlBaseNodeNameRepID = j;
        } else if (rep.atomEquals(rep.nodeNameNameAtom[j], xsiURIBytes)) {
            if (rep.atomEquals(rep.nodeNameNameAtom[j], typeBytes))
                xsiTypeNodeNameRepID = j;
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace(String.format("xmlSpaceNodeNameRepID %d", xmlSpaceNodeNameRepID));
        LOG.trace(String.format("xmlLangNodeNameRepID %d", xmlLangNodeNameRepID));
        LOG.trace(String.format("xmlBaseNodeNameRepID %d", xmlBaseNodeNameRepID));
        LOG.trace(String.format("xsiTypeNodeNameRepID %d", xsiTypeNodeNameRepID));
    }
    int numElemNodeReps = 0;
    int numAttrNodeReps = 0;
    int numDocNodeReps = 0;
    int numPINodeReps = 0;
    int numArrayNodeReps = 0;
    int numDoubles = 0;
    // node counts
    rep.numNodeReps = decoder.decodeUnsigned();
    if (LOG.isTraceEnabled())
        LOG.trace(String.format("numNodeReps %d", rep.numNodeReps));

    if (rep.numNodeReps == 0) {
        // escape
        int version = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("version %d", version));
        assert (version == 0);

        rep.numNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("rep.numNodeReps %d", rep.numNodeReps));
        if (rep.numNodeReps > 0) {
            rep.nodes = new NodeImpl[rep.numNodeReps];
            rep.nodeOrdinal = new long[rep.numNodeReps];
            rep.nodeKind = new byte[rep.numNodeReps];
            rep.nodeRepID = new int[rep.numNodeReps];
            rep.nodeParentNodeRepID = new int[rep.numNodeReps];
        }
        numArrayNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numArrayNodeReps %d", numArrayNodeReps));
        if (numArrayNodeReps > 0) {
            rep.arrayNodeTextRepID = new int[numArrayNodeReps];
            rep.arrayNodeChildNodeRepID = new int[numArrayNodeReps];
            rep.arrayNodeNumChildren = new int[numArrayNodeReps];
        }
        numDoubles = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numDoubles %d", numDoubles));
        if (numDoubles > 0) {
            rep.doubles = new double[numDoubles];
        }
        numDocNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numDocNodeReps %d", numDocNodeReps));
        if (numDocNodeReps > 0) {
            rep.docNodeTextRepID = new int[numDocNodeReps];
            rep.docNodeChildNodeRepID = new int[numDocNodeReps];
            rep.docNodeNumChildren = new int[numDocNodeReps];
        }
    } else {
        // compat
        if (rep.numNodeReps > 0) {
            rep.nodes = new NodeImpl[rep.numNodeReps];
            rep.nodeOrdinal = new long[rep.numNodeReps];
            rep.nodeKind = new byte[rep.numNodeReps];
            rep.nodeRepID = new int[rep.numNodeReps];
            rep.nodeParentNodeRepID = new int[rep.numNodeReps];
        }
        numElemNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numElemNodeReps %d", numElemNodeReps));
        if (numElemNodeReps > 0) {
            rep.elemNodeNodeNameRepID = new int[numElemNodeReps];
            rep.elemNodeAttrNodeRepID = new int[numElemNodeReps];
            rep.elemNodeChildNodeRepID = new int[numElemNodeReps];
            rep.elemNodeElemDeclRepID = new int[numElemNodeReps];
            rep.elemNodeNumAttributes = new int[numElemNodeReps];
            rep.elemNodeNumDefaultAttrs = new int[numElemNodeReps];
            rep.elemNodeNumChildren = new int[numElemNodeReps];
            rep.elemNodeFlags = new int[numElemNodeReps];
        }
        numAttrNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numAttrNodeReps %d", numAttrNodeReps));
        if (numAttrNodeReps > 0) {
            rep.attrNodeNodeNameRepID = new int[numAttrNodeReps];
            rep.attrNodeTextRepID = new int[numAttrNodeReps];
            rep.attrNodeAttrDeclRepID = new int[numAttrNodeReps];
        }
        rep.numLinkNodeReps = decoder.decodeUnsigned() * 4 / 3;
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numLinkNodeReps %d", rep.numLinkNodeReps));
        if (rep.numLinkNodeReps > 0) {
            rep.linkNodeKey = new long[rep.numLinkNodeReps];
            rep.linkNodeNodeCount = new long[rep.numLinkNodeReps];
            rep.linkNodeNodeNameRepID = new int[rep.numLinkNodeReps];
            rep.linkNodeNodeRepID = new int[rep.numLinkNodeReps];
        }
        numDocNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numDocNodeReps %d", numDocNodeReps));
        if (numDocNodeReps > 0) {
            rep.docNodeTextRepID = new int[numDocNodeReps];
            rep.docNodeChildNodeRepID = new int[numDocNodeReps];
            rep.docNodeNumChildren = new int[numDocNodeReps];
        }
        numPINodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numPINodeReps %d", numPINodeReps));
        if (numPINodeReps > 0) {
            rep.piNodeTargetAtom = new int[numPINodeReps];
            rep.piNodeTextRepID = new int[numPINodeReps];
        }
        rep.numNSNodeReps = decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("numNSNodeReps %d", rep.numNSNodeReps));
        if (rep.numNSNodeReps > 0) {
            rep.nsNodeOrdinal = new long[rep.numNSNodeReps];
            rep.nsNodePrevNSNodeRepID = new int[rep.numNSNodeReps];
            rep.nsNodePrefixAtom = new int[rep.numNSNodeReps];
            rep.nsNodeUriAtom = new int[rep.numNSNodeReps];
        }
    }

    rep.numPermNodeReps = decoder.decodeUnsigned();
    if (LOG.isTraceEnabled())
        LOG.trace(String.format("numPermNodeReps %d", rep.numPermNodeReps));
    if (rep.numPermNodeReps > 0) {
        rep.permNodeOrdinal = new long[rep.numPermNodeReps];
        rep.permNodePrevPermNodeRepID = new int[rep.numPermNodeReps];
        rep.permNodeCapability = new Capability[rep.numPermNodeReps];
        rep.permNodeRoleId = new long[rep.numPermNodeReps];
    }
    // uri atoms
    rep.uriTextRepID = 0;
    decodeText(rep, decoder, rep.atomLimit);
    // collection atoms
    rep.colsTextRepID = rep.numTextReps;
    decodeText(rep, decoder, rep.atomLimit);
    // nodes
    int nextDocNodeRep = 0;
    int nextElemNodeRep = 0;
    int nextAttrNodeRep = 0;
    int nextPINodeRep = 0;
    int nextNSNodeRep = 0;
    int nextPermNodeRep = 0;
    int parentNodeRepID = 0;
    int nextArrayNodeRep = 0;
    int nextDouble = 0;
    long lastNSNodeRepOrdinal = 0;
    long lastPermNodeRepOrdinal = 0;
    for (int i = 0; i < rep.numNodeReps; i++) {
        rep.nodeKind[i] = (byte) decoder.decodeUnsigned(4);
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("  nodeKind[%d] %s", i, rep.nodeKind[i]));
        //assert (rep.nodeKind[i] != NodeKind.NULL);
        parentNodeRepID += decoder.decodeUnsigned();
        if (LOG.isTraceEnabled())
            LOG.trace(String.format("  parentNodeRepID[%d] %d", i, parentNodeRepID));
        assert (parentNodeRepID <= i);
        if (parentNodeRepID == i)
            rep.nodeParentNodeRepID[i] = Integer.MAX_VALUE;
        else {
            rep.nodeParentNodeRepID[i] = parentNodeRepID;
            assert (rep.nodeKind[parentNodeRepID] == NodeKind.ELEM
                    || rep.nodeKind[parentNodeRepID] == NodeKind.DOC
                    || rep.nodeKind[parentNodeRepID] == NodeKind.ARRAY
                    || rep.nodeKind[parentNodeRepID] == NodeKind.OBJECT
                    || rep.nodeKind[parentNodeRepID] == NodeKind.LINK);
            int parentRepID = rep.nodeRepID[parentNodeRepID];
            switch (rep.nodeKind[parentNodeRepID]) {
            case NodeKind.ELEM: {
                switch (rep.nodeKind[i]) {
                case NodeKind.ATTR:
                    if (rep.elemNodeAttrNodeRepID[parentRepID] == Integer.MAX_VALUE)
                        rep.elemNodeAttrNodeRepID[parentRepID] = i;
                    assert (rep.elemNodeAttrNodeRepID[parentRepID]
                            + rep.elemNodeNumAttributes[parentRepID] == i);
                    ++rep.elemNodeNumAttributes[parentRepID];
                    break;
                default:
                    if (rep.elemNodeChildNodeRepID[parentRepID] == Integer.MAX_VALUE)
                        rep.elemNodeChildNodeRepID[parentRepID] = i;
                    assert (rep.elemNodeChildNodeRepID[parentRepID]
                            + rep.elemNodeNumChildren[parentRepID] == i);
                    ++rep.elemNodeNumChildren[parentRepID];
                }
                break;
            }
            case NodeKind.DOC: {
                if (rep.docNodeChildNodeRepID[parentNodeRepID] == Integer.MAX_VALUE)
                    rep.docNodeChildNodeRepID[parentNodeRepID] = i;
                assert (rep.docNodeChildNodeRepID[parentNodeRepID]
                        + rep.docNodeNumChildren[parentNodeRepID] == i);
                ++rep.docNodeNumChildren[parentNodeRepID];
                break;
            }
            case NodeKind.ARRAY:
            case NodeKind.OBJECT: {
                if (rep.arrayNodeChildNodeRepID[parentRepID] == Integer.MAX_VALUE)
                    rep.arrayNodeChildNodeRepID[parentRepID] = i;
                assert (rep.arrayNodeChildNodeRepID[parentRepID] + rep.arrayNodeNumChildren[parentRepID] == i);
                ++rep.arrayNodeNumChildren[parentRepID];
                break;
            }
            default:
                break;
            }
        }
        switch (rep.nodeKind[i]) {
        case NodeKind.ELEM: {
            int j = nextElemNodeRep++;
            rep.nodeRepID[i] = j;
            assert (j < numElemNodeReps);
            rep.elemNodeNodeNameRepID[j] = decoder.decodeUnsigned();
            rep.elemNodeAttrNodeRepID[j] = Integer.MAX_VALUE;
            rep.elemNodeChildNodeRepID[j] = Integer.MAX_VALUE;
            rep.elemNodeElemDeclRepID[j] = Integer.MAX_VALUE;
            rep.elemNodeNumAttributes[j] = 0;
            rep.elemNodeNumDefaultAttrs[j] = 0;
            rep.elemNodeNumChildren[j] = 0;
            rep.elemNodeFlags[j] = 0;
            if (rep.elemNodeNodeNameRepID[j] >= numNodeNameReps) {
                rep.elemNodeNumDefaultAttrs[j] = rep.elemNodeNodeNameRepID[j] / numNodeNameReps;
                rep.elemNodeNodeNameRepID[j] = rep.elemNodeNodeNameRepID[j] % numNodeNameReps;
            }
            break;
        }
        case NodeKind.ATTR: {
            assert (parentNodeRepID < i);
            assert (rep.nodeKind[parentNodeRepID] == NodeKind.ELEM);
            rep.nodeRepID[i] = nextAttrNodeRep++;
            assert (rep.nodeRepID[i] < numAttrNodeReps);
            rep.attrNodeNodeNameRepID[rep.nodeRepID[i]] = decoder.decodeUnsigned();
            assert (rep.attrNodeNodeNameRepID[rep.nodeRepID[i]] < numNodeNameReps);
            if (rep.attrNodeNodeNameRepID[rep.nodeRepID[i]] == xmlSpaceNodeNameRepID)
                rep.elemNodeFlags[rep.nodeRepID[parentNodeRepID]] |= xmlSpaceAttrPresentFlag;
            else if (rep.attrNodeNodeNameRepID[rep.nodeRepID[i]] == xmlLangNodeNameRepID)
                rep.elemNodeFlags[rep.nodeRepID[parentNodeRepID]] |= xmlLangAttrPresentFlag;
            else if (rep.attrNodeNodeNameRepID[rep.nodeRepID[i]] == xmlBaseNodeNameRepID)
                rep.elemNodeFlags[rep.nodeRepID[parentNodeRepID]] |= xmlBaseAttrPresentFlag;
            else if (rep.attrNodeNodeNameRepID[rep.nodeRepID[i]] == xsiTypeNodeNameRepID)
                rep.elemNodeFlags[rep.nodeRepID[parentNodeRepID]] |= xsiTypeAttrPresentFlag;
            rep.attrNodeTextRepID[rep.nodeRepID[i]] = rep.numTextReps;
            decodeText(rep, decoder, rep.atomLimit);
            rep.attrNodeAttrDeclRepID[rep.nodeRepID[i]] = Integer.MAX_VALUE;
            break;
        }
        case NodeKind.TEXT: {
            rep.nodeRepID[i] = rep.numTextReps;
            decodeText(rep, decoder, rep.atomLimit);
            break;
        }
        case NodeKind.BINARY: {
            rep.nodeRepID[i] = 0;
            int nbytes = decoder.decodeUnsigned();
            if (nbytes > MAX_BINARY_BYTES) { // large binary
                rep.binaryKey = decoder.decode64bits();
                rep.binaryOffset = decoder.decodeUnsignedLong();
                rep.binarySize = decoder.decodeUnsignedLong();
                rep.binaryOrigLen = decoder.decodeUnsignedLong();
                rep.binaryPathAtom = decoder.decodeUnsigned();
            } else {
                decodeBinary(decoder, rep, nbytes);
            }
            break;
        }
        case NodeKind.PI: {
            int piNodeRep = rep.nodeRepID[i] = nextPINodeRep++;
            assert (piNodeRep < numPINodeReps);
            int targetAtom = rep.piNodeTargetAtom[piNodeRep] = decoder.decodeUnsigned();
            assert (targetAtom < rep.atomLimit);
            rep.piNodeTextRepID[piNodeRep] = rep.numTextReps;
            decodeText(rep, decoder, rep.atomLimit);
            break;
        }
        case NodeKind.LINK: {
            long key = decoder.decode64bits();
            int linkNodeRep = (int) (key % rep.numLinkNodeReps);
            while (true) {
                if (rep.linkNodeKey[linkNodeRep] == 0) {
                    rep.nodeRepID[i] = linkNodeRep;
                    rep.linkNodeKey[linkNodeRep] = key;
                    rep.linkNodeNodeCount[linkNodeRep] = decoder.decodeUnsignedLong();
                    rep.linkNodeNodeNameRepID[linkNodeRep] = decoder.decodeUnsigned();
                    assert (rep.linkNodeNodeNameRepID[linkNodeRep] < numNodeNameReps);
                    rep.linkNodeNodeRepID[linkNodeRep] = i;
                    break;
                }
                linkNodeRep = hashWrap(linkNodeRep + 1, rep.numLinkNodeReps);
            }
            break;
        }
        case NodeKind.COMMENT: {
            rep.nodeRepID[i] = rep.numTextReps;
            decodeText(rep, decoder, rep.atomLimit);
            break;
        }
        case NodeKind.DOC: {
            int docNode = rep.nodeRepID[i] = nextDocNodeRep++;
            assert (docNode < numDocNodeReps);
            rep.docNodeTextRepID[i] = rep.numTextReps;
            decodeText(rep, decoder, rep.atomLimit);
            rep.docNodeChildNodeRepID[docNode] = Integer.MAX_VALUE;
            rep.docNodeNumChildren[docNode] = 0;
            break;
        }
        case NodeKind.NS: {
            int nsNode = rep.nodeRepID[i] = nextNSNodeRep++;
            assert (nsNode < rep.numNSNodeReps);
            lastNSNodeRepOrdinal = rep.nsNodeOrdinal[nsNode] = lastNSNodeRepOrdinal
                    + decoder.decodeUnsignedLong();
            rep.nsNodePrevNSNodeRepID[nsNode] = rep.nodeRepID[i] - decoder.decodeUnsigned() - 1;
            assert (rep.nsNodePrevNSNodeRepID[nsNode] < rep.numNSNodeReps
                    || rep.nsNodePrevNSNodeRepID[nsNode] == Integer.MAX_VALUE);
            rep.nsNodePrefixAtom[nsNode] = decoder.decodeUnsigned() - 1;
            assert (rep.nsNodePrefixAtom[nsNode] < rep.atomLimit
                    || rep.nsNodePrefixAtom[nsNode] == Integer.MAX_VALUE);
            rep.nsNodeUriAtom[nsNode] = decoder.decodeUnsigned() - 1;
            assert (rep.nsNodeUriAtom[nsNode] < rep.atomLimit
                    || rep.nsNodeUriAtom[nsNode] == Integer.MAX_VALUE);
            break;
        }
        case NodeKind.PERM: {
            int permNode = rep.nodeRepID[i] = nextPermNodeRep++;
            assert (permNode < rep.numPermNodeReps);
            lastPermNodeRepOrdinal = rep.permNodeOrdinal[permNode] = lastPermNodeRepOrdinal
                    + decoder.decodeUnsignedLong();
            long prevPermNode = rep.permNodePrevPermNodeRepID[permNode] = permNode - decoder.decodeUnsigned()
                    - 1;
            assert (prevPermNode < rep.numPermNodeReps || prevPermNode == Integer.MAX_VALUE);
            Capability capability = rep.permNodeCapability[permNode] = Capability.values()[decoder
                    .decodeUnsigned(4)];
            assert (capability != Capability.NULL);
            long roleId = rep.permNodeRoleId[permNode] = decoder.decode64bits();
            assert (roleId < Long.MAX_VALUE);
            break;
        }
        case NodeKind.NULL: {
            switch (decoder.decodeUnsigned(3)) {
            case 1: {
                rep.nodeKind[i] = NodeKind.BOOLEAN;
                rep.nodeRepID[i] = 0;
                break;
            }
            case 2: {
                rep.nodeKind[i] = NodeKind.BOOLEAN;
                rep.nodeRepID[i] = 1;
                break;
            }
            case 3: {
                rep.nodeKind[i] = NodeKind.NUMBER;
                rep.nodeRepID[i] = nextDouble++;
                assert (rep.nodeRepID[i] < numDoubles);
                rep.doubles[rep.nodeRepID[i]] = decoder.decodeDouble();
                break;
            }
            case 4: {
                rep.nodeKind[i] = NodeKind.ARRAY;
                rep.nodeRepID[i] = nextArrayNodeRep++;
                assert (rep.nodeRepID[i] < numArrayNodeReps);
                rep.arrayNodeTextRepID[rep.nodeRepID[i]] = Integer.MAX_VALUE;
                rep.arrayNodeChildNodeRepID[rep.nodeRepID[i]] = Integer.MAX_VALUE;
                rep.arrayNodeNumChildren[rep.nodeRepID[i]] = 0;
                break;
            }
            case 5: {
                rep.nodeKind[i] = NodeKind.OBJECT;
                rep.nodeRepID[i] = nextArrayNodeRep++;
                assert (rep.nodeRepID[i] < numArrayNodeReps);
                rep.arrayNodeTextRepID[rep.nodeRepID[i]] = rep.numTextReps;
                rep.arrayNodeChildNodeRepID[rep.nodeRepID[i]] = Integer.MAX_VALUE;
                rep.arrayNodeNumChildren[rep.nodeRepID[i]] = 0;
                int numKeys = decoder.decodeUnsigned();
                addText(rep, numKeys);
                int atomLimit = rep.atomLimit;
                for (int j = 0; j < numKeys; ++j) {
                    int atom = decoder.decodeUnsigned();
                    assert (atom < atomLimit);
                    if (atom >= atomLimit) {
                        bad = "atom";
                        if (LOG.isTraceEnabled())
                            LOG.trace(String.format("bad atom %d atomLimit %d", atom, atomLimit));
                    }
                    rep.textReps[rep.numTextReps++] = atom;
                }
                break;
            }
            default:
                break;
            }
            break;
        }
        default:
            break;
        }
    }
    if (rep.numNodeReps > 0) {
        assignOrdinals(rep);
    }
    return rep;
}

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

@Override
public void decode() {
    try {/*from  w w  w .  j  a  v  a2 s. c om*/
        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:Comman.Tool.java

public byte[] Image_decompress(final byte[] data) {
    if (data == null || data.length == 0) {
        return new byte[0];
    }/* w  w w  . jav  a2s.  co m*/

    final Inflater inflater = new Inflater();
    inflater.setInput(data);

    try (final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length)) {
        final byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            out.write(buffer, 0, inflater.inflate(buffer));
        }

        return out.toByteArray();
    } catch (final IOException | DataFormatException e) {
        System.err.println("Decompression failed! Returning the compressed data...");
        return data;
    }
}

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());//  w w  w. j a va  2 s . 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.fastcatsearch.ir.document.DocumentWriter.java

public Document readDocument(int docNo) throws IOException, IRException {
    long prevPosPos = positionOutput.position();
    long docPos = -1;
    try {//ww  w .  jav a 2  s  .  co  m
        long positionOffset = ((long) docNo) * IOUtil.SIZE_OF_LONG;
        positionOutput.seek(positionOffset);
        docPos = IOUtil.readLong(positionOutput.getRaf());
    } finally {
        positionOutput.seek(prevPosPos);
    }

    // find a document block
    long prevDocPos = docOutput.position();
    try {
        docOutput.seek(docPos);
        RandomAccessFile raf = docOutput.getRaf();
        int len = IOUtil.readInt(raf);
        long n = raf.getFilePointer();
        InputStream docInput = Channels.newInputStream(docOutput.getRaf().getChannel().position(n));
        //2014-11-26 ?  working ?   ? ? GC ?? OOM ? ?.
        // Stream  .
        InflaterInputStream decompressInputStream = null;
        inflaterOutput.reset();
        int count = -1;
        try {
            BoundedInputStream boundedInputStream = new BoundedInputStream(docInput, len);
            boundedInputStream.setPropagateClose(false);// docInput  .
            decompressInputStream = new InflaterInputStream(boundedInputStream, new Inflater(), 512);
            while ((count = decompressInputStream.read(workingBuffer)) != -1) {
                inflaterOutput.write(workingBuffer, 0, count);
            }
        } finally {
            decompressInputStream.close();
        }
    } finally {
        docOutput.seek(prevDocPos);
    }

    BytesRef bytesRef = inflaterOutput.getBytesRef();
    DataInput bai = new BytesDataInput(bytesRef.bytes, 0, bytesRef.length);

    Document document = new Document(fields.size());
    for (int i = 0; i < fields.size(); i++) {
        FieldSetting fs = fields.get(i);
        Field f = null;
        boolean hasValue = bai.readBoolean();
        if (hasValue) {
            f = fs.createEmptyField();
            f.readRawFrom(bai);
        } else {
            f = fs.createEmptyField();
        }
        if (f != null) {
            String multiValueDelimiter = fs.getMultiValueDelimiter();
            try {
                f.parseIndexable(multiValueDelimiter);
            } catch (FieldDataParseException e) {
                throw new IOException(e);
            }
        }
        document.add(f);
    }
    document.setDocId(docNo);
    return document;
}

From source file:edu.umn.msi.tropix.proteomics.conversion.impl.ConversionUtils.java

public static byte[] decompress(byte[] compressedData) {
    byte[] decompressedData;

    // using a ByteArrayOutputStream to not having to define the result array size beforehand
    Inflater decompressor = new Inflater();

    decompressor.setInput(compressedData);
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {/*from   w ww.j  a  v a2s  .c om*/
            int count = decompressor.inflate(buf);
            if (count == 0 && decompressor.needsInput()) {
                break;
            }
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IllegalStateException(
                    "Encountered wrong data format " + "while trying to decompress binary data!", e);
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
        // ToDo: add logging
        e.printStackTrace();
    }
    // Get the decompressed data
    decompressedData = bos.toByteArray();

    if (decompressedData == null) {
        throw new IllegalStateException("Decompression of binary data produced no result (null)!");
    }
    return decompressedData;
}

From source file:org.apache.qpid.multiconsumer.AMQTest.java

private String inflateString(String string) throws Exception {
    byte[] input = string.getBytes();

    // First convert Base64 string back to binary array
    byte[] bytes = Base64.decodeBase64(input);

    // Set string as input data for decompressor
    Inflater decompressor = new Inflater();
    decompressor.setInput(bytes);// www  .  j  a v a2 s  .  c  o  m

    // Decompress the data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] output = bos.toByteArray();

    // Get the decompressed data
    return new String(output, UTF8);
}

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

/**
 * UnCompress payload using GZIP Algorithm
 *
 * @param body byte array// www . j av  a 2 s  .  co  m
 * @return decompressed bytes.
 * @throws IOException
 */
private byte[] ungzip(byte[] body) throws IOException {
    Inflater inf = new Inflater();
    ByteArrayInputStream bytein = new ByteArrayInputStream(body);
    GZIPInputStream gzin = new GZIPInputStream(bytein);
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();

    int res = 0;
    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;
}