OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / packages / apps / Email / src / com / beetstra / jutf7 / UTF7StyleCharset.java
1 /* ====================================================================\r
2  * Copyright (c) 2006 J.T. Beetstra\r
3  *\r
4  * Permission is hereby granted, free of charge, to any person obtaining \r
5  * a copy of this software and associated documentation files (the \r
6  * "Software"), to deal in the Software without restriction, including \r
7  * without limitation the rights to use, copy, modify, merge, publish, \r
8  * distribute, sublicense, and/or sell copies of the Software, and to \r
9  * permit persons to whom the Software is furnished to do so, subject to \r
10  * the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be \r
13  * included in all copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, \r
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF \r
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. \r
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY \r
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, \r
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE \r
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  * ====================================================================\r
23  */\r
24 \r
25 package com.beetstra.jutf7;\r
26 \r
27 import java.nio.charset.Charset;\r
28 import java.nio.charset.CharsetDecoder;\r
29 import java.nio.charset.CharsetEncoder;\r
30 import java.util.Arrays;\r
31 import java.util.List;\r
32 \r
33 /**\r
34  * <p>\r
35  * Abstract base class for UTF-7 style encoding and decoding.\r
36  * </p>\r
37  * \r
38  * @author Jaap Beetstra\r
39  */\r
40 abstract class UTF7StyleCharset extends Charset {\r
41     private static final List CONTAINED = Arrays.asList(new String[] {\r
42             "US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16", "UTF-16LE", "UTF-16BE"\r
43     });\r
44     final boolean strict;\r
45     Base64Util base64;\r
46 \r
47     /**\r
48      * <p>\r
49      * Besides the name and aliases, two additional parameters are required.\r
50      * First the base 64 alphabet used; in modified UTF-7 a slightly different\r
51      * alphabet is used. Additionally, it should be specified if encoders and\r
52      * decoders should be strict about the interpretation of malformed encoded\r
53      * sequences. This is used since modified UTF-7 specifically disallows some\r
54      * constructs which are allowed (or not specifically disallowed) in UTF-7\r
55      * (RFC 2152).\r
56      * </p>\r
57      * \r
58      * @param canonicalName The name as defined in java.nio.charset.Charset\r
59      * @param aliases The aliases as defined in java.nio.charset.Charset\r
60      * @param alphabet The base 64 alphabet used\r
61      * @param strict True if strict handling of sequences is requested\r
62      */\r
63     protected UTF7StyleCharset(String canonicalName, String[] aliases, String alphabet,\r
64             boolean strict) {\r
65         super(canonicalName, aliases);\r
66         this.base64 = new Base64Util(alphabet);\r
67         this.strict = strict;\r
68     }\r
69 \r
70     /*\r
71      * (non-Javadoc)\r
72      * @see java.nio.charset.Charset#contains(java.nio.charset.Charset)\r
73      */\r
74     public boolean contains(final Charset cs) {\r
75         return CONTAINED.contains(cs.name());\r
76     }\r
77 \r
78     /*\r
79      * (non-Javadoc)\r
80      * @see java.nio.charset.Charset#newDecoder()\r
81      */\r
82     public CharsetDecoder newDecoder() {\r
83         return new UTF7StyleCharsetDecoder(this, base64, strict);\r
84     }\r
85 \r
86     /*\r
87      * (non-Javadoc)\r
88      * @see java.nio.charset.Charset#newEncoder()\r
89      */\r
90     public CharsetEncoder newEncoder() {\r
91         return new UTF7StyleCharsetEncoder(this, base64, strict);\r
92     }\r
93 \r
94     /**\r
95      * Tells if a character can be encoded using simple (US-ASCII) encoding or\r
96      * requires base 64 encoding.\r
97      * \r
98      * @param ch The character\r
99      * @return True if the character can be encoded directly, false otherwise\r
100      */\r
101     abstract boolean canEncodeDirectly(char ch);\r
102 \r
103     /**\r
104      * Returns character used to switch to base 64 encoding.\r
105      * \r
106      * @return The shift character\r
107      */\r
108     abstract byte shift();\r
109 \r
110     /**\r
111      * Returns character used to switch from base 64 encoding to simple\r
112      * encoding.\r
113      * \r
114      * @return The unshift character\r
115      */\r
116     abstract byte unshift();\r
117 }\r