annotate src/org/tn5250j/encoding/JavaCodePageFactory.java @ 59:228273d69ca4 tn5250

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