Torsten Curdt | 444238f | 2008-07-10 21:36:43 +0000 | [diff] [blame] | 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 | */ |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 19 | package org.apache.commons.compress.compressors; |
| 20 | |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 21 | import java.io.IOException; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 22 | import java.io.InputStream; |
| 23 | import java.io.OutputStream; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 24 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 25 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; |
| 26 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; |
| 27 | import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; |
| 28 | import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; |
| 29 | |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 30 | /** |
Christian Grobmeier | 0ef0fc4 | 2009-04-14 04:56:41 +0000 | [diff] [blame] | 31 | * <p>Factory to create Compressor[In|Out]putStreams from names. To add other |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 32 | * implementations you should extend CompressorStreamFactory and override the |
Christian Grobmeier | 0ef0fc4 | 2009-04-14 04:56:41 +0000 | [diff] [blame] | 33 | * appropriate methods (and call their implementation from super of course).</p> |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 34 | * |
Christian Grobmeier | 0ef0fc4 | 2009-04-14 04:56:41 +0000 | [diff] [blame] | 35 | * Example (Compressing a file): |
| 36 | * |
| 37 | * <pre> |
| 38 | * final OutputStream out = new FileOutputStream(output); |
| 39 | * CompressorOutputStream cos = |
| 40 | * new CompressorStreamFactory().createCompressorOutputStream("bzip2", out); |
| 41 | * IOUtils.copy(new FileInputStream(input), cos); |
| 42 | * cos.close(); |
| 43 | * </pre> |
| 44 | * |
| 45 | * Example (Compressing a file): |
| 46 | * <pre> |
| 47 | * final InputStream is = new FileInputStream(input); |
| 48 | * CompressorInputStream in = |
| 49 | * new CompressorStreamFactory().createCompressorInputStream("bzip2", is); |
| 50 | * IOUtils.copy(in, new FileOutputStream(output)); |
| 51 | * in.close(); |
| 52 | * </pre> |
| 53 | * |
Sebastian Bazley | 44dbd93 | 2009-03-28 00:03:11 +0000 | [diff] [blame] | 54 | * @Immutable |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 55 | */ |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 56 | public class CompressorStreamFactory { |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 57 | |
Stefan Bodewig | bed564b | 2010-02-18 12:34:02 +0000 | [diff] [blame^] | 58 | private static final String BZ = "bzip2"; |
| 59 | private static final String GZ = "gz"; |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Create an compressor input stream from an input stream, autodetecting |
| 63 | * the compressor type from the first few bytes of the stream. The InputStream |
| 64 | * must support marks, like BufferedInputStream. |
| 65 | * |
| 66 | * @param in the input stream |
| 67 | * @return the compressor input stream |
| 68 | * @throws CompressorInputStream if the compressor name is not known |
| 69 | * @throws IllegalArgumentException if the stream is null or does not support mark |
| 70 | */ |
| 71 | public CompressorInputStream createCompressorInputStream(final InputStream in) |
| 72 | throws CompressorException { |
| 73 | if (in == null) { |
| 74 | throw new IllegalArgumentException("Stream must not be null."); |
| 75 | } |
| 76 | |
| 77 | if (!in.markSupported()) { |
| 78 | throw new IllegalArgumentException("Mark is not supported."); |
| 79 | } |
| 80 | |
| 81 | final byte[] signature = new byte[12]; |
| 82 | in.mark(signature.length); |
| 83 | try { |
| 84 | int signatureLength = in.read(signature); |
| 85 | in.reset(); |
| 86 | |
| 87 | if (BZip2CompressorInputStream.matches(signature, signatureLength)) { |
| 88 | return new BZip2CompressorInputStream(in); |
| 89 | } |
| 90 | |
| 91 | if (GzipCompressorInputStream.matches(signature, signatureLength)) { |
| 92 | return new GzipCompressorInputStream(in); |
| 93 | } |
| 94 | |
| 95 | } catch (IOException e) { |
| 96 | throw new CompressorException("Failed to detect Compressor from InputStream.", e); |
| 97 | } |
| 98 | |
| 99 | throw new CompressorException("No Compressor found for the stream signature."); |
| 100 | } |
| 101 | |
Sebastian Bazley | dc281aa | 2009-03-16 14:42:56 +0000 | [diff] [blame] | 102 | /** |
| 103 | * Create a compressor input stream from a compressor name and an input stream. |
| 104 | * |
| 105 | * @param name of the compressor, i.e. "gz" or "bzip2" |
| 106 | * @param in the input stream |
| 107 | * @return compressor input stream |
| 108 | * @throws CompressorException if the compressor name is not known |
| 109 | * @throws IllegalArgumentException if the name or input stream is null |
| 110 | */ |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 111 | public CompressorInputStream createCompressorInputStream(final String name, |
| 112 | final InputStream in) throws CompressorException { |
| 113 | if (name == null || in == null) { |
| 114 | throw new IllegalArgumentException( |
| 115 | "Compressor name and stream must not be null."); |
| 116 | } |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 117 | |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 118 | try { |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 119 | |
Stefan Bodewig | bed564b | 2010-02-18 12:34:02 +0000 | [diff] [blame^] | 120 | if (GZ.equalsIgnoreCase(name)) { |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 121 | return new GzipCompressorInputStream(in); |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Stefan Bodewig | bed564b | 2010-02-18 12:34:02 +0000 | [diff] [blame^] | 124 | if (BZ.equalsIgnoreCase(name)) { |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 125 | return new BZip2CompressorInputStream(in); |
| 126 | } |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 127 | |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 128 | } catch (IOException e) { |
| 129 | throw new CompressorException( |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 130 | "Could not create CompressorInputStream.", e); |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 131 | } |
Christian Grobmeier | c75d2d3 | 2009-04-14 04:46:49 +0000 | [diff] [blame] | 132 | throw new CompressorException("Compressor: " + name + " not found."); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Christian Grobmeier | c75d2d3 | 2009-04-14 04:46:49 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Create an compressor output stream from an compressor name and an input stream. |
| 137 | * |
| 138 | * @param name the compressor name, i.e. "gz" or "bzip2" |
| 139 | * @param out the output stream |
| 140 | * @return the compressor output stream |
| 141 | * @throws CompressorException if the archiver name is not known |
| 142 | * @throws IllegalArgumentException if the archiver name or stream is null |
| 143 | */ |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 144 | public CompressorOutputStream createCompressorOutputStream( |
| 145 | final String name, final OutputStream out) |
| 146 | throws CompressorException { |
| 147 | if (name == null || out == null) { |
| 148 | throw new IllegalArgumentException( |
| 149 | "Compressor name and stream must not be null."); |
| 150 | } |
Torsten Curdt | 6a70323 | 2009-01-07 13:16:01 +0000 | [diff] [blame] | 151 | |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 152 | try { |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 153 | |
Stefan Bodewig | bed564b | 2010-02-18 12:34:02 +0000 | [diff] [blame^] | 154 | if (GZ.equalsIgnoreCase(name)) { |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 155 | return new GzipCompressorOutputStream(out); |
Stefan Bodewig | ae0f984 | 2010-02-12 15:22:02 +0000 | [diff] [blame] | 156 | } |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 157 | |
Stefan Bodewig | bed564b | 2010-02-18 12:34:02 +0000 | [diff] [blame^] | 158 | if (BZ.equalsIgnoreCase(name)) { |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 159 | return new BZip2CompressorOutputStream(out); |
| 160 | } |
Torsten Curdt | 2e3acfa | 2010-02-12 14:14:31 +0000 | [diff] [blame] | 161 | |
Stefan Bodewig | c4a65f0 | 2009-03-13 05:23:22 +0000 | [diff] [blame] | 162 | } catch (IOException e) { |
| 163 | throw new CompressorException( |
| 164 | "Could not create CompressorOutputStream", e); |
| 165 | } |
Christian Grobmeier | c75d2d3 | 2009-04-14 04:46:49 +0000 | [diff] [blame] | 166 | throw new CompressorException("Compressor: " + name + " not found."); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 167 | } |
| 168 | } |