/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/TbcdString.java

http://mobicents.googlecode.com/ · Java · 175 lines · 124 code · 19 blank · 32 comment · 13 complexity · e54061cdc39f01e4674d0eb057016ac0 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.ss7.map.primitives;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;
  26. import org.mobicents.protocols.ss7.map.api.MAPException;
  27. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  28. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  29. /**
  30. *
  31. * @author amit bhayani
  32. * @author sergey vetyutnev
  33. *
  34. */
  35. public abstract class TbcdString implements MAPAsnPrimitive {
  36. protected static int DIGIT_1_MASK = 0x0F;
  37. protected static int DIGIT_2_MASK = 0xF0;
  38. public static String decodeString(InputStream ansIS, int length) throws IOException, MAPParsingComponentException {
  39. StringBuilder s = new StringBuilder();
  40. for (int i1 = 0; i1 < length; i1++) {
  41. int b = ansIS.read();
  42. int digit1 = (b & DIGIT_1_MASK);
  43. if (digit1 == 15) {
  44. // this is mask
  45. } else {
  46. s.append(decodeNumber(digit1));
  47. }
  48. int digit2 = ((b & DIGIT_2_MASK) >> 4);
  49. if (digit2 == 15) {
  50. // this is mask
  51. } else {
  52. s.append(decodeNumber(digit2));
  53. }
  54. }
  55. return s.toString();
  56. }
  57. public static void encodeString(OutputStream asnOs, String data) throws MAPException {
  58. char[] chars = data.toCharArray();
  59. for (int i = 0; i < chars.length; i = i + 2) {
  60. char a = chars[i];
  61. int digit1 = encodeNumber(a);
  62. int digit2;
  63. if ((i + 1) == chars.length) {
  64. // add the filler instead
  65. digit2 = 15;
  66. } else {
  67. char b = chars[i + 1];
  68. digit2 = encodeNumber(b);
  69. }
  70. int digit = (digit2 << 4) | digit1;
  71. try {
  72. asnOs.write(digit);
  73. } catch (IOException e) {
  74. throw new MAPException("Error when encoding TbcdString: " + e.getMessage(), e);
  75. }
  76. }
  77. }
  78. protected static int encodeNumber(char c) throws MAPException {
  79. switch (c) {
  80. case '0':
  81. return 0;
  82. case '1':
  83. return 1;
  84. case '2':
  85. return 2;
  86. case '3':
  87. return 3;
  88. case '4':
  89. return 4;
  90. case '5':
  91. return 5;
  92. case '6':
  93. return 6;
  94. case '7':
  95. return 7;
  96. case '8':
  97. return 8;
  98. case '9':
  99. return 9;
  100. case '*':
  101. return 10;
  102. case '#':
  103. return 11;
  104. case 'a':
  105. return 12;
  106. case 'b':
  107. return 13;
  108. case 'c':
  109. return 14;
  110. default:
  111. throw new MAPException(
  112. "char should be between 0 - 9, *, #, a, b, c for Telephony Binary Coded Decimal String. Received "
  113. + c);
  114. }
  115. }
  116. protected static char decodeNumber(int i) throws MAPParsingComponentException {
  117. switch (i) {
  118. case 0:
  119. return '0';
  120. case 1:
  121. return '1';
  122. case 2:
  123. return '2';
  124. case 3:
  125. return '3';
  126. case 4:
  127. return '4';
  128. case 5:
  129. return '5';
  130. case 6:
  131. return '6';
  132. case 7:
  133. return '7';
  134. case 8:
  135. return '8';
  136. case 9:
  137. return '9';
  138. case 10:
  139. return '*';
  140. case 11:
  141. return '#';
  142. case 12:
  143. return 'a';
  144. case 13:
  145. return 'b';
  146. case 14:
  147. return 'c';
  148. // case 15:
  149. // return 'd';
  150. default:
  151. throw new MAPParsingComponentException(
  152. "Integer should be between 0 - 15 for Telephony Binary Coded Decimal String. Received "
  153. + i, MAPParsingComponentExceptionReason.MistypedParameter);
  154. }
  155. }
  156. }