Mercurial > 510Connectbot
comparison app/src/main/java/org/tn5250j/encoding/JavaCodePageFactory.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/org/tn5250j/encoding/JavaCodePageFactory.java@77ac18bc1b2f |
children |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
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 } | |
54 catch (Exception cce) { | |
55 return ' '; | |
56 } | |
57 } | |
58 | |
59 /* (non-Javadoc) | |
60 * @see org.tn5250j.encoding.CodePage#uni2ebcdic(char) | |
61 */ | |
62 public byte uni2ebcdic(char character) { | |
63 try { | |
64 final CharBuffer in = CharBuffer.wrap(new char[] {character}); | |
65 final ByteBuffer out = this.encoder.encode(in); | |
66 return out.get(0); | |
67 } | |
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 | |
81 try { | |
82 final Charset cs = java.nio.charset.Charset.forName(encoding); | |
83 dec = cs.newDecoder(); | |
84 enc = cs.newEncoder(); | |
85 } | |
86 catch (Exception e) { | |
87 enc = null; | |
88 dec = null; | |
89 } | |
90 | |
91 if ((enc != null) && (dec != null)) { | |
92 return new JavaCodePageFactory(encoding, enc, dec); | |
93 } | |
94 | |
95 return null; | |
96 } | |
97 | |
98 } |