blob: 6c9c6462ad28afcb184859b4778d781b9b4f8fb0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 *
28 * package private helper class which provides decoder (native->ucs)
29 * mapping capability for the benefit of compound encoders/decoders
30 * whose individual component submappings do not need an association with
31 * an enclosing charset
32 *
33 */
34
35package sun.nio.cs.ext;
36
37public class DBCSDecoderMapping {
38
39 /* 1rst level index */
40 private short[] index1;
41 /*
42 * 2nd level index, provided by subclass
43 * every string has 0x10*(end-start+1) characters.
44 */
45 private String[] index2;
46
47 protected int start;
48 protected int end;
49
50 protected static final char REPLACE_CHAR='\uFFFD';
51
52 public DBCSDecoderMapping(short[] index1, String[] index2,
53 int start, int end) {
54 this.index1 = index1;
55 this.index2 = index2;
56 this.start = start;
57 this.end = end;
58 }
59
60 /*
61 * Can be changed by subclass
62 */
63 protected char decodeSingle(int b) {
64 if (b >= 0)
65 return (char) b;
66 return REPLACE_CHAR;
67 }
68
69 protected char decodeDouble(int byte1, int byte2) {
70 if (((byte1 < 0) || (byte1 > index1.length))
71 || ((byte2 < start) || (byte2 > end)))
72 return REPLACE_CHAR;
73
74 int n = (index1[byte1] & 0xf) * (end - start + 1) + (byte2 - start);
75 return index2[index1[byte1] >> 4].charAt(n);
76 }
77}