PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/ss7/isup/isup-impl/src/main/java/org/mobicents/protocols/ss7/isup/impl/message/parameter/TransitNetworkSelectionImpl.java

http://mobicents.googlecode.com/
Java | 319 lines | 177 code | 50 blank | 92 comment | 22 complexity | 493998148d58e1711fb642841bb7c526 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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. /**
  23. * Start time:17:05:31 2009-04-03<br>
  24. * Project: mobicents-isup-stack<br>
  25. *
  26. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski
  27. * </a>
  28. *
  29. */
  30. package org.mobicents.protocols.ss7.isup.impl.message.parameter;
  31. import java.io.ByteArrayInputStream;
  32. import java.io.ByteArrayOutputStream;
  33. import java.io.IOException;
  34. import org.apache.log4j.Logger;
  35. import org.mobicents.protocols.ss7.isup.ParameterException;
  36. import org.mobicents.protocols.ss7.isup.message.parameter.TransitNetworkSelection;
  37. /**
  38. * Start time:17:05:31 2009-04-03<br>
  39. * Project: mobicents-isup-stack<br>
  40. *
  41. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  42. */
  43. public class TransitNetworkSelectionImpl extends AbstractISUPParameter implements TransitNetworkSelection {
  44. protected static final Logger logger = Logger.getLogger(TransitNetworkSelectionImpl.class);
  45. // FIXME: Oleg is this correct?
  46. private String address;
  47. private int typeOfNetworkIdentification;
  48. private int networkIdentificationPlan;
  49. /**
  50. * Holds odd flag, it can have either value: 10000000(x80) or 00000000. For
  51. * each it takes value 1 and 0;
  52. */
  53. protected int oddFlag;
  54. /**
  55. * indicates odd flag value (0x80) as integer (1). This is achieved when odd
  56. * flag in parameter is moved to the right by sever possitions ( >> 7)
  57. */
  58. public static final int _FLAG_ODD = 1;
  59. public TransitNetworkSelectionImpl(String address, int typeOfNetworkIdentification, int networkIdentificationPlan) {
  60. super();
  61. this.setAddress(address);
  62. this.typeOfNetworkIdentification = typeOfNetworkIdentification;
  63. this.networkIdentificationPlan = networkIdentificationPlan;
  64. }
  65. public TransitNetworkSelectionImpl(byte[] b) throws ParameterException {
  66. super();
  67. decode(b);
  68. }
  69. public TransitNetworkSelectionImpl() {
  70. super();
  71. }
  72. public byte[] encode() throws ParameterException {
  73. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  74. if (logger.isDebugEnabled()) {
  75. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding header");
  76. }
  77. int count = encodeHeader(bos);
  78. if (logger.isDebugEnabled()) {
  79. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding header, write count: " + count);
  80. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding body");
  81. }
  82. count += encodeDigits(bos);
  83. if (logger.isDebugEnabled()) {
  84. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding digits, write count: " + count);
  85. }
  86. ByteArrayOutputStream out = new ByteArrayOutputStream();
  87. // out.write(tag);
  88. // Util.encodeLength(count, out);
  89. try {
  90. out.write(bos.toByteArray());
  91. } catch (IOException e) {
  92. throw new ParameterException(e);
  93. }
  94. return out.toByteArray();
  95. }
  96. public int encode(ByteArrayOutputStream out) throws ParameterException {
  97. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  98. if (logger.isDebugEnabled()) {
  99. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding header");
  100. }
  101. int count = encodeHeader(bos);
  102. if (logger.isDebugEnabled()) {
  103. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding header, write count: " + count);
  104. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding body");
  105. }
  106. count += encodeDigits(bos);
  107. if (logger.isDebugEnabled()) {
  108. logger.debug("[" + this.getClass().getSimpleName() + "]Encoding digits, write count: " + count);
  109. }
  110. // count += tag.length;
  111. // out.write(tag);
  112. // count += Util.encodeLength(count, out);
  113. try {
  114. out.write(bos.toByteArray());
  115. } catch (IOException e) {
  116. throw new ParameterException(e);
  117. }
  118. return count;
  119. }
  120. public int decode(byte[] b) throws ParameterException {
  121. ByteArrayInputStream bis = new ByteArrayInputStream(b);
  122. return this.decode(bis);
  123. }
  124. protected int decode(ByteArrayInputStream bis) throws ParameterException {
  125. if (logger.isDebugEnabled()) {
  126. logger.debug("[" + this.getClass().getSimpleName() + "]Decoding header");
  127. }
  128. int count = decodeHeader(bis);
  129. if (logger.isDebugEnabled()) {
  130. logger.debug("[" + this.getClass().getSimpleName() + "]Decoding header, read count: " + count);
  131. logger.debug("[" + this.getClass().getSimpleName() + "]Decoding body");
  132. }
  133. count += decodeDigits(bis);
  134. if (logger.isDebugEnabled()) {
  135. logger.debug("[" + this.getClass().getSimpleName() + "]Decoding digits, read count: " + count);
  136. }
  137. return count;
  138. }
  139. /**
  140. * This method is used in encode. Encodes digits part. This is
  141. * because
  142. *
  143. * @param bos
  144. * - where digits will be encoded
  145. * @return - number of bytes encoded
  146. *
  147. */
  148. public int encodeDigits(ByteArrayOutputStream bos) {
  149. boolean isOdd = this.oddFlag == _FLAG_ODD;
  150. byte b = 0;
  151. int count = (!isOdd) ? address.length() : address.length() - 1;
  152. int bytesCount = 0;
  153. for (int i = 0; i < count - 1; i += 2) {
  154. String ds1 = address.substring(i, i + 1);
  155. String ds2 = address.substring(i + 1, i + 2);
  156. int d1 = Integer.parseInt(ds1, 16);
  157. int d2 = Integer.parseInt(ds2, 16);
  158. b = (byte) (d2 << 4 | d1);
  159. bos.write(b);
  160. bytesCount++;
  161. }
  162. if (isOdd) {
  163. String ds1 = address.substring(count, count + 1);
  164. int d = Integer.parseInt(ds1);
  165. b = (byte) (d & 0x0f);
  166. bos.write(b);
  167. bytesCount++;
  168. }
  169. return bytesCount;
  170. }
  171. /**
  172. * This method is used in constructor that takes byte[] or
  173. * ByteArrayInputStream as parameter. Decodes digits part. Stores result in
  174. * digits field, where digits[0] holds most significant digit. This is
  175. * because
  176. *
  177. * @param bis
  178. * @return - number of bytes reads throws IllegalArgumentException - thrown
  179. * if read error is encountered.
  180. * @throws ParameterException
  181. * - thrown if read error is encountered.
  182. */
  183. public int decodeDigits(ByteArrayInputStream bis) throws ParameterException {
  184. if (bis.available() == 0) {
  185. throw new ParameterException("No more data to read.");
  186. }
  187. // FIXME: we could spare time by passing length arg - or getting it from
  188. // bis??
  189. int count = 0;
  190. address = "";
  191. int b = 0;
  192. while (bis.available() - 1 > 0) {
  193. b = (byte) bis.read();
  194. int d1 = b & 0x0f;
  195. int d2 = (b & 0xf0) >> 4;
  196. address += Integer.toHexString(d1) + Integer.toHexString(d2);
  197. }
  198. b = bis.read() & 0xff;
  199. address += Integer.toHexString((b & 0x0f));
  200. if (oddFlag != 1) {
  201. address += Integer.toHexString((b & 0xf0) >> 4);
  202. }
  203. return count;
  204. }
  205. /**
  206. * This method is used in constructor that takes byte[] or
  207. * ByteArrayInputStream as parameter. Decodes header part (its 1 or 2 bytes
  208. * usually.) Default implemetnation decodes header of one byte - where most
  209. * significant bit is O/E indicator and bits 7-1 are NAI. This method should
  210. * be over
  211. *
  212. * @param bis
  213. * @return - number of bytes reads
  214. * @throws ParameterException
  215. * - thrown if read error is encountered.
  216. */
  217. public int decodeHeader(ByteArrayInputStream bis) throws ParameterException {
  218. if (bis.available() == 0) {
  219. throw new ParameterException("No more data to read.");
  220. }
  221. int b = bis.read() & 0xff;
  222. this.oddFlag = (b & 0x80) >> 7;
  223. this.setTypeOfNetworkIdentification((b >> 4));
  224. this.setNetworkIdentificationPlan(b);
  225. return 1;
  226. }
  227. /**
  228. * This method is used in encode method. It encodes header part (1 or
  229. * 2 bytes usually.)
  230. *
  231. * @param bis
  232. * @return - number of bytes encoded.
  233. */
  234. public int encodeHeader(ByteArrayOutputStream bos) {
  235. int b = this.networkIdentificationPlan & 0x0F;
  236. b |= (this.typeOfNetworkIdentification & 0x07) << 4;
  237. // Even is 000000000 == 0
  238. boolean isOdd = this.oddFlag == _FLAG_ODD;
  239. if (isOdd)
  240. b |= 0x80;
  241. bos.write(b);
  242. return 1;
  243. }
  244. public int getTypeOfNetworkIdentification() {
  245. return typeOfNetworkIdentification;
  246. }
  247. public void setTypeOfNetworkIdentification(int typeOfNetworkIdentification) {
  248. this.typeOfNetworkIdentification = typeOfNetworkIdentification & 0x07;
  249. }
  250. public int getNetworkIdentificationPlan() {
  251. return networkIdentificationPlan;
  252. }
  253. public void setNetworkIdentificationPlan(int networkIdentificationPlan) {
  254. this.networkIdentificationPlan = networkIdentificationPlan & 0x0F;
  255. }
  256. public String getAddress() {
  257. return address;
  258. }
  259. public void setAddress(String address) {
  260. this.address = address;
  261. oddFlag = this.address.length() % 2;
  262. }
  263. public boolean isOddFlag() {
  264. return oddFlag == _FLAG_ODD;
  265. }
  266. public int getCode() {
  267. return _PARAMETER_CODE;
  268. }
  269. }