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.ImagingException;
026
027/**
028 * <p>
029 * Utility class to compress/decompress bytes using the ZLIB deflate/inflate compression.
030 * </p>
031 *
032 * <p>
033 * <a href="https://www.ietf.org/rfc/rfc1951.txt">RFC 1951 - DEFLATE Compressed Data Format Specification version 1.3</a>
034 * </p>
035 */
036public final class ZlibDeflate {
037
038    /**
039     * Compress the byte[] using ZLIB deflate compression.
040     *
041     * @param bytes The bytes to compress
042     *
043     * @return The compressed bytes.
044     * @throws ImagingException if the bytes could not be compressed.
045     * @see DeflaterOutputStream
046     */
047    public static byte[] compress(final byte[] bytes) throws ImagingException {
048        final ByteArrayOutputStream out = new ByteArrayOutputStream(Allocator.checkByteArray(bytes.length / 2));
049        try (DeflaterOutputStream compressOut = new DeflaterOutputStream(out)) {
050            compressOut.write(bytes);
051        } catch (final IOException e) {
052            throw new ImagingException("Unable to compress image", e);
053        }
054        return out.toByteArray();
055    }
056
057    /**
058     * Compress the byte[] using ZLIB deflate decompression.
059     *
060     * @param bytes        The bytes to decompress
061     * @param expectedSize The expected size of the decompressed byte[].
062     *
063     * @return The decompressed bytes.
064     * @throws ImagingException if the bytes could not be decompressed.
065     * @see Inflater
066     */
067    public static byte[] decompress(final byte[] bytes, final int expectedSize) throws ImagingException {
068        try {
069            final Inflater inflater = new Inflater();
070            inflater.setInput(bytes);
071            final byte[] result = Allocator.byteArray(expectedSize);
072            inflater.inflate(result);
073            return result;
074        } catch (final DataFormatException e) {
075            throw new ImagingException("Unable to decompress image", e);
076        }
077    }
078
079    private ZlibDeflate() {
080        // empty
081    }
082}