Pack200CompressorOutputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.compress.compressors.pack200;

  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.Map;
  23. import java.util.jar.JarInputStream;

  24. import org.apache.commons.compress.compressors.CompressorOutputStream;
  25. import org.apache.commons.compress.java.util.jar.Pack200;

  26. /**
  27.  * An output stream that compresses using the Pack200 format.
  28.  *
  29.  * @NotThreadSafe
  30.  * @since 1.3
  31.  */
  32. public class Pack200CompressorOutputStream extends CompressorOutputStream<OutputStream> {
  33.     private boolean finished;
  34.     private final AbstractStreamBridge abstractStreamBridge;
  35.     private final Map<String, String> properties;

  36.     /**
  37.      * Compresses the given stream, caching the compressed data in memory.
  38.      *
  39.      * @param out the stream to write to
  40.      * @throws IOException if writing fails
  41.      */
  42.     public Pack200CompressorOutputStream(final OutputStream out) throws IOException {
  43.         this(out, Pack200Strategy.IN_MEMORY);
  44.     }

  45.     /**
  46.      * Compresses the given stream, caching the compressed data in memory and using the given properties.
  47.      *
  48.      * @param out   the stream to write to
  49.      * @param props Pack200 properties to use
  50.      * @throws IOException if writing fails
  51.      */
  52.     public Pack200CompressorOutputStream(final OutputStream out, final Map<String, String> props) throws IOException {
  53.         this(out, Pack200Strategy.IN_MEMORY, props);
  54.     }

  55.     /**
  56.      * Compresses the given stream using the given strategy to cache the results.
  57.      *
  58.      * @param out  the stream to write to
  59.      * @param mode the strategy to use
  60.      * @throws IOException if writing fails
  61.      */
  62.     public Pack200CompressorOutputStream(final OutputStream out, final Pack200Strategy mode) throws IOException {
  63.         this(out, mode, null);
  64.     }

  65.     /**
  66.      * Compresses the given stream using the given strategy to cache the results and the given properties.
  67.      *
  68.      * @param out   the stream to write to
  69.      * @param mode  the strategy to use
  70.      * @param props Pack200 properties to use
  71.      * @throws IOException if writing fails
  72.      */
  73.     public Pack200CompressorOutputStream(final OutputStream out, final Pack200Strategy mode, final Map<String, String> props) throws IOException {
  74.         super(out);
  75.         abstractStreamBridge = mode.newStreamBridge();
  76.         properties = props;
  77.     }

  78.     @Override
  79.     public void close() throws IOException {
  80.         try {
  81.             finish();
  82.         } finally {
  83.             try {
  84.                 abstractStreamBridge.stop();
  85.             } finally {
  86.                 out.close();
  87.             }
  88.         }
  89.     }

  90.     public void finish() throws IOException {
  91.         if (!finished) {
  92.             finished = true;
  93.             final Pack200.Packer p = Pack200.newPacker();
  94.             if (properties != null) {
  95.                 p.properties().putAll(properties);
  96.             }
  97.             try (JarInputStream ji = new JarInputStream(abstractStreamBridge.getInputStream())) {
  98.                 p.pack(ji, out);
  99.             }
  100.         }
  101.     }

  102.     @Override
  103.     public void write(final byte[] b) throws IOException {
  104.         abstractStreamBridge.write(b);
  105.     }

  106.     @Override
  107.     public void write(final byte[] b, final int from, final int length) throws IOException {
  108.         abstractStreamBridge.write(b, from, length);
  109.     }

  110.     @Override
  111.     public void write(final int b) throws IOException {
  112.         abstractStreamBridge.write(b);
  113.     }
  114. }