comparison app/src/main/java/org/tn5250j/encoding/ToolboxCodePageFactory.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/org/tn5250j/encoding/ToolboxCodePageFactory.java@77ac18bc1b2f
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /**
2 * Title: ToolboxCodePage
3 * Copyright: Copyright (c) 2001, 2002, 2003
4 * Company:
5 * @author Kenneth J. Pouncey
6 * rewritten by LDC, WVL, Luc
7 * @version 0.4
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;
28
29 import java.lang.reflect.Constructor;
30 import java.lang.reflect.Method;
31
32 import android.util.Log;
33
34
35 class ToolboxCodePageFactory {
36 private static final String TAG = "ToolboxCodePageFactory";
37 private final static String[] CODEPAGES = { "Big5", "Cp037", "Cp273", "Cp277", "Cp278",
38 "Cp280", "Cp284", "Cp285", "Cp297", "Cp420", "Cp424", "Cp437",
39 "Cp500", "Cp737", "Cp775", "Cp838", "Cp850", "Cp852", "Cp855",
40 "Cp856", "Cp857", "Cp858", "Cp860", "Cp861", "Cp862", "Cp863",
41 "Cp864", "Cp865", "Cp866", "Cp868", "Cp869", "Cp870",
42 "Cp871", "Cp874", "Cp875", "Cp918", "Cp921", "Cp922",
43 "Cp923", // IBM Latin-9.
44 "Cp930", "Cp933", "Cp935", "Cp937", "Cp939", "Cp942", "Cp943",
45 "Cp948", "Cp949", "Cp950", "Cp964", "Cp970", "Cp1006", "Cp1025",
46 "Cp1026", "Cp1046", "Cp1097", "Cp1098", "Cp1112", "Cp1122",
47 "Cp1123", "Cp1124", "Cp1140", "Cp1141", "Cp1142", "Cp1143",
48 "Cp1144", "Cp1145", "Cp1146", "Cp1147", "Cp1148", "Cp1149",
49 "Cp1252", "Cp1250", "Cp1251", "Cp1253", "Cp1254", "Cp1255",
50 "Cp1256", "Cp1257", "Cp1258", "Cp1381", "Cp1383", "Cp33722"
51 };
52
53 private static final String CONVERTER_NAME = "com.ibm.as400.access.CharConverter";
54 private static final String TOBYTES_NAME = "stringToByteArray";
55 private static final String TOSTRING_NAME = "byteArrayToString";
56
57 private static ToolboxCodePageFactory singleton;
58
59
60
61 private ToolboxCodePageFactory() {
62 /* private for singleton */
63 }
64
65 public static synchronized ToolboxCodePageFactory getInstance() {
66 if (singleton == null) {
67 singleton = new ToolboxCodePageFactory();
68 }
69
70 return singleton;
71 }
72
73 /**
74 * @return
75 */
76 public String[] getAvailableCodePages() {
77 try {
78 final ClassLoader loader = getClassLoader();
79 Class.forName(CONVERTER_NAME, false, loader);
80 return CODEPAGES;
81 }
82 catch (Exception e) {
83 Log.i(TAG, "Couldn't locate JT400 Toolbox in classpath. Charset converters can't be used.");
84 return new String[0];
85 }
86 }
87
88 /**
89 * @param encoding
90 * @return
91 */
92 public ICodePage getCodePage(String encoding) {
93 try {
94 ClassLoader loader = getClassLoader();
95 Class<?> conv_class = Class.forName(CONVERTER_NAME, true, loader);
96 Constructor<?> conv_constructor = conv_class.getConstructor(new Class[] { String.class });
97 Method toBytes_method = conv_class.getMethod(TOBYTES_NAME, new Class[] { String.class });
98 Method toString_method = conv_class.getMethod(TOSTRING_NAME, new Class[] { byte[].class });
99 Object convobj = conv_constructor.newInstance(new Object[] { encoding });
100 return new ToolboxConverterProxy(convobj, toBytes_method, toString_method);
101 }
102 catch (Exception e) {
103 Log.w(TAG, "Can't load charset converter from JT400 Toolbox for code page " + encoding, e);
104 return null;
105 }
106 }
107
108 private static final ClassLoader getClassLoader() {
109 ClassLoader loader = ToolboxCodePageFactory.class.getClassLoader();
110
111 if (loader == null) {
112 loader = ClassLoader.getSystemClassLoader();
113 }
114
115 return loader;
116 }
117
118 private static class ToolboxConverterProxy implements ICodePage {
119
120 private final Object converter;
121 private final Method tobytesMethod;
122 private final Method tostringMethod;
123
124 private ToolboxConverterProxy(Object converterObject, Method tobytesMethod, Method tostringMethod) {
125 super();
126 this.converter = converterObject;
127 this.tobytesMethod = tobytesMethod;
128 this.tostringMethod = tostringMethod;
129 }
130
131 public char ebcdic2uni(int index) {
132 Object result;
133
134 try {
135 result = tostringMethod.invoke(converter, new Object[] { new byte[] { (byte)(index & 0xFF) } });
136 }
137 catch (Throwable t) {
138 result = null;
139 }
140
141 if (result == null)
142 return 0x00;
143
144 return ((String) result).charAt(0);
145 }
146
147 public byte uni2ebcdic(char index) {
148 Object result;
149
150 try {
151 result = tobytesMethod.invoke(converter, new Object[] { new String(new char[] { index }) });
152 }
153 catch (Throwable t) {
154 result = null;
155 }
156
157 if (result == null)
158 return 0x00;
159
160 return ((byte[]) result)[0];
161 }
162 }
163
164 }