PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/encoding/HPRoman8Encoding.java

http://mobicents.googlecode.com/
Java | 144 lines | 109 code | 5 blank | 30 comment | 0 complexity | 45acbb661d969bd68881b762e1b93163 MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.smpp.encoding;
 24
 25import java.io.ByteArrayOutputStream;
 26
 27/**
 28 * Encoding class representing the HP-Roman8 character set.
 29 * 
 30 * @version $Id: HPRoman8Encoding.java 452 2009-01-15 16:56:36Z orank $
 31 */
 32public final class HPRoman8Encoding extends AlphabetEncoding {
 33
 34    /**
 35     * Data coding value. There isn't an 'official' value for HP-Roman8. Usually
 36     * it is the default encoding of the SMSC.
 37     */
 38    private static final int DCS = 0;
 39
 40    private static final char[] CHAR_TABLE = {
 41        '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007',
 42        '\b',     '\t',     '\n',     '\u000b', '\f',     '\r',     '\u000e', '\u000f',
 43        '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017',
 44        '\u0018', '\u0019', '\u001a', '\u001b', '\u001c', '\u001d', '\u001e', '\u001f',
 45        ' ',      '!',      '"',      '#',      '$',      '%',      '&',      '\'',
 46        '(',      ')',      '*',      '+',      ',',      '-',      '.',      '/',
 47        '0',      '1',      '2',      '3',      '4',      '5',      '6',      '7',
 48        '8',      '9',      ':',      ';',      '<',      '=',      '>',      '?',
 49        '@',      'A',      'B',      'C',      'D',      'E',      'F',      'G',
 50        'H',      'I',      'J',      'K',      'L',      'M',      'N',      'O',
 51        'P',      'Q',      'R',      'S',      'T',      'U',      'V',      'W',
 52        'X',      'Y',      'Z',      '[',      '\\',     ']',     '^',     '_',
 53        '`',      'a',      'b',      'c',      'd',      'e',      'f',      'g',
 54        'h',      'i',      'j',      'k',      'l',      'm',      'n',      'o',
 55        'p',      'q',      'r',      's',      't',      'u',      'v',      'w',
 56        'x',      'y',      'z',      '{',      '|',      '}',      '~',      0,
 57        0,        0,        0,        0,        0,        0,        0,        0,
 58        0,        0,        0,        0,        0,        0,        0,        0,
 59        0,        0,        0,        0,        0,        0,        0,        0,
 60        0,        0,        0,        0,        0,        0,        0,        0,
 61        '\u00a0', '\u00c0', '\u00c2', '\u00c8', '\u00ca', '\u00cb', '\u00ce', '\u00cf',
 62        '\u00b4', '\u02cb', '\u02c6', '\u00a8', '\u02dc', '\u00d9', '\u00db', '\u20a4',
 63        '\u00af', '\u00dd', '\u00fd', '\u00b0', '\u00c7', '\u00e7', '\u00d1', '\u00f1',
 64        '\u00a1', '\u00bf', '\u00a4', '\u00a3', '\u00a5', '\u00a7', '\u0192', '\u00a2',
 65        '\u00e2', '\u00ea', '\u00f4', '\u00fb', '\u00e1', '\u00e9', '\u00f3', '\u00fa',
 66        '\u00e0', '\u00e8', '\u00f2', '\u00f9', '\u00e4', '\u00eb', '\u00f6', '\u00fc',
 67        '\u00c5', '\u00ee', '\u00d8', '\u00c6', '\u00e5', '\u00ed', '\u00f8', '\u00e6',
 68        '\u00c4', '\u00ec', '\u00d6', '\u00dc', '\u00c9', '\u00ef', '\u00df', '\u00d4',
 69        '\u00c1', '\u00c3', '\u00e3', '\u00d0', '\u00f0', '\u00cd', '\u00cc', '\u00d3',
 70        '\u00d2', '\u00d5', '\u00f5', '\u0160', '\u0161', '\u00da', '\u0178', '\u00ff',
 71        '\u00de', '\u00fe', '\u00b7', '\u00b5', '\u00b6', '\u00be', '\u2014', '\u00bc',
 72        '\u00bd', '\u00aa', '\u00ba', '\u00ab', '\u25a0', '\u00bb', '\u00b1',
 73    };
 74
 75    private int unknownCharReplacement = 0x3f;
 76    
 77    public HPRoman8Encoding() {
 78        super(DCS);
 79    }
 80
 81    public int getUnknownCharReplacement() {
 82        return unknownCharReplacement;
 83    }
 84
 85    /**
 86     * Set the byte to use when there is no code point for a Unicode character.
 87     * This byte will be inserted into an encoded byte array if the String
 88     * being encoded contains a character that HPRoman8
 89     * has no code point for. The default is to insert the code point for
 90     * the '?' character - that is, byte 0x3f.
 91     * @param unknownCharReplacement A code point for one of the characters
 92     * in the Roman8 character table.
 93     * @throws IllegalArgumentException If <code>0 &lt; unknownCharReplacement
 94     * &lt; 256</code> or <code>unknownCharReplacement</code> is <code>0x1b
 95     * </code>.
 96     */
 97    public void setUnknownCharReplacement(int unknownCharReplacement) {
 98        if (unknownCharReplacement < 0 || unknownCharReplacement > 255) {
 99            throw new IllegalArgumentException(
100                    "Replacement code point is out of bounds.");
101        }
102        this.unknownCharReplacement = unknownCharReplacement;
103    }
104
105    @Override
106    public String decode(byte[] data, int offset, int length) {
107        if (data == null) {
108            throw new NullPointerException("Data cannot be null.");
109        }
110        StringBuilder buf = new StringBuilder();
111        int endIndex = offset + length;
112        for (int i = offset; i < endIndex; i++) {
113            int code = (int) data[i] & 0x000000ff;
114            buf.append(CHAR_TABLE[code]);
115        }
116        return buf.toString();
117    }
118    
119    @Override
120    public byte[] encode(String s) {
121        if (s == null) {
122            return new byte[0];
123        }
124
125        char[] c = s.toCharArray();
126        ByteArrayOutputStream enc = new ByteArrayOutputStream(256);
127
128        for (int loop = 0; loop < c.length; loop++) {
129
130            int search = 0;
131            for (; search < CHAR_TABLE.length; search++) {
132
133                if (c[loop] == CHAR_TABLE[search]) {
134                    enc.write((byte) search);
135                    break;
136               }
137            }
138            if (search == CHAR_TABLE.length) {
139                enc.write(unknownCharReplacement);
140            }
141        }
142        return enc.toByteArray();
143    }
144}