001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.commons.compress.compressors.pack200;
021
022import java.io.IOException;
023import java.io.OutputStream;
024import java.util.Map;
025import java.util.jar.JarInputStream;
026
027import org.apache.commons.compress.compressors.CompressorOutputStream;
028import org.apache.commons.compress.java.util.jar.Pack200;
029
030/**
031 * An output stream that compresses using the Pack200 format.
032 *
033 * @NotThreadSafe
034 * @since 1.3
035 */
036public class Pack200CompressorOutputStream extends CompressorOutputStream {
037    private boolean finished;
038    private final OutputStream originalOutput;
039    private final AbstractStreamBridge abstractStreamBridge;
040    private final Map<String, String> properties;
041
042    /**
043     * Compresses the given stream, caching the compressed data in
044     * memory.
045     *
046     * @param out the stream to write to
047     * @throws IOException if writing fails
048     */
049    public Pack200CompressorOutputStream(final OutputStream out)
050        throws IOException {
051        this(out, Pack200Strategy.IN_MEMORY);
052    }
053
054    /**
055     * Compresses the given stream, caching the compressed data in
056     * memory and using the given properties.
057     *
058     * @param out the stream to write to
059     * @param props Pack200 properties to use
060     * @throws IOException if writing fails
061     */
062    public Pack200CompressorOutputStream(final OutputStream out,
063                                         final Map<String, String> props)
064        throws IOException {
065        this(out, Pack200Strategy.IN_MEMORY, props);
066    }
067
068    /**
069     * Compresses the given stream using the given strategy to cache
070     * the results.
071     *
072     * @param out the stream to write to
073     * @param mode the strategy to use
074     * @throws IOException if writing fails
075     */
076    public Pack200CompressorOutputStream(final OutputStream out,
077                                         final Pack200Strategy mode)
078        throws IOException {
079        this(out, mode, null);
080    }
081
082    /**
083     * Compresses the given stream using the given strategy to cache
084     * the results and the given properties.
085     *
086     * @param out the stream to write to
087     * @param mode the strategy to use
088     * @param props Pack200 properties to use
089     * @throws IOException if writing fails
090     */
091    public Pack200CompressorOutputStream(final OutputStream out,
092                                         final Pack200Strategy mode,
093                                         final Map<String, String> props)
094        throws IOException {
095        originalOutput = out;
096        abstractStreamBridge = mode.newStreamBridge();
097        properties = props;
098    }
099
100    @Override
101    public void close() throws IOException {
102        try {
103            finish();
104        } finally {
105            try {
106                abstractStreamBridge.stop();
107            } finally {
108                originalOutput.close();
109            }
110        }
111    }
112
113    public void finish() throws IOException {
114        if (!finished) {
115            finished = true;
116            final Pack200.Packer p = Pack200.newPacker();
117            if (properties != null) {
118                p.properties().putAll(properties);
119            }
120            try (JarInputStream ji = new JarInputStream(abstractStreamBridge.getInputStream())) {
121                p.pack(ji, originalOutput);
122            }
123        }
124    }
125
126    @Override
127    public void write(final byte[] b) throws IOException {
128        abstractStreamBridge.write(b);
129    }
130
131    @Override
132    public void write(final byte[] b, final int from, final int length) throws IOException {
133        abstractStreamBridge.write(b, from, length);
134    }
135
136    @Override
137    public void write(final int b) throws IOException {
138        abstractStreamBridge.write(b);
139    }
140}