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

http://mobicents.googlecode.com/ · Java · 184 lines · 122 code · 36 blank · 26 comment · 15 complexity · ba9b0de98bb38efe1b7a7f959171e98c 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.util.Arrays;
  25. import org.mobicents.protocols.asn.AsnException;
  26. import org.mobicents.protocols.asn.AsnInputStream;
  27. import org.mobicents.protocols.asn.AsnOutputStream;
  28. import org.mobicents.protocols.asn.Tag;
  29. import org.mobicents.protocols.ss7.map.api.MAPException;
  30. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  31. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  32. import org.mobicents.protocols.ss7.map.api.primitives.LMSI;
  33. /**
  34. *
  35. * @author sergey vetyutnev
  36. *
  37. */
  38. public class LMSIImpl implements LMSI, MAPAsnPrimitive {
  39. private byte[] data;
  40. public LMSIImpl() {
  41. }
  42. public LMSIImpl(byte[] data) {
  43. this.data = data;
  44. }
  45. public int getTag() {
  46. return Tag.STRING_OCTET;
  47. }
  48. @Override
  49. public int getTagClass() {
  50. return Tag.CLASS_UNIVERSAL;
  51. }
  52. @Override
  53. public boolean getIsPrimitive() {
  54. return true;
  55. }
  56. @Override
  57. public byte[] getData() {
  58. return this.data;
  59. }
  60. @Override
  61. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  62. try {
  63. int length = ansIS.readLength();
  64. this._decode(ansIS, length);
  65. } catch (IOException e) {
  66. throw new MAPParsingComponentException("IOException when decoding LMSI: " + e.getMessage(), e,
  67. MAPParsingComponentExceptionReason.MistypedParameter);
  68. }
  69. }
  70. @Override
  71. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  72. try {
  73. this._decode(ansIS, length);
  74. } catch (IOException e) {
  75. throw new MAPParsingComponentException("IOException when decoding LMSI: " + e.getMessage(), e,
  76. MAPParsingComponentExceptionReason.MistypedParameter);
  77. }
  78. }
  79. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
  80. if (length != 4)
  81. throw new MAPParsingComponentException("Error decoding LMSI: the LMSI field must contain 4 octets. Contains: " + length,
  82. MAPParsingComponentExceptionReason.MistypedParameter);
  83. try {
  84. this.data = new byte[4];
  85. ansIS.read(this.data);
  86. } catch (IOException e) {
  87. throw new MAPParsingComponentException("IOException when decoding LMSI: " + e.getMessage(), e,
  88. MAPParsingComponentExceptionReason.MistypedParameter);
  89. }
  90. }
  91. @Override
  92. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  93. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
  94. }
  95. @Override
  96. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  97. try {
  98. asnOs.writeTag(tagClass, true, tag);
  99. int pos = asnOs.StartContentDefiniteLength();
  100. this.encodeData(asnOs);
  101. asnOs.FinalizeContent(pos);
  102. } catch (AsnException e) {
  103. throw new MAPException("AsnException when encoding LMSI: " + e.getMessage(), e);
  104. }
  105. }
  106. @Override
  107. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  108. if (this.data == null)
  109. throw new MAPException("Error while encoding the LMSI: data is not defined");
  110. if (this.data.length != 4)
  111. throw new MAPException("Error while encoding the LMSI: data field length must equale 4");
  112. asnOs.write(this.data);
  113. }
  114. @Override
  115. public String toString() {
  116. return "LMSI [Data= " + this.printDataArr() + "]";
  117. }
  118. @Override
  119. public int hashCode() {
  120. final int prime = 31;
  121. int result = 1;
  122. result = prime * result + Arrays.hashCode(data);
  123. return result;
  124. }
  125. @Override
  126. public boolean equals(Object obj) {
  127. if (this == obj)
  128. return true;
  129. if (obj == null)
  130. return false;
  131. if (getClass() != obj.getClass())
  132. return false;
  133. LMSIImpl other = (LMSIImpl) obj;
  134. if (!Arrays.equals(data, other.data))
  135. return false;
  136. return true;
  137. }
  138. private String printDataArr() {
  139. StringBuilder sb = new StringBuilder();
  140. if( this.data!=null ) {
  141. for( int b : this.data ) {
  142. sb.append(b);
  143. sb.append(" ");
  144. }
  145. }
  146. return sb.toString();
  147. }
  148. }