View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.common;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.util.zip.DataFormatException;
22  import java.util.zip.DeflaterOutputStream;
23  import java.util.zip.Inflater;
24  
25  import org.apache.commons.imaging.ImagingException;
26  
27  /**
28   * <p>
29   * Utility class to compress/decompress bytes using the ZLIB deflate/inflate compression.
30   * </p>
31   *
32   * <p>
33   * <a href="https://www.ietf.org/rfc/rfc1951.txt">RFC 1951 - DEFLATE Compressed Data Format Specification version 1.3</a>
34   * </p>
35   */
36  public final class ZlibDeflate {
37  
38      /**
39       * Compress the byte[] using ZLIB deflate compression.
40       *
41       * @param bytes The bytes to compress
42       *
43       * @return The compressed bytes.
44       * @throws ImagingException if the bytes could not be compressed.
45       * @see DeflaterOutputStream
46       */
47      public static byte[] compress(final byte[] bytes) throws ImagingException {
48          final ByteArrayOutputStream out = new ByteArrayOutputStream(Allocator.checkByteArray(bytes.length / 2));
49          try (DeflaterOutputStream compressOut = new DeflaterOutputStream(out)) {
50              compressOut.write(bytes);
51          } catch (final IOException e) {
52              throw new ImagingException("Unable to compress image", e);
53          }
54          return out.toByteArray();
55      }
56  
57      /**
58       * Compress the byte[] using ZLIB deflate decompression.
59       *
60       * @param bytes        The bytes to decompress
61       * @param expectedSize The expected size of the decompressed byte[].
62       *
63       * @return The decompressed bytes.
64       * @throws ImagingException if the bytes could not be decompressed.
65       * @see Inflater
66       */
67      public static byte[] decompress(final byte[] bytes, final int expectedSize) throws ImagingException {
68          try {
69              final Inflater inflater = new Inflater();
70              inflater.setInput(bytes);
71              final byte[] result = Allocator.byteArray(expectedSize);
72              inflater.inflate(result);
73              return result;
74          } catch (final DataFormatException e) {
75              throw new ImagingException("Unable to decompress image", e);
76          }
77      }
78  
79      private ZlibDeflate() {
80          // empty
81      }
82  }