Mercurial > 510Connectbot
diff src/org/tn5250j/encoding/builtin/CodepageConverterAdapter.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 | b29b39f386a4 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/tn5250j/encoding/builtin/CodepageConverterAdapter.java Thu May 22 12:38:09 2014 -0700 @@ -0,0 +1,82 @@ +/** + * $Id$ + * + * Title: tn5250J + * Copyright: Copyright (c) 2001,2009 + * Company: + * @author: master_jaf + * + * Description: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + * + */ +package org.tn5250j.encoding.builtin; + +import java.util.Arrays; + +/** + * Adapter class for converters using 8bit codepages. + * + * @author master_jaf + */ +public abstract class CodepageConverterAdapter implements ICodepageConverter { + + private char[] codepage = null; + private int[] reverse_codepage = null; + + /* (non-Javadoc) + * @see org.tn5250j.cp.ICodepageConverter#init() + */ + public ICodepageConverter init() { + codepage = getCodePage(); + + int size = 0; + for (int i=0; i<codepage.length; i++) { + size = Math.max(size, codepage[i]); + } + assert (size + 1) < 1024*1024; // some kind of maximum size limiter. + reverse_codepage = new int[size+1]; + Arrays.fill(reverse_codepage, '?'); + for (int i=0; i<codepage.length; i++) { + reverse_codepage[codepage[i]] = i; + } + return this; + } + + /* (non-Javadoc) + * @see org.tn5250j.cp.ICodepageConverter#uni2ebcdic(char) + */ + public byte uni2ebcdic(char index) { + assert index < reverse_codepage.length; + return (byte)reverse_codepage[index]; + } + + /* (non-Javadoc) + * @see org.tn5250j.cp.ICodepageConverter#ebcdic2uni(int) + */ + public char ebcdic2uni(int index) { + index = index & 0xFF; + assert index < 256; + return codepage[index]; + } + + /** + * @return The oringal 8bit codepage. + */ + protected abstract char[] getCodePage(); + +}