KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > archive > spi > AbstractArchiveDriver


1 /*
2  * AbstractArchiveDriver.java
3  *
4  * Created on 3. April 2006, 19:04
5  */

6
7 package de.schlichtherle.io.archive.spi;
8
9 import de.schlichtherle.io.archive.Archive;
10 import java.io.CharConversionException JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
14 import java.nio.charset.Charset JavaDoc;
15 import java.nio.charset.CharsetEncoder JavaDoc;
16 import java.nio.charset.UnsupportedCharsetException JavaDoc;
17 import javax.swing.Icon JavaDoc;
18
19 /**
20  * An abstract {@link ArchiveDriver} which provides default implementations
21  * for character set encodings and icon handling.
22  * <p>
23  * Since TrueZIP 6.4, this class is serializable in order to meet the
24  * requirements of the {@link de.schlichtherle.io.File} class.
25  *
26  * @author Christian Schlichtherle
27  * @version @version@
28  * @since TrueZIP 6.0
29  */

30 public abstract class AbstractArchiveDriver
31         implements ArchiveDriver, Serializable JavaDoc {
32
33     private final String JavaDoc encoding;
34
35     /**
36      * Returns a {@link CharsetEncoder} suitable for {@link #encoding} when
37      * its {@link ThreadLocal#get} method is called.
38      * This method may throw a number of <code>RuntimeException</code>s
39      * if the encoding is not properly supported by this JVM.
40      */

41     private final transient ThreadLocalEncoder tlEncoder // never transmit this over the wire!
42
= new ThreadLocalEncoder();
43
44     private final Icon JavaDoc openIcon;
45
46     private final Icon JavaDoc closedIcon;
47
48     /**
49      * Creates a new instance of AbstractArchiveDriver.
50      * This constructor may throw a number of <code>RuntimeException</code>s
51      * if the encoding is not properly supported by this JVM.
52      */

53     protected AbstractArchiveDriver(
54             String JavaDoc encoding,
55             Icon JavaDoc openIcon,
56             Icon JavaDoc closedIcon) {
57         this.encoding = encoding;
58         this.openIcon = openIcon;
59         this.closedIcon = closedIcon;
60
61         // Perform fail fast tests for character set encodings using both
62
// JSE 1.1 API and the NIO API.
63
// On some JRE/Locale combinations, a test may fail for an API while
64
// it passes for the other API.
65
// This is considered to be a bug in the JSE implementation.
66

67         // Test charset availability via JSE 1.1 API.
68
// This API may be used by any implementation method.
69
try {
70             new String JavaDoc(new byte[0], encoding);
71         } catch (UnsupportedEncodingException JavaDoc uee) {
72             UnsupportedCharsetException JavaDoc uce
73                     = new UnsupportedCharsetException JavaDoc(encoding);
74             uce.initCause(uee);
75             throw uce;
76         }
77
78         // Test charset availability via NIO API.
79
// This API is used in ensureEncodable(), which is supposed to be used
80
// by the createArchiveEntry implementation, but may also be used by
81
// any other implementation method.
82
tlEncoder.get();
83     }
84
85     /**
86      * Returns the encoding for the archives read or written by this driver.
87      *
88      * @return The character set encoding to use for entry names and probably
89      * other meta data in any archive file read or written by this
90      * driver.
91      */

92     public final String JavaDoc getEncoding() {
93         return encoding;
94     }
95
96     /**
97      * Ensures that the given entry name is representable in this driver's
98      * character set encoding.
99      * Should be called by sub classes in their implementation of the method
100      * {@link ArchiveDriver#createArchiveEntry}.
101      *
102      * @see #getEncoding
103      *
104      * @throws UndeclaredThrowableException If the character set encoding is
105      * unsupported.
106      */

107     protected final void ensureEncodable(final String JavaDoc entryName)
108     throws CharConversionException JavaDoc {
109         if (!tlEncoder.canEncode(entryName))
110             throw new CharConversionException JavaDoc(entryName +
111                     ": Illegal characters in entry name!");
112     }
113     
114     public Icon JavaDoc getOpenIcon(Archive archive) {
115         return openIcon;
116     }
117     
118     public Icon JavaDoc getClosedIcon(Archive archive) {
119         return closedIcon;
120     }
121
122     private final class ThreadLocalEncoder extends ThreadLocal JavaDoc {
123         protected Object JavaDoc initialValue() {
124             return Charset.forName(getEncoding()).newEncoder();
125         }
126
127         /*public CharsetEncoder getEncoder() {
128             return (CharsetEncoder) get();
129         }*/

130
131         public boolean canEncode(CharSequence JavaDoc cs) {
132             return ((CharsetEncoder JavaDoc) get()).canEncode(cs);
133         }
134     }
135 }
136
Popular Tags