KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TarDriver.java
3  *
4  * Created on 28. Februar 2006, 17:48
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 import java.util.logging.*;
30
31 import javax.swing.*;
32
33 /**
34  * An archive driver which builds TAR archives.
35  * <p>
36  * Instances of this class are immutable.
37  *
38  * @author Christian Schlichtherle
39  * @version @version@
40  * @since TrueZIP 6.0
41  */

42 public class TarDriver extends AbstractArchiveDriver {
43
44     /**
45      * The character set encoding used by TAR archives, which is
46      * <code>US-ASCII</code>.
47      * TARs should actually be able to use the system's native character set
48      * encoding. However, the low level TAR code as of Ant 1.6.5 doesn't
49      * support that, hence this constraint.
50      */

51     public static final String JavaDoc ENCODING = "US-ASCII";
52
53     /**
54      * Equivalent to {@link #TarDriver(String, Icon, Icon)
55      * this(ENCODING, null, null)}.
56      * These parameters are based on heuristics.
57      */

58     public TarDriver() {
59         super(ENCODING, null, null);
60     }
61
62     /**
63      * Equivalent to {@link #TarDriver(String, Icon, Icon)
64      * this(encoding, null, null)}.
65      * These parameters are based on heuristics.
66      * <p>
67      * Warning: The encoding parameter is currently not yet supported by this
68      * driver!
69      */

70     public TarDriver(String JavaDoc encoding) {
71         super(encoding, null, null);
72     }
73
74     /**
75      * Constructs a new TAR driver with the given parameters.
76      * <p>
77      * Warning: The encoding parameter is currently not yet supported by this
78      * driver!
79      *
80      * @param encoding The character set encoding to use for entry names and
81      * comments in any archive file read or written by this driver.
82      * @param openIcon The icon which should be displayed if an archive of
83      * this type is in the "open" state in a
84      * {@link de.schlichtherle.io.swing.JFileChooser} or
85      * {@link de.schlichtherle.io.swing.JFileTree}.
86      * @param closedIcon The icon which should be displayed if an archive of
87      * this type is in the "closed" state in a
88      * {@link de.schlichtherle.io.swing.JFileChooser} or
89      * {@link de.schlichtherle.io.swing.JFileTree}.
90      *
91      * @see #TarDriver()
92      */

93     public TarDriver(
94             String JavaDoc encoding,
95             Icon openIcon,
96             Icon closedIcon) {
97         super(encoding, openIcon, closedIcon);
98     }
99
100     //
101
// Factory methods:
102
//
103

104     public ArchiveEntry createArchiveEntry(
105             final Archive archive,
106             final String JavaDoc entryName,
107             final ArchiveEntry blueprint)
108     throws CharConversionException {
109         ensureEncodable(entryName);
110
111         final TarEntry entry;
112         if (blueprint != null) {
113             if (blueprint instanceof TarEntry) {
114                 entry = new TarEntry(entryName, (TarEntry) blueprint);
115             } else {
116                 entry = new TarEntry(entryName);
117                 entry.setTime(blueprint.getTime());
118             }
119         } else {
120             entry = new TarEntry(entryName);
121         }
122
123         return entry;
124     }
125
126     public InputArchive createInputArchive(
127             final Archive archive,
128             final ReadOnlyFile rof)
129     throws IOException {
130         final InputStream in = createInputStream(rof);
131         try {
132             return createInputArchive(archive, in);
133         } finally {
134             in.close();
135         }
136     }
137
138     /**
139      * Returns a new <code>InputStream</code> to read the contents from the
140      * given <code>ReadOnlyFile</code> from.
141      * Override this method in order to decorate the stream returned by the
142      * implementation in this class in order to have the driver read the TAR
143      * file from wrapper file formats such as GZIP or BZIP2.
144      * <p>
145      * Note that the returned stream should support marking for best
146      * performance and will <em>always</em> be closed by
147      * {@link #createInputArchive(Archive, ReadOnlyFile)}
148      * (which is pretty early).
149      */

150     protected InputStream createInputStream(ReadOnlyFile rof)
151     throws IOException {
152         return new ReadOnlyFileInputStream(rof);
153     }
154
155     /**
156      * Returns a new <code>InputArchive</code> to read the contents from the
157      * given <code>InputStream</code>.
158      * The implementation in this class simply returns
159      * <code>new TarInputArchive(archive, in)</code>.
160      *
161      * @param archive The abstract archive representation which TrueZIP's
162      * internal <code>ArchiveController</code> is processing
163      * - never <code>null</code>.
164      * @deprecated Do <em>not</em> override this method - override
165      * {@link #createInputStream} instead.
166      */

167     protected InputArchive createInputArchive(Archive archive, InputStream in)
168     throws IOException {
169         return new TarInputArchive(archive, in);
170     }
171
172     public OutputArchive createOutputArchive(
173             final Archive archive,
174             final OutputStream out,
175             final InputArchive source)
176     throws IOException {
177         return new TarOutputArchive(out);
178     }
179 }
180
Popular Tags