comparison src/org/tn5250j/encoding/JavaCodePageFactory.java @ 5:cbdff98c45ea tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 12:38:09 -0700
parents
children ab8d2f6c5847
comparison
equal deleted inserted replaced
4:1f5d9b76a183 5:cbdff98c45ea
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 @Override
49 public char ebcdic2uni(int codepoint) {
50 try {
51 final ByteBuffer in = ByteBuffer.wrap(new byte[] { (byte) codepoint });
52 final CharBuffer out = this.decoder.decode(in);
53 return out.get(0);
54 } catch (Exception cce) {
55 return ' ';
56 }
57 }
58
59 /* (non-Javadoc)
60 * @see org.tn5250j.encoding.CodePage#uni2ebcdic(char)
61 */
62 @Override
63 public byte uni2ebcdic(char character) {
64 try {
65 final CharBuffer in = CharBuffer.wrap(new char[] {character});
66 final ByteBuffer out = this.encoder.encode(in);
67 return out.get(0);
68 } catch (Exception cce) {
69 return 0x0;
70 }
71 }
72
73 /**
74 * @param encoding
75 * @return A new {@link CodePage} object OR null, if not available.
76 */
77 /* package */ static ICodePage getCodePage(final String encoding) {
78 CharsetDecoder dec = null;
79 CharsetEncoder enc = null;
80 try {
81 final Charset cs = java.nio.charset.Charset.forName(encoding);
82 dec = cs.newDecoder();
83 enc = cs.newEncoder();
84 } catch (Exception e) {
85 enc = null;
86 dec = null;
87 }
88 if ((enc != null) && (dec != null)) {
89 return new JavaCodePageFactory(encoding, enc, dec);
90 }
91 return null;
92 }
93
94 }