5
|
1 /**
|
|
2 * $Id$
|
|
3 *
|
|
4 * Title: tn5250J
|
|
5 * Copyright: Copyright (c) 2001,2009
|
|
6 * Company:
|
|
7 * @author: master_jaf
|
|
8 *
|
|
9 * Description:
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2, or (at your option)
|
|
14 * any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this software; see the file COPYING. If not, write to
|
|
23 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
24 * Boston, MA 02111-1307 USA
|
|
25 *
|
|
26 */
|
|
27 package org.tn5250j.encoding.builtin;
|
|
28
|
|
29 import java.util.Arrays;
|
|
30
|
|
31 /**
|
|
32 * Adapter class for converters using 8bit codepages.
|
|
33 *
|
|
34 * @author master_jaf
|
|
35 */
|
|
36 public abstract class CodepageConverterAdapter implements ICodepageConverter {
|
|
37
|
|
38 private char[] codepage = null;
|
|
39 private int[] reverse_codepage = null;
|
|
40
|
|
41 /* (non-Javadoc)
|
|
42 * @see org.tn5250j.cp.ICodepageConverter#init()
|
|
43 */
|
|
44 public ICodepageConverter init() {
|
|
45 codepage = getCodePage();
|
|
46
|
|
47 int size = 0;
|
|
48 for (int i=0; i<codepage.length; i++) {
|
|
49 size = Math.max(size, codepage[i]);
|
|
50 }
|
|
51 assert (size + 1) < 1024*1024; // some kind of maximum size limiter.
|
|
52 reverse_codepage = new int[size+1];
|
|
53 Arrays.fill(reverse_codepage, '?');
|
|
54 for (int i=0; i<codepage.length; i++) {
|
|
55 reverse_codepage[codepage[i]] = i;
|
|
56 }
|
|
57 return this;
|
|
58 }
|
|
59
|
|
60 /* (non-Javadoc)
|
|
61 * @see org.tn5250j.cp.ICodepageConverter#uni2ebcdic(char)
|
|
62 */
|
|
63 public byte uni2ebcdic(char index) {
|
|
64 assert index < reverse_codepage.length;
|
|
65 return (byte)reverse_codepage[index];
|
|
66 }
|
|
67
|
|
68 /* (non-Javadoc)
|
|
69 * @see org.tn5250j.cp.ICodepageConverter#ebcdic2uni(int)
|
|
70 */
|
|
71 public char ebcdic2uni(int index) {
|
|
72 index = index & 0xFF;
|
|
73 assert index < 256;
|
|
74 return codepage[index];
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * @return The oringal 8bit codepage.
|
|
79 */
|
|
80 protected abstract char[] getCodePage();
|
|
81
|
|
82 }
|