2 * Copyright (C) 2007 Esmertec AG.
3 * Copyright (C) 2007 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package com.google.android.mms.pdu;
20 import java.io.UnsupportedEncodingException;
21 import java.util.HashMap;
23 public class CharacterSets {
25 * IANA assigned MIB enum numbers.
27 * From wap-230-wsp-20010705-a.pdf
28 * Any-charset = <Octet 128>
29 * Equivalent to the special RFC2616 charset value "*"
31 public static final int ANY_CHARSET = 0x00;
32 public static final int US_ASCII = 0x03;
33 public static final int ISO_8859_1 = 0x04;
34 public static final int ISO_8859_2 = 0x05;
35 public static final int ISO_8859_3 = 0x06;
36 public static final int ISO_8859_4 = 0x07;
37 public static final int ISO_8859_5 = 0x08;
38 public static final int ISO_8859_6 = 0x09;
39 public static final int ISO_8859_7 = 0x0A;
40 public static final int ISO_8859_8 = 0x0B;
41 public static final int ISO_8859_9 = 0x0C;
42 public static final int SHIFT_JIS = 0x11;
43 public static final int UTF_8 = 0x6A;
44 public static final int BIG5 = 0x07EA;
45 public static final int UCS2 = 0x03E8;
46 public static final int UTF_16 = 0x03F7;
49 * If the encoding of given data is unsupported, use UTF_8 to decode it.
51 public static final int DEFAULT_CHARSET = UTF_8;
54 * Array of MIB enum numbers.
56 private static final int[] MIBENUM_NUMBERS = {
76 * The Well-known-charset Mime name.
78 public static final String MIMENAME_ANY_CHARSET = "*";
79 public static final String MIMENAME_US_ASCII = "us-ascii";
80 public static final String MIMENAME_ISO_8859_1 = "iso-8859-1";
81 public static final String MIMENAME_ISO_8859_2 = "iso-8859-2";
82 public static final String MIMENAME_ISO_8859_3 = "iso-8859-3";
83 public static final String MIMENAME_ISO_8859_4 = "iso-8859-4";
84 public static final String MIMENAME_ISO_8859_5 = "iso-8859-5";
85 public static final String MIMENAME_ISO_8859_6 = "iso-8859-6";
86 public static final String MIMENAME_ISO_8859_7 = "iso-8859-7";
87 public static final String MIMENAME_ISO_8859_8 = "iso-8859-8";
88 public static final String MIMENAME_ISO_8859_9 = "iso-8859-9";
89 public static final String MIMENAME_SHIFT_JIS = "shift_JIS";
90 public static final String MIMENAME_UTF_8 = "utf-8";
91 public static final String MIMENAME_BIG5 = "big5";
92 public static final String MIMENAME_UCS2 = "iso-10646-ucs-2";
93 public static final String MIMENAME_UTF_16 = "utf-16";
95 public static final String DEFAULT_CHARSET_NAME = MIMENAME_UTF_8;
98 * Array of the names of character sets.
100 private static final String[] MIME_NAMES = {
101 MIMENAME_ANY_CHARSET,
119 private static final HashMap<Integer, String> MIBENUM_TO_NAME_MAP;
120 private static final HashMap<String, Integer> NAME_TO_MIBENUM_MAP;
123 // Create the HashMaps.
124 MIBENUM_TO_NAME_MAP = new HashMap<Integer, String>();
125 NAME_TO_MIBENUM_MAP = new HashMap<String, Integer>();
126 assert(MIBENUM_NUMBERS.length == MIME_NAMES.length);
127 int count = MIBENUM_NUMBERS.length - 1;
128 for(int i = 0; i <= count; i++) {
129 MIBENUM_TO_NAME_MAP.put(MIBENUM_NUMBERS[i], MIME_NAMES[i]);
130 NAME_TO_MIBENUM_MAP.put(MIME_NAMES[i], MIBENUM_NUMBERS[i]);
134 private CharacterSets() {} // Non-instantiatable
137 * Map an MIBEnum number to the name of the charset which this number
138 * is assigned to by IANA.
140 * @param mibEnumValue An IANA assigned MIBEnum number.
141 * @return The name string of the charset.
142 * @throws UnsupportedEncodingException
144 public static String getMimeName(int mibEnumValue)
145 throws UnsupportedEncodingException {
146 String name = MIBENUM_TO_NAME_MAP.get(mibEnumValue);
148 throw new UnsupportedEncodingException();
154 * Map a well-known charset name to its assigned MIBEnum number.
156 * @param mimeName The charset name.
157 * @return The MIBEnum number assigned by IANA for this charset.
158 * @throws UnsupportedEncodingException
160 public static int getMibEnumValue(String mimeName)
161 throws UnsupportedEncodingException {
162 if(null == mimeName) {
166 Integer mibEnumValue = NAME_TO_MIBENUM_MAP.get(mimeName);
167 if (mibEnumValue == null) {
168 throw new UnsupportedEncodingException();