1 /* ====================================================================
\r
2 * Copyright (c) 2006 J.T. Beetstra
\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
12 * The above copyright notice and this permission notice shall be
\r
13 * included in all copies or substantial portions of the Software.
\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
25 package com.beetstra.jutf7;
\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
35 * Abstract base class for UTF-7 style encoding and decoding.
\r
38 * @author Jaap Beetstra
\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
44 final boolean strict;
\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
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
63 protected UTF7StyleCharset(String canonicalName, String[] aliases, String alphabet,
\r
65 super(canonicalName, aliases);
\r
66 this.base64 = new Base64Util(alphabet);
\r
67 this.strict = strict;
\r
72 * @see java.nio.charset.Charset#contains(java.nio.charset.Charset)
\r
74 public boolean contains(final Charset cs) {
\r
75 return CONTAINED.contains(cs.name());
\r
80 * @see java.nio.charset.Charset#newDecoder()
\r
82 public CharsetDecoder newDecoder() {
\r
83 return new UTF7StyleCharsetDecoder(this, base64, strict);
\r
88 * @see java.nio.charset.Charset#newEncoder()
\r
90 public CharsetEncoder newEncoder() {
\r
91 return new UTF7StyleCharsetEncoder(this, base64, strict);
\r
95 * Tells if a character can be encoded using simple (US-ASCII) encoding or
\r
96 * requires base 64 encoding.
\r
98 * @param ch The character
\r
99 * @return True if the character can be encoded directly, false otherwise
\r
101 abstract boolean canEncodeDirectly(char ch);
\r
104 * Returns character used to switch to base 64 encoding.
\r
106 * @return The shift character
\r
108 abstract byte shift();
\r
111 * Returns character used to switch from base 64 encoding to simple
\r
114 * @return The unshift character
\r
116 abstract byte unshift();
\r