001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.common;
018
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021import java.util.zip.DataFormatException;
022import java.util.zip.DeflaterOutputStream;
023import java.util.zip.Inflater;
024
025import org.apache.commons.imaging.ImageReadException;
026import org.apache.commons.imaging.ImageWriteException;
027
028/**
029 * <p>
030 * Utility class to compress/decompress bytes using the ZLIB deflate/inflate
031 * compression.
032 * </p>
033 *
034 * <p>
035 * <a href="http://www.ietf.org/rfc/rfc1951.txt">RFC 1951 - DEFLATE Compressed
036 * Data Format Specification version 1.3</a>
037 * </p>
038 */
039public class ZlibDeflate {
040
041    /**
042     * Compress the byte[] using ZLIB deflate decompression.
043     *
044     * @param bytes The bytes to decompress
045     * @param expectedSize The expected size of the decompressed byte[].
046     *
047     * @return The decompressed bytes.
048     * @throws ImageReadException if the bytes could not be decompressed.
049     * @see Inflater
050     */
051    public static byte[] decompress(final byte[] bytes, final int expectedSize) throws ImageReadException {
052        try {
053            final Inflater inflater = new Inflater();
054            inflater.setInput(bytes);
055            final byte[] result = new byte[expectedSize];
056            inflater.inflate(result);
057            return result;
058        } catch (final DataFormatException e) {
059            throw new ImageReadException("Unable to decompress image", e);
060        }
061    }
062
063    /**
064     * Compress the byte[] using ZLIB deflate compression.
065     *
066     * @param bytes The bytes to compress
067     *
068     * @return The compressed bytes.
069     * @throws ImageWriteException if the bytes could not be compressed.
070     * @see DeflaterOutputStream
071     */
072    public static byte[] compress(final byte[] bytes) throws ImageWriteException {
073        final ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length / 2);
074        try (DeflaterOutputStream compressOut = new DeflaterOutputStream(out)) {
075            compressOut.write(bytes);
076        } catch (final IOException e) {
077            throw new ImageWriteException("Unable to compress image", e);
078        }
079        return out.toByteArray();
080    }
081
082}