annotate src/org/tn5250j/encoding/JavaCodePageFactory.java @ 96:5e52fa030308 stable-1.8.0

remove obsolete files
author Carl Byington <carl@five-ten-sg.com>
date Mon, 16 Jun 2014 17:50:04 -0700
parents ab8d2f6c5847
children 77ac18bc1b2f
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 public char ebcdic2uni(int codepoint) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 final ByteBuffer in = ByteBuffer.wrap(new byte[] { (byte) codepoint });
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 final CharBuffer out = this.decoder.decode(in);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 return out.get(0);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 } catch (Exception cce) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 return ' ';
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 }
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 /* (non-Javadoc)
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 * @see org.tn5250j.encoding.CodePage#uni2ebcdic(char)
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 public byte uni2ebcdic(char character) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 final CharBuffer in = CharBuffer.wrap(new char[] {character});
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 final ByteBuffer out = this.encoder.encode(in);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 return out.get(0);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 } catch (Exception cce) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 return 0x0;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 }
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 * @param encoding
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 * @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
74 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 /* package */ static ICodePage getCodePage(final String encoding) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 CharsetDecoder dec = null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 CharsetEncoder enc = null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 final Charset cs = java.nio.charset.Charset.forName(encoding);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 dec = cs.newDecoder();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 enc = cs.newEncoder();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 } catch (Exception e) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 enc = null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 dec = null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 if ((enc != null) && (dec != null)) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 return new JavaCodePageFactory(encoding, enc, dec);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 return null;
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
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 }