001: /*
002: * $Id: AsciiEbcdicEncoder.java,v 1.1 2004/08/13 02:01:10 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.util;
042:
043: /**
044: * Utility to convert Ascii to EBCDIC and vice-versa
045: * @author Rahul Dwivedi
046: */
047: public class AsciiEbcdicEncoder {
048: private static byte[] AToE = {
049: /* 0 */0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 21, 11, 12, 13, 14, 15,
050: /* 16 */16, 17, 18, 63, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29,
051: 30, 31,
052: /* 32 */64, 90, 127, 123, 91, 108, 80, 125, 77, 93, 92,
053: 78, 107, 96, 75, 97,
054: /* 48 */-16, -15, -14, -13, -12, -11, -10, -9, -8, -7,
055: 122, 94, 76, 126, 110, 111,
056: /* 64 */124, -63, -62, -61, -60, -59, -58, -57, -56, -55,
057: -47, -46, -45, -44, -43, -42,
058: /* 80 */-41, -40, -39, -30, -29, -28, -27, -26, -25, -24,
059: -23, 63, 63, 63, 63, 109,
060: /* 96 */-71, -127, -126, -125, -124, -123, -122, -121,
061: -120, -119, -111, -110, -109, -108, -107, -106,
062: /* 112 */-105, -104, -103, -94, -93, -92, -91, -90, -89,
063: -88, -87, 63, 79, 63, 63, 7,
064: /* 128 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
065: 63, 63, 63, 63,
066: /* 144 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
067: 63, 63, 63, 63,
068: /* 160 */64, 63, 74, 123, 63, 63, 63, 63, 63, 63, 63, 63,
069: 95, 96, 63, 63,
070: /* 176 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
071: 63, 63, 63, 63,
072: /* 192 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
073: 63, 63, 63, 63,
074: /* 208 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
075: 63, 63, 63, 63,
076: /* 224 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
077: 63, 63, 63, 63,
078: /* 240 */63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
079: 63, 63, 63, 63 };
080:
081: /**
082: * EBCDIC to ASCII translate table. source code generated by com.mindprod.trt.TrtBuild
083: */
084: private static byte[] EToA = {
085: /* 0 */0, 1, 2, 3, 26, 9, 26, 127, 26, 26, 26, 11, 12, 13, 14, 15,
086: /* 16 */16, 17, 18, 26, 26, 10, 8, 26, 24, 25, 26, 26, 28, 29, 30,
087: 31,
088: /* 32 */26, 26, 28, 26, 26, 10, 23, 27, 26, 26, 26, 26,
089: 26, 5, 6, 7,
090: /* 48 */26, 26, 22, 26, 26, 30, 26, 4, 26, 26, 26, 26, 20,
091: 21, 26, 26,
092: /* 64 */32, 26, 26, 26, 26, 26, 26, 26, 26, 26, -94, 46,
093: 60, 40, 43, 124,
094: /* 80 */38, 26, 26, 26, 26, 26, 26, 26, 26, 26, 33, 36,
095: 42, 41, 59, -84,
096: /* 96 */45, 47, 26, 26, 26, 26, 26, 26, 26, 26, 26, 44,
097: 37, 95, 62, 63,
098: /* 112 */26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 58, 35,
099: 64, 39, 61, 34,
100: /* 128 */26, 97, 98, 99, 100, 101, 102, 103, 104, 105, 26,
101: 26, 26, 26, 26, 26,
102: /* 144 */26, 106, 107, 108, 109, 110, 111, 112, 113, 114,
103: 26, 26, 26, 26, 26, 26,
104: /* 160 */26, 26, 115, 116, 117, 118, 119, 120, 121, 122,
105: 26, 26, 26, 26, 26, 26,
106: /* 176 */26, 26, 26, 26, 26, 26, 26, 26, 26, 96, 26, 26,
107: 26, 26, 26, 26,
108: /* 192 */26, 65, 66, 67, 68, 69, 70, 71, 72, 73, 26, 26,
109: 26, 26, 26, 26,
110: /* 208 */26, 74, 75, 76, 77, 78, 79, 80, 81, 82, 26, 26,
111: 26, 26, 26, 26,
112: /* 224 */26, 26, 83, 84, 85, 86, 87, 88, 89, 90, 26, 26,
113: 26, 26, 26, 26,
114: /* 240 */48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 26, 26,
115: 26, 26, 26, 26 };
116:
117: public static int ASCIIToEBCDIC(int ascii) {
118: return AToE[ascii & 0xff] & 0xff;
119: }
120:
121: public static int EBCDICToASCII(int ebcdic) {
122: return EToA[ebcdic & 0xff] & 0xff;
123: }
124:
125: public static void convertAsciiToEbcdic(byte[] ascii) {
126: for (int i = 0; i < ascii.length; i++) {
127: ascii[i] = (byte) ASCIIToEBCDIC(ascii[i]);
128: }
129: }
130:
131: public static void convertEbcdicToAscii(byte[] ascii) {
132: for (int i = 0; i < ascii.length; i++) {
133: ascii[i] = (byte) EBCDICToASCII(ascii[i]);
134: }
135: }
136: }
|