1/* ====================================================================
2 * Copyright (c) 2006 J.T. Beetstra
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * ====================================================================
23 */
24
25package com.beetstra.jutf7;
26
27/**
28 * <p>
29 * The character set specified in RFC 2152. Two variants are supported using the
30 * encodeOptional constructor flag
31 * </p>
32 *
33 * @see <a href="http://tools.ietf.org/html/rfc2152">RFC 2152< /a>
34 * @author Jaap Beetstra
35 */
36class UTF7Charset extends UTF7StyleCharset {
37    private static final String BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38            + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/";
39    private static final String SET_D = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'(),-./:?";
40    private static final String SET_O = "!\"#$%&*;<=>@[]^_`{|}";
41    private static final String RULE_3 = " \t\r\n";
42    final String directlyEncoded;
43
44    UTF7Charset(String name, String[] aliases, boolean includeOptional) {
45        super(name, aliases, BASE64_ALPHABET, false);
46        if (includeOptional)
47            this.directlyEncoded = SET_D + SET_O + RULE_3;
48        else
49            this.directlyEncoded = SET_D + RULE_3;
50    }
51
52    /*
53     * (non-Javadoc)
54     * @see com.beetstra.jutf7.UTF7StyleCharset#canEncodeDirectly(char)
55     */
56    boolean canEncodeDirectly(char ch) {
57        return directlyEncoded.indexOf(ch) >= 0;
58    }
59
60    /*
61     * (non-Javadoc)
62     * @see com.beetstra.jutf7.UTF7StyleCharset#shift()
63     */
64    byte shift() {
65        return '+';
66    }
67
68    /*
69     * (non-Javadoc)
70     * @see com.beetstra.jutf7.UTF7StyleCharset#unshift()
71     */
72    byte unshift() {
73        return '-';
74    }
75}
76