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    package org.apache.commons.compress.compressors;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    
025    import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
026    import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
027    import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
028    import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
029    
030    /**
031     * <p>Factory to create Compressor[In|Out]putStreams from names. To add other
032     * implementations you should extend CompressorStreamFactory and override the
033     * appropriate methods (and call their implementation from super of course).</p>
034     * 
035     * Example (Compressing a file):
036     * 
037     * <pre>
038     * final OutputStream out = new FileOutputStream(output); 
039     * CompressorOutputStream cos = 
040     *      new CompressorStreamFactory().createCompressorOutputStream("bzip2", out);
041     * IOUtils.copy(new FileInputStream(input), cos);
042     * cos.close();
043     * </pre>    
044     * 
045     * Example (Compressing a file):
046     * <pre>
047     * final InputStream is = new FileInputStream(input); 
048     * CompressorInputStream in = 
049     *      new CompressorStreamFactory().createCompressorInputStream("bzip2", is);
050     * IOUtils.copy(in, new FileOutputStream(output));
051     * in.close();
052     * </pre>
053     * 
054     * @Immutable
055     */
056    public class CompressorStreamFactory {
057    
058        /**
059         * Create a compressor input stream from a compressor name and an input stream.
060         * 
061         * @param name of the compressor, i.e. "gz" or "bzip2"
062         * @param in the input stream
063         * @return compressor input stream
064         * @throws CompressorException if the compressor name is not known
065         * @throws IllegalArgumentException if the name or input stream is null
066         */
067        public CompressorInputStream createCompressorInputStream(final String name,
068                final InputStream in) throws CompressorException {
069            if (name == null || in == null) {
070                throw new IllegalArgumentException(
071                        "Compressor name and stream must not be null.");
072            }
073    
074            try {
075                if ("gz".equalsIgnoreCase(name)) {
076                    return new GzipCompressorInputStream(in);
077                } else if ("bzip2".equalsIgnoreCase(name)) {
078                    return new BZip2CompressorInputStream(in);
079                }
080            } catch (IOException e) {
081                throw new CompressorException(
082                        "Could not create CompressorInputStream", e);
083            }
084            throw new CompressorException("Compressor: " + name + " not found.");
085        }
086    
087        /**
088         * Create an compressor output stream from an compressor name and an input stream.
089         * 
090         * @param name the compressor name, i.e. "gz" or "bzip2"
091         * @param out the output stream
092         * @return the compressor output stream
093         * @throws CompressorException if the archiver name is not known
094         * @throws IllegalArgumentException if the archiver name or stream is null
095         */
096        public CompressorOutputStream createCompressorOutputStream(
097                final String name, final OutputStream out)
098                throws CompressorException {
099            if (name == null || out == null) {
100                throw new IllegalArgumentException(
101                        "Compressor name and stream must not be null.");
102            }
103    
104            try {
105                if ("gz".equalsIgnoreCase(name)) {
106                    return new GzipCompressorOutputStream(out);
107                } else if ("bzip2".equalsIgnoreCase(name)) {
108                    return new BZip2CompressorOutputStream(out);
109                }
110            } catch (IOException e) {
111                throw new CompressorException(
112                        "Could not create CompressorOutputStream", e);
113            }
114            throw new CompressorException("Compressor: " + name + " not found.");
115        }
116    }