|
| 1 | +/* |
| 2 | + * Copyright 2013 Deutsche Nationalbibliothek |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.util; |
| 17 | + |
| 18 | +import java.io.BufferedInputStream; |
| 19 | +import java.io.BufferedOutputStream; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | + |
| 23 | +import org.apache.commons.compress.compressors.CompressorException; |
| 24 | +import org.apache.commons.compress.compressors.CompressorStreamFactory; |
| 25 | +import org.apache.commons.io.FilenameUtils; |
| 26 | +import org.apache.commons.io.input.ProxyInputStream; |
| 27 | +import org.apache.commons.io.output.ProxyOutputStream; |
| 28 | +import org.culturegraph.mf.exceptions.MetafactureException; |
| 29 | + |
| 30 | +/** |
| 31 | + * Provides a convenient interface for using stream compressors |
| 32 | + * and decompressors. |
| 33 | + * |
| 34 | + * @author Christoph Böhme |
| 35 | + * |
| 36 | + */ |
| 37 | +public enum FileCompression { |
| 38 | + |
| 39 | + NONE { |
| 40 | + @Override |
| 41 | + public OutputStream createCompressor(final OutputStream writeTo, final String fileName) { |
| 42 | + return new ProxyOutputStream(writeTo); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public InputStream createDecompressor(final InputStream readFrom) { |
| 47 | + return new ProxyInputStream(readFrom) { |
| 48 | + }; |
| 49 | + } |
| 50 | + }, |
| 51 | + |
| 52 | + AUTO { |
| 53 | + @Override |
| 54 | + public OutputStream createCompressor(final OutputStream writeTo, final String fileName) { |
| 55 | + if (fileName == null) { |
| 56 | + throw new IllegalArgumentException("fileName is required for auto-selecting compressor"); |
| 57 | + } |
| 58 | + |
| 59 | + final String extension = FilenameUtils.getExtension(fileName); |
| 60 | + final FileCompression compressor; |
| 61 | + if ("gz".equalsIgnoreCase(extension)) { |
| 62 | + compressor = GZIP; |
| 63 | + } else if ("gzip".equalsIgnoreCase(extension)) { |
| 64 | + compressor = GZIP; |
| 65 | + } else if ("bz2".equalsIgnoreCase(extension)) { |
| 66 | + compressor = BZIP2; |
| 67 | + } else if ("bzip2".equalsIgnoreCase(extension)) { |
| 68 | + compressor = BZIP2; |
| 69 | + } else if ("xz".equalsIgnoreCase(extension)) { |
| 70 | + compressor = XZ; |
| 71 | + } else { |
| 72 | + compressor = NONE; |
| 73 | + } |
| 74 | + |
| 75 | + return compressor.createCompressor(writeTo, fileName); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public InputStream createDecompressor(final InputStream readFrom) { |
| 80 | + final InputStream bufferedStream = bufferStream(readFrom); |
| 81 | + try { |
| 82 | + return APACHE_COMPRESSOR_FACTORY.createCompressorInputStream(bufferedStream); |
| 83 | + } catch (CompressorException e) { |
| 84 | + return NONE.createDecompressor(bufferedStream); |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + |
| 89 | + BZIP2 { |
| 90 | + @Override |
| 91 | + public OutputStream createCompressor(final OutputStream writeTo, final String fileName) { |
| 92 | + try { |
| 93 | + return APACHE_COMPRESSOR_FACTORY.createCompressorOutputStream( |
| 94 | + CompressorStreamFactory.BZIP2, bufferStream(writeTo)); |
| 95 | + } catch (CompressorException e) { |
| 96 | + throw new MetafactureException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public InputStream createDecompressor(final InputStream readFrom) { |
| 102 | + try { |
| 103 | + return APACHE_COMPRESSOR_FACTORY.createCompressorInputStream( |
| 104 | + CompressorStreamFactory.BZIP2, bufferStream(readFrom)); |
| 105 | + } catch (CompressorException e) { |
| 106 | + throw new MetafactureException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + |
| 111 | + GZIP { |
| 112 | + @Override |
| 113 | + public OutputStream createCompressor(final OutputStream writeTo, final String fileName) { |
| 114 | + try { |
| 115 | + return APACHE_COMPRESSOR_FACTORY.createCompressorOutputStream( |
| 116 | + CompressorStreamFactory.GZIP, bufferStream(writeTo)); |
| 117 | + } catch (CompressorException e) { |
| 118 | + throw new MetafactureException(e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public InputStream createDecompressor(final InputStream readFrom) { |
| 124 | + try { |
| 125 | + return APACHE_COMPRESSOR_FACTORY.createCompressorInputStream( |
| 126 | + CompressorStreamFactory.GZIP, bufferStream(readFrom)); |
| 127 | + } catch (CompressorException e) { |
| 128 | + throw new MetafactureException(e); |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | + |
| 133 | + PACK200 { |
| 134 | + @Override |
| 135 | + public OutputStream createCompressor(final OutputStream writeTo, final String fileName) { |
| 136 | + try { |
| 137 | + return APACHE_COMPRESSOR_FACTORY.createCompressorOutputStream( |
| 138 | + CompressorStreamFactory.PACK200, bufferStream(writeTo)); |
| 139 | + } catch (CompressorException e) { |
| 140 | + throw new MetafactureException(e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public InputStream createDecompressor(final InputStream readFrom) { |
| 146 | + try { |
| 147 | + return APACHE_COMPRESSOR_FACTORY.createCompressorInputStream( |
| 148 | + CompressorStreamFactory.PACK200, bufferStream(readFrom)); |
| 149 | + } catch (CompressorException e) { |
| 150 | + throw new MetafactureException(e); |
| 151 | + } |
| 152 | + } |
| 153 | + }, |
| 154 | + |
| 155 | + XZ { |
| 156 | + @Override |
| 157 | + public OutputStream createCompressor(final OutputStream writeTo, final String fileName) { |
| 158 | + try { |
| 159 | + return APACHE_COMPRESSOR_FACTORY.createCompressorOutputStream( |
| 160 | + CompressorStreamFactory.XZ, bufferStream(writeTo)); |
| 161 | + } catch (CompressorException e) { |
| 162 | + throw new MetafactureException(e); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public InputStream createDecompressor(final InputStream readFrom) { |
| 168 | + try { |
| 169 | + return APACHE_COMPRESSOR_FACTORY.createCompressorInputStream( |
| 170 | + CompressorStreamFactory.XZ, bufferStream(readFrom)); |
| 171 | + } catch (CompressorException e) { |
| 172 | + throw new MetafactureException(e); |
| 173 | + } |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + private static final CompressorStreamFactory APACHE_COMPRESSOR_FACTORY = new CompressorStreamFactory(); |
| 178 | + private static final int BUFFER_SIZE = 8 * 1024 * 1024; |
| 179 | + |
| 180 | + public abstract OutputStream createCompressor(final OutputStream writeTo, final String fileName); |
| 181 | + |
| 182 | + public abstract InputStream createDecompressor(final InputStream readFrom); |
| 183 | + |
| 184 | + private static OutputStream bufferStream(final OutputStream stream) { |
| 185 | + if (stream instanceof BufferedOutputStream) { |
| 186 | + return stream; |
| 187 | + } |
| 188 | + return new BufferedOutputStream(stream, BUFFER_SIZE); |
| 189 | + } |
| 190 | + |
| 191 | + private static InputStream bufferStream(final InputStream stream) { |
| 192 | + if (stream instanceof BufferedInputStream) { |
| 193 | + return stream; |
| 194 | + } |
| 195 | + return new BufferedInputStream(stream, BUFFER_SIZE); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments