001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util;
019
020
021import java.io.ByteArrayInputStream;
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.util.zip.Deflater;
025import java.util.zip.DeflaterOutputStream;
026import java.util.zip.InflaterInputStream;
027import java.util.zip.Inflater;
028
029
030/**
031 * Deflate (RFC 1951) utilities.
032 *
033 * @author Vladimir Dzhuvinov
034 * @version 2013-04-16
035 */
036public class DeflateUtils {
037
038
039        /**
040         * Omit headers and CRC fields from output, as specified by RFC 1950.
041         * Note that the Deflater JavaDocs are incorrect, see
042         * http://stackoverflow.com/questions/11076060/decompressing-gzipped-data-with-inflater-in-java
043         */
044        private static final boolean NOWRAP = true;
045
046
047        /**
048         * Compresses the specified byte array according to the DEFLATE 
049         * specification (RFC 1951).
050         *
051         * @param bytes The byte array to compress. Must not be {@code null}.
052         *
053         * @return The compressed bytes.
054         *
055         * @throws IOException If compression failed.
056         */
057        public static byte[] compress(final byte[] bytes)
058                throws IOException {
059
060                ByteArrayOutputStream out = new ByteArrayOutputStream();
061
062
063                Deflater deflater = null;
064                DeflaterOutputStream def = null;
065                try {
066                        deflater = new Deflater(Deflater.DEFLATED, NOWRAP);
067                        def = new DeflaterOutputStream(out, deflater);
068                        def.write(bytes);
069                } finally {
070                        if(def != null) {
071                                def.close();
072                        }
073                        if(deflater != null) {
074                                deflater.end();
075                        }
076                }
077
078                return out.toByteArray();
079        }
080
081
082        /**
083         * Decompresses the specified byte array according to the DEFLATE
084         * specification (RFC 1951).
085         *
086         * @param bytes The byte array to decompress. Must not be {@code null}.
087         *
088         * @return The decompressed bytes.
089         *
090         * @throws IOException If decompression failed.
091         */
092        public static byte[] decompress(final byte[] bytes)
093                        throws IOException {
094
095                Inflater inflater = null;
096                InflaterInputStream inf = null;
097                try {
098                        inflater = new Inflater(NOWRAP);
099                        inf = new InflaterInputStream(new ByteArrayInputStream(bytes), inflater);
100
101                        ByteArrayOutputStream out = new ByteArrayOutputStream();
102
103                        // Transfer bytes from the compressed array to the output
104                        byte[] buf = new byte[1024];
105
106                        int len;
107
108                        while ((len = inf.read(buf)) > 0) {
109
110                                out.write(buf, 0, len);
111                        }
112
113                        return out.toByteArray();
114                } finally {
115                        if(inf != null) {
116                                inf.close();
117                        }
118                        if(inflater != null) {
119                                inflater.end();
120                        }
121                }
122        }
123
124
125        /**
126         * Prevents public instantiation.
127         */
128        private DeflateUtils() {
129
130        }
131}