/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. package org.mobicents.protocols.smpp.encoding;
  23. import java.io.ByteArrayOutputStream;
  24. /**
  25. * Encoding class representing the HP-Roman8 character set.
  26. *
  27. * @version $Id: HPRoman8Encoding.java 452 2009-01-15 16:56:36Z orank $
  28. */
  29. public final class HPRoman8Encoding extends AlphabetEncoding {
  30. /**
  31. * Data coding value. There isn't an 'official' value for HP-Roman8. Usually
  32. * it is the default encoding of the SMSC.
  33. */
  34. private static final int DCS = 0;
  35. private static final char[] CHAR_TABLE = {
  36. '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007',
  37. '\b', '\t', '\n', '\u000b', '\f', '\r', '\u000e', '\u000f',
  38. '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017',
  39. '\u0018', '\u0019', '\u001a', '\u001b', '\u001c', '\u001d', '\u001e', '\u001f',
  40. ' ', '!', '"', '#', '$', '%', '&', '\'',
  41. '(', ')', '*', '+', ',', '-', '.', '/',
  42. '0', '1', '2', '3', '4', '5', '6', '7',
  43. '8', '9', ':', ';', '<', '=', '>', '?',
  44. '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  45. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  46. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  47. 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
  48. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  49. 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  50. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  51. 'x', 'y', 'z', '{', '|', '}', '~', 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0,
  56. '\u00a0', '\u00c0', '\u00c2', '\u00c8', '\u00ca', '\u00cb', '\u00ce', '\u00cf',
  57. '\u00b4', '\u02cb', '\u02c6', '\u00a8', '\u02dc', '\u00d9', '\u00db', '\u20a4',
  58. '\u00af', '\u00dd', '\u00fd', '\u00b0', '\u00c7', '\u00e7', '\u00d1', '\u00f1',
  59. '\u00a1', '\u00bf', '\u00a4', '\u00a3', '\u00a5', '\u00a7', '\u0192', '\u00a2',
  60. '\u00e2', '\u00ea', '\u00f4', '\u00fb', '\u00e1', '\u00e9', '\u00f3', '\u00fa',
  61. '\u00e0', '\u00e8', '\u00f2', '\u00f9', '\u00e4', '\u00eb', '\u00f6', '\u00fc',
  62. '\u00c5', '\u00ee', '\u00d8', '\u00c6', '\u00e5', '\u00ed', '\u00f8', '\u00e6',
  63. '\u00c4', '\u00ec', '\u00d6', '\u00dc', '\u00c9', '\u00ef', '\u00df', '\u00d4',
  64. '\u00c1', '\u00c3', '\u00e3', '\u00d0', '\u00f0', '\u00cd', '\u00cc', '\u00d3',
  65. '\u00d2', '\u00d5', '\u00f5', '\u0160', '\u0161', '\u00da', '\u0178', '\u00ff',
  66. '\u00de', '\u00fe', '\u00b7', '\u00b5', '\u00b6', '\u00be', '\u2014', '\u00bc',
  67. '\u00bd', '\u00aa', '\u00ba', '\u00ab', '\u25a0', '\u00bb', '\u00b1',
  68. };
  69. private int unknownCharReplacement = 0x3f;
  70. public HPRoman8Encoding() {
  71. super(DCS);
  72. }
  73. public int getUnknownCharReplacement() {
  74. return unknownCharReplacement;
  75. }
  76. /**
  77. * Set the byte to use when there is no code point for a Unicode character.
  78. * This byte will be inserted into an encoded byte array if the String
  79. * being encoded contains a character that HPRoman8
  80. * has no code point for. The default is to insert the code point for
  81. * the '?' character - that is, byte 0x3f.
  82. * @param unknownCharReplacement A code point for one of the characters
  83. * in the Roman8 character table.
  84. * @throws IllegalArgumentException If <code>0 &lt; unknownCharReplacement
  85. * &lt; 256</code> or <code>unknownCharReplacement</code> is <code>0x1b
  86. * </code>.
  87. */
  88. public void setUnknownCharReplacement(int unknownCharReplacement) {
  89. if (unknownCharReplacement < 0 || unknownCharReplacement > 255) {
  90. throw new IllegalArgumentException(
  91. "Replacement code point is out of bounds.");
  92. }
  93. this.unknownCharReplacement = unknownCharReplacement;
  94. }
  95. @Override
  96. public String decode(byte[] data, int offset, int length) {
  97. if (data == null) {
  98. throw new NullPointerException("Data cannot be null.");
  99. }
  100. StringBuilder buf = new StringBuilder();
  101. int endIndex = offset + length;
  102. for (int i = offset; i < endIndex; i++) {
  103. int code = (int) data[i] & 0x000000ff;
  104. buf.append(CHAR_TABLE[code]);
  105. }
  106. return buf.toString();
  107. }
  108. @Override
  109. public byte[] encode(String s) {
  110. if (s == null) {
  111. return new byte[0];
  112. }
  113. char[] c = s.toCharArray();
  114. ByteArrayOutputStream enc = new ByteArrayOutputStream(256);
  115. for (int loop = 0; loop < c.length; loop++) {
  116. int search = 0;
  117. for (; search < CHAR_TABLE.length; search++) {
  118. if (c[loop] == CHAR_TABLE[search]) {
  119. enc.write((byte) search);
  120. break;
  121. }
  122. }
  123. if (search == CHAR_TABLE.length) {
  124. enc.write(unknownCharReplacement);
  125. }
  126. }
  127. return enc.toByteArray();
  128. }
  129. }