KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > archive > tar > TarBZip2Driver


1 /*
2  * TarBZip2Driver.java
3  *
4  * Created on 24. Dezember 2005, 00:01
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io.archive.tar;
23
24 import de.schlichtherle.io.archive.*;
25 import de.schlichtherle.io.archive.spi.*;
26 import de.schlichtherle.io.rof.*;
27
28 import java.io.*;
29
30 import org.apache.tools.bzip2.*;
31
32 /**
33  * An archive driver which builds TAR archives compressed with BZIP2.
34  * <p>
35  * <b>Warning:</b> This driver is still experimental:
36  * Ant's underlying BZIP2 implementation is very exhaustive on memory and
37  * slow (I'm not sure whether this is an implementation issue or a general
38  * issue with the BZIP2 specification).
39  * Do not use this driver in productive environments for other than small
40  * archives!
41  * If you need to use it nevertheless, use Ant 1.7.0 RC1 or later.
42  * The BZIP2 implementation in this version has been much improved and is
43  * required to pass TrueZIP's comprehensive unit tests for the File class
44  * with this driver.
45  * <p>
46  * Instances of this class are immutable.
47  *
48  * @author Christian Schlichtherle
49  * @version @version@
50  * @since TrueZIP 6.0
51  */

52 public class TarBZip2Driver extends TarDriver {
53
54     private static final int BUFSIZE = 4096;
55
56     /**
57      * The default block size to use if no block size is provided to the
58      * constructor.
59      * This is 9, which is equivalent to
60      * <code>CBZip2OutputStream.MAX_BLOCKSIZE</code> in ant 1.7.0 and later.
61      */

62     public static final int BLOCKSIZE = 9; //CBZip2OutputStream.MAX_BLOCKSIZE;
63

64     private final int blockSize;
65
66     /**
67      * Equivalent to {@link TarDriver#TarDriver(String, Icon, Icon)
68      * super(ENCODING, null, null)}.
69      * These parameters are based on heuristics.
70      */

71     public TarBZip2Driver() {
72         this(ENCODING, BLOCKSIZE);
73     }
74
75     /**
76      * Equivalent to {@link TarDriver#TarDriver(String, Icon, Icon)
77      * super(encoding, null, null)}.
78      * These parameters are based on heuristics.
79      * <p>
80      * Warning: The encoding parameter is currently not yet supported by this
81      * driver!
82      */

83     public TarBZip2Driver(String JavaDoc encoding) {
84         this(encoding, BLOCKSIZE);
85     }
86
87     public TarBZip2Driver(int blockSize) {
88         this(ENCODING, blockSize);
89     }
90
91     /**
92      * Equivalent to {@link TarDriver#TarDriver(String, Icon, Icon)
93      * super(encoding, null, null)}.
94      * These parameters are based on heuristics.
95      * <p>
96      * Warning: The encoding parameter is currently not yet supported by this
97      * driver!
98      */

99     public TarBZip2Driver(final String JavaDoc encoding, final int blockSize) {
100         super(encoding, null, null);
101         this.blockSize = blockSize;
102     }
103
104     //
105
// Driver implementation:
106
//
107

108     /**
109      * Returns a newly created and verified {@link CBZip2InputStream}.
110      * This method performs a simple verification by computing the checksum
111      * for the first record only.
112      * This method is required because the <code>CBZip2InputStream</code>
113      * unfortunately does not do sufficient verification!
114      */

115     protected InputStream createInputStream(ReadOnlyFile rof)
116     throws IOException {
117         final InputStream in = super.createInputStream(rof);
118         // Consume and check the first two magic bytes. This is required for
119
// the CBZip2InputStream class. Bad design, I think...
120
if (in.read() != 'B' || in.read() != 'Z')
121             throw new IOException("Not a BZIP2 compressed input stream!");
122         final byte[] magic = new byte[2];
123         final InputStream vin = TarInputArchive.readAhead(in, magic);
124         if (magic[0] != 'h' || magic[1] < '1' || '9' < magic[1])
125             throw new IOException("Not a BZIP2 compressed input stream!");
126         return new CBZip2InputStream(new BufferedInputStream(vin, BUFSIZE));
127     }
128
129     public OutputArchive createOutputArchive(
130             final Archive archive,
131             final OutputStream out,
132             final InputArchive source)
133     throws IOException {
134         // Produce the first two magic bytes. This is required for the
135
// CBZip2OutputStream class. Bad design, I think...
136
out.write(new byte[] { 'B', 'Z' });
137         return super.createOutputArchive(archive,
138                 new CBZip2OutputStream(
139                     new BufferedOutputStream(out, BUFSIZE),
140                     blockSize),
141                 source);
142     }
143 }
144
Popular Tags