Mercurial > 510Connectbot
annotate app/src/main/java/org/tn5250j/encoding/JavaCodePageFactory.java @ 507:f7498bd7621b
Added tag stable-1.9.4-5 for changeset b8cc360e1550
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 16 Nov 2022 12:07:04 -0700 |
parents | d29cce60f393 |
children |
rev | line source |
---|---|
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 | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
36 private final CharsetEncoder encoder; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
37 private final CharsetDecoder decoder; |
5 | 38 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
39 /* package */ JavaCodePageFactory(String encoding, CharsetEncoder encoder, CharsetDecoder decoder) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
40 super(encoding); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
41 this.encoder = encoder; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
42 this.decoder = decoder; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
43 } |
5 | 44 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
45 /* (non-Javadoc) |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
46 * @see org.tn5250j.encoding.CodePage#ebcdic2uni(int) |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
47 */ |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
48 public char ebcdic2uni(int codepoint) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
49 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
50 final ByteBuffer in = ByteBuffer.wrap(new byte[] { (byte) codepoint }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
51 final CharBuffer out = this.decoder.decode(in); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
52 return out.get(0); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
53 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
54 catch (Exception cce) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
55 return ' '; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
56 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
57 } |
5 | 58 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
59 /* (non-Javadoc) |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
60 * @see org.tn5250j.encoding.CodePage#uni2ebcdic(char) |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
61 */ |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
62 public byte uni2ebcdic(char character) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
63 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
64 final CharBuffer in = CharBuffer.wrap(new char[] {character}); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
65 final ByteBuffer out = this.encoder.encode(in); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
66 return out.get(0); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
67 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
68 catch (Exception cce) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
69 return 0x0; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
70 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
71 } |
5 | 72 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
73 /** |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
74 * @param encoding |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
75 * @return A new {@link CodePage} object OR null, if not available. |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
76 */ |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
77 /* package */ static ICodePage getCodePage(final String encoding) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
78 CharsetDecoder dec = null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
79 CharsetEncoder enc = null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
80 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
81 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
82 final Charset cs = java.nio.charset.Charset.forName(encoding); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
83 dec = cs.newDecoder(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
84 enc = cs.newEncoder(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
85 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
86 catch (Exception e) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
87 enc = null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
88 dec = null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
89 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
90 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
91 if ((enc != null) && (dec != null)) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
92 return new JavaCodePageFactory(encoding, enc, dec); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
93 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
94 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
95 return null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
96 } |
5 | 97 |
98 } |