View Javadoc
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  
20  package org.apache.commons.compress.compressors.pack200;
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.util.Map;
25  import java.util.jar.JarInputStream;
26  
27  import org.apache.commons.compress.compressors.CompressorOutputStream;
28  import org.apache.commons.compress.java.util.jar.Pack200;
29  
30  /**
31   * An output stream that compresses using the Pack200 format.
32   *
33   * @NotThreadSafe
34   * @since 1.3
35   */
36  public class Pack200CompressorOutputStream extends CompressorOutputStream {
37      private boolean finished;
38      private final OutputStream originalOutput;
39      private final AbstractStreamBridge abstractStreamBridge;
40      private final Map<String, String> properties;
41  
42      /**
43       * Compresses the given stream, caching the compressed data in memory.
44       *
45       * @param out the stream to write to
46       * @throws IOException if writing fails
47       */
48      public Pack200CompressorOutputStream(final OutputStream out) throws IOException {
49          this(out, Pack200Strategy.IN_MEMORY);
50      }
51  
52      /**
53       * Compresses the given stream, caching the compressed data in memory and using the given properties.
54       *
55       * @param out   the stream to write to
56       * @param props Pack200 properties to use
57       * @throws IOException if writing fails
58       */
59      public Pack200CompressorOutputStream(final OutputStream out, final Map<String, String> props) throws IOException {
60          this(out, Pack200Strategy.IN_MEMORY, props);
61      }
62  
63      /**
64       * Compresses the given stream using the given strategy to cache the results.
65       *
66       * @param out  the stream to write to
67       * @param mode the strategy to use
68       * @throws IOException if writing fails
69       */
70      public Pack200CompressorOutputStream(final OutputStream out, final Pack200Strategy mode) throws IOException {
71          this(out, mode, null);
72      }
73  
74      /**
75       * Compresses the given stream using the given strategy to cache the results and the given properties.
76       *
77       * @param out   the stream to write to
78       * @param mode  the strategy to use
79       * @param props Pack200 properties to use
80       * @throws IOException if writing fails
81       */
82      public Pack200CompressorOutputStream(final OutputStream out, final Pack200Strategy mode, final Map<String, String> props) throws IOException {
83          originalOutput = out;
84          abstractStreamBridge = mode.newStreamBridge();
85          properties = props;
86      }
87  
88      @Override
89      public void close() throws IOException {
90          try {
91              finish();
92          } finally {
93              try {
94                  abstractStreamBridge.stop();
95              } finally {
96                  originalOutput.close();
97              }
98          }
99      }
100 
101     public void finish() throws IOException {
102         if (!finished) {
103             finished = true;
104             final Pack200.Packer p = Pack200.newPacker();
105             if (properties != null) {
106                 p.properties().putAll(properties);
107             }
108             try (JarInputStream ji = new JarInputStream(abstractStreamBridge.getInputStream())) {
109                 p.pack(ji, originalOutput);
110             }
111         }
112     }
113 
114     @Override
115     public void write(final byte[] b) throws IOException {
116         abstractStreamBridge.write(b);
117     }
118 
119     @Override
120     public void write(final byte[] b, final int from, final int length) throws IOException {
121         abstractStreamBridge.write(b, from, length);
122     }
123 
124     @Override
125     public void write(final int b) throws IOException {
126         abstractStreamBridge.write(b);
127     }
128 }