~ubuntu-branches/ubuntu/maverick/libcommons-compress-java/maverick

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/compress/compressors/bzip2/BZip2Compressor.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-12-17 15:17:09 UTC
  • Revision ID: james.westby@ubuntu.com-20071217151709-dqfo6iq1zxyssgf6
Tags: upstream-0~svn604876
ImportĀ upstreamĀ versionĀ 0~svn604876

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
19
package org.apache.commons.compress.compressors.bzip2;
 
20
 
 
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;
 
27
 
 
28
import org.apache.commons.compress.AbstractCompressor;
 
29
import org.apache.commons.compress.CompressException;
 
30
import org.apache.commons.compress.CompressUtils;
 
31
/**
 
32
 * Implementation of the Compressor Interface for BZip2. 
 
33
 * 
 
34
 * @author christian.grobmeier
 
35
 */
 
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";
 
43
        
 
44
        /**
 
45
         * Constructor. 
 
46
         */
 
47
        public BZip2Compressor() {
 
48
                super();
 
49
        }
 
50
 
 
51
        /* (non-Javadoc)
 
52
         * @see org.apache.commons.compress.Compressor#compress(java.io.FileInputStream, java.io.FileOutputStream)
 
53
         */
 
54
        public void compressTo(FileInputStream inputStream, FileOutputStream outputStream) throws CompressException {
 
55
                BZip2OutputStream outputBZStream = null;
 
56
                try {
 
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);
 
63
                } finally {
 
64
                        try {
 
65
                                outputBZStream.close();
 
66
                        } catch (IOException e1) {
 
67
                                throw new CompressException("An IO Exception occured while closing the streams", e1);
 
68
                        }
 
69
                }
 
70
        }
 
71
        
 
72
        /* 
 
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)
 
75
         */
 
76
        public void decompressTo(FileInputStream input, FileOutputStream outputStream) 
 
77
                throws CompressException {
 
78
                BZip2InputStream inputStream = null;
 
79
                try {
 
80
                        inputStream = getPackedInput( input );
 
81
                        CompressUtils.copy( inputStream, outputStream );
 
82
                } catch (IOException e) {
 
83
                        throw new CompressException("An I/O Exception has occured", e);
 
84
                }
 
85
        }
 
86
        
 
87
        /**
 
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
 
92
         */
 
93
        private BZip2InputStream getPackedInput( final InputStream input )
 
94
                throws IOException {
 
95
                // skips the 'BZ' header bytes required by the BZip2InputStream class
 
96
                input.read();
 
97
                input.read();
 
98
                return new BZip2InputStream( input );
 
99
        }
 
100
        
 
101
        /**
 
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.
 
105
         * 
 
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
 
109
         */
 
110
        private BZip2OutputStream getPackedOutput( final OutputStream output )
 
111
                throws IOException {
 
112
                output.write( HEADER );
 
113
                return new BZip2OutputStream( output );
 
114
        }
 
115
        
 
116
        /* (non-Javadoc)
 
117
         * @see org.apache.commons.compress.Compressor#getHeader()
 
118
         */
 
119
        public byte[] getHeader() {
 
120
                return HEADER;
 
121
        }
 
122
 
 
123
        /* (non-Javadoc)
 
124
         * @see org.apache.commons.compress.Compressor#getName()
 
125
         */
 
126
        public String getName() {
 
127
                return NAME;
 
128
        }
 
129
        
 
130
        /* (non-Javadoc)
 
131
         * @see org.apache.commons.compress.AbstractCompressor#getDefaultFileExtension()
 
132
         */
 
133
        public String getDefaultFileExtension() {
 
134
                return DEFAULT_FILE_EXTENSION;
 
135
        }
 
136
}