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

http://mobicents.googlecode.com/ · Java · 176 lines · 105 code · 31 blank · 40 comment · 39 complexity · 62f9bf6bcab4c06364ff2ea328470630 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;
  23. import java.nio.ByteBuffer;
  24. import java.nio.CharBuffer;
  25. import java.nio.charset.Charset;
  26. import java.nio.charset.CharsetEncoder;
  27. import java.nio.charset.CoderResult;
  28. import java.util.BitSet;
  29. /**
  30. *
  31. * @author amit bhayani
  32. * @author sergey vetyutnev
  33. *
  34. */
  35. public class GSMCharsetEncoder extends CharsetEncoder {
  36. private int bitpos = 0;
  37. private int carryOver;
  38. private GSMCharset cs;
  39. private GSMCharsetEncodingData encodingData;
  40. // The mask to check if corresponding bit in read byte is 1 or 0 and hence
  41. // store it i BitSet accordingly
  42. byte[] mask = new byte[] { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };
  43. // BitSet to hold the bits of passed char to be encoded
  44. BitSet bitSet = new BitSet();
  45. static final byte ESCAPE = 0x1B;
  46. protected GSMCharsetEncoder(Charset cs, float averageBytesPerChar,
  47. float maxBytesPerChar) {
  48. super(cs, averageBytesPerChar, maxBytesPerChar);
  49. implReset();
  50. this.cs = (GSMCharset) cs;
  51. if (encodingData != null)
  52. encodingData.totalSeptetCount = 0;
  53. }
  54. public void setGSMCharsetEncodingData(GSMCharsetEncodingData encodingData) {
  55. this.encodingData = encodingData;
  56. }
  57. public GSMCharsetEncodingData getGSMCharsetEncodingData() {
  58. return this.encodingData;
  59. }
  60. @Override
  61. protected void implReset() {
  62. bitpos = 0;
  63. carryOver = 0;
  64. bitSet.clear();
  65. }
  66. /**
  67. * TODO :
  68. */
  69. @Override
  70. protected CoderResult implFlush(ByteBuffer out) {
  71. if (!out.hasRemaining()) {
  72. return CoderResult.OVERFLOW;
  73. }
  74. return CoderResult.UNDERFLOW;
  75. }
  76. byte rawData = 0;
  77. @Override
  78. protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
  79. if (this.encodingData != null && this.encodingData.leadingBuffer != null) {
  80. int septetCount = (this.encodingData.leadingBuffer.length * 8 + 6) / 7;
  81. bitpos = septetCount % 8;
  82. this.encodingData.totalSeptetCount = septetCount;
  83. for (int ind = 0; ind < this.encodingData.leadingBuffer.length; ind++) {
  84. out.put(this.encodingData.leadingBuffer[ind]);
  85. }
  86. }
  87. char lastChar = ' ';
  88. while (in.hasRemaining()) {
  89. // Read the first char
  90. char c = in.get();
  91. lastChar = c;
  92. boolean found = false;
  93. // searching a char in the main character table
  94. for (int i = 0; i < this.cs.mainTable.length; i++) {
  95. if (this.cs.mainTable[i] == c) {
  96. found = true;
  97. this.putByte(i, out);
  98. break;
  99. }
  100. }
  101. // searching a char in the extension character table
  102. if (!found && this.cs.extensionTable != null) {
  103. for (int i = 0; i < this.cs.mainTable.length; i++) {
  104. if (this.cs.extensionTable[i] == c) {
  105. found = true;
  106. this.putByte(GSMCharsetEncoder.ESCAPE, out);
  107. this.putByte(i, out);
  108. break;
  109. }
  110. }
  111. }
  112. if (!found) {
  113. // found no suitable symbol - encode a space char
  114. this.putByte(0x20, out);
  115. }
  116. }
  117. if (bitpos != 0) {
  118. // USSD: replace 7-bit pad with <CR>
  119. if (this.encodingData != null && this.encodingData.ussdStyleEncoding && bitpos == 7)
  120. carryOver |= 0x1A;
  121. // writing a carryOver data
  122. out.put((byte) carryOver);
  123. } else {
  124. // USSD: adding extra <CR> if the last symbol is <CR> and no padding
  125. if (this.encodingData != null && this.encodingData.ussdStyleEncoding && lastChar == '\r')
  126. out.put((byte) 0x0D);
  127. }
  128. return CoderResult.UNDERFLOW;
  129. }
  130. private void putByte(int data, ByteBuffer out) {
  131. if (bitpos == 0) {
  132. carryOver = data;
  133. } else {
  134. int i1 = data << (8 - bitpos);
  135. out.put((byte) (i1 | carryOver));
  136. carryOver = data >>> bitpos;
  137. }
  138. bitpos++;
  139. if (bitpos == 8) {
  140. bitpos = 0;
  141. }
  142. if (this.encodingData != null)
  143. this.encodingData.totalSeptetCount++;
  144. }
  145. }