5
|
1 /**
|
|
2 * Title: JavaCodePage
|
|
3 * Copyright: Copyright (c) 2001, 2002, 2003
|
|
4 * Company:
|
|
5 * @author LDC, WVL, Luc, master_jaf
|
|
6 * @version 0.4
|
|
7 *
|
|
8 * Description:
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2, or (at your option)
|
|
13 * any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this software; see the file COPYING. If not, write to
|
|
22 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
23 * Boston, MA 02111-1307 USA
|
|
24 *
|
|
25 */
|
|
26 package org.tn5250j.encoding;
|
|
27
|
|
28 import java.nio.ByteBuffer;
|
|
29 import java.nio.CharBuffer;
|
|
30 import java.nio.charset.Charset;
|
|
31 import java.nio.charset.CharsetDecoder;
|
|
32 import java.nio.charset.CharsetEncoder;
|
|
33
|
|
34 /* package */ class JavaCodePageFactory extends AbstractCodePage {
|
|
35
|
|
36 private final CharsetEncoder encoder;
|
|
37 private final CharsetDecoder decoder;
|
|
38
|
|
39 /* package */ JavaCodePageFactory(String encoding, CharsetEncoder encoder, CharsetDecoder decoder) {
|
|
40 super(encoding);
|
|
41 this.encoder = encoder;
|
|
42 this.decoder = decoder;
|
|
43 }
|
|
44
|
|
45 /* (non-Javadoc)
|
|
46 * @see org.tn5250j.encoding.CodePage#ebcdic2uni(int)
|
|
47 */
|
|
48 public char ebcdic2uni(int codepoint) {
|
|
49 try {
|
|
50 final ByteBuffer in = ByteBuffer.wrap(new byte[] { (byte) codepoint });
|
|
51 final CharBuffer out = this.decoder.decode(in);
|
|
52 return out.get(0);
|
|
53 } catch (Exception cce) {
|
|
54 return ' ';
|
|
55 }
|
|
56 }
|
|
57
|
|
58 /* (non-Javadoc)
|
|
59 * @see org.tn5250j.encoding.CodePage#uni2ebcdic(char)
|
|
60 */
|
|
61 public byte uni2ebcdic(char character) {
|
|
62 try {
|
|
63 final CharBuffer in = CharBuffer.wrap(new char[] {character});
|
|
64 final ByteBuffer out = this.encoder.encode(in);
|
|
65 return out.get(0);
|
|
66 } catch (Exception cce) {
|
|
67 return 0x0;
|
|
68 }
|
|
69 }
|
|
70
|
|
71 /**
|
|
72 * @param encoding
|
|
73 * @return A new {@link CodePage} object OR null, if not available.
|
|
74 */
|
|
75 /* package */ static ICodePage getCodePage(final String encoding) {
|
|
76 CharsetDecoder dec = null;
|
|
77 CharsetEncoder enc = null;
|
|
78 try {
|
|
79 final Charset cs = java.nio.charset.Charset.forName(encoding);
|
|
80 dec = cs.newDecoder();
|
|
81 enc = cs.newEncoder();
|
|
82 } catch (Exception e) {
|
|
83 enc = null;
|
|
84 dec = null;
|
|
85 }
|
|
86 if ((enc != null) && (dec != null)) {
|
|
87 return new JavaCodePageFactory(encoding, enc, dec);
|
|
88 }
|
|
89 return null;
|
|
90 }
|
|
91
|
|
92 } |