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
10
* http://www.apache.org/licenses/LICENSE-2.0
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
19
package org.apache.commons.compress.compressors.bzip2;
21
import java.io.FileInputStream;
22
import java.io.FileNotFoundException;
23
import java.io.FileOutputStream;
24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
28
import org.apache.commons.compress.AbstractCompressor;
29
import org.apache.commons.compress.CompressException;
30
import org.apache.commons.compress.CompressUtils;
32
* Implementation of the Compressor Interface for BZip2.
34
* @author christian.grobmeier
36
public class BZip2Compressor extends AbstractCompressor {
37
/* Header BZ as byte-Array */
38
private static final byte[] HEADER = new byte[]{(byte)'B', (byte)'Z'};
39
/* Name of this implementation */
40
private static final String NAME = "bz2";
41
/* Default file extension*/
42
private static String DEFAULT_FILE_EXTENSION = "bz2";
47
public BZip2Compressor() {
52
* @see org.apache.commons.compress.Compressor#compress(java.io.FileInputStream, java.io.FileOutputStream)
54
public void compressTo(FileInputStream inputStream, FileOutputStream outputStream) throws CompressException {
55
BZip2OutputStream outputBZStream = null;
57
outputBZStream = getPackedOutput( outputStream );
58
CompressUtils.copy( inputStream, outputBZStream );
59
} catch (FileNotFoundException e) {
60
throw new CompressException("File could not be found", e);
61
} catch (IOException e) {
62
throw new CompressException("An IO Exception occured", e);
65
outputBZStream.close();
66
} catch (IOException e1) {
67
throw new CompressException("An IO Exception occured while closing the streams", e1);
73
* This decompress method uses a special InputStream Class for BZ2
74
* @see org.apache.commons.compress.Compressor#decompress(java.io.FileInputStream, java.io.FileOutputStream)
76
public void decompressTo(FileInputStream input, FileOutputStream outputStream)
77
throws CompressException {
78
BZip2InputStream inputStream = null;
80
inputStream = getPackedInput( input );
81
CompressUtils.copy( inputStream, outputStream );
82
} catch (IOException e) {
83
throw new CompressException("An I/O Exception has occured", e);
88
* Skips the 'BZ' header bytes. required by the BZip2InputStream class.
89
* @param input input stream
90
* @return {@link BZip2InputStream} instance
91
* @throws IOException if an IO error occurs
93
private BZip2InputStream getPackedInput( final InputStream input )
95
// skips the 'BZ' header bytes required by the BZip2InputStream class
98
return new BZip2InputStream( input );
102
* Writes a 'BZ' header to the output stream, and creates a
103
* BZip2OutputStream object ready for use, as required by the
104
* BZip2OutputStream class.
106
* @param output {@link Output} stream to add a header to
107
* @return {@link BZip2OutputStream} ready to write to
108
* @throws IOException if an IO error occurs
110
private BZip2OutputStream getPackedOutput( final OutputStream output )
112
output.write( HEADER );
113
return new BZip2OutputStream( output );
117
* @see org.apache.commons.compress.Compressor#getHeader()
119
public byte[] getHeader() {
124
* @see org.apache.commons.compress.Compressor#getName()
126
public String getName() {
131
* @see org.apache.commons.compress.AbstractCompressor#getDefaultFileExtension()
133
public String getDefaultFileExtension() {
134
return DEFAULT_FILE_EXTENSION;