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                DeflaterOutputStream def = new DeflaterOutputStream(out, new Deflater(Deflater.DEFLATED, NOWRAP));
063                def.write(bytes);
064                def.close();
065
066                return out.toByteArray();
067        }
068
069
070        /**
071         * Decompresses the specified byte array according to the DEFLATE
072         * specification (RFC 1951).
073         *
074         * @param bytes The byte array to decompress. Must not be {@code null}.
075         *
076         * @return The decompressed bytes.
077         *
078         * @throws IOException If decompression failed.
079         */
080        public static byte[] decompress(final byte[] bytes)
081                throws IOException {
082
083                InflaterInputStream inf = new InflaterInputStream(new ByteArrayInputStream(bytes), new Inflater(NOWRAP));
084                ByteArrayOutputStream out = new ByteArrayOutputStream();
085
086                // Transfer bytes from the compressed array to the output
087                byte[] buf = new byte[1024];
088
089                int len;
090
091                while ((len = inf.read(buf)) > 0) {
092
093                        out.write(buf, 0, len);
094                }
095
096                inf.close();
097                out.close();
098
099                return out.toByteArray();
100        }
101
102
103        /**
104         * Prevents public instantiation.
105         */
106        private DeflateUtils() {
107
108        }
109}