/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/parameter/OPCListImpl.java

http://mobicents.googlecode.com/ · Java · 153 lines · 92 code · 29 blank · 32 comment · 6 complexity · c45cedf38c7269c9c77317b7b2087ee7 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.m3ua.impl.parameter;
  23. import java.util.Arrays;
  24. import javolution.xml.XMLFormat;
  25. import javolution.xml.XMLSerializable;
  26. import javolution.xml.stream.XMLStreamException;
  27. import org.mobicents.protocols.ss7.m3ua.parameter.OPCList;
  28. import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
  29. /**
  30. *
  31. * @author amit bhayani
  32. *
  33. */
  34. public class OPCListImpl extends ParameterImpl implements OPCList, XMLSerializable {
  35. private static final String OPC = "opc";
  36. private static final String MASK = "mask";
  37. private static final String ARRAY_SIZE = "size";
  38. private byte[] value;
  39. private int[] pointCodes;
  40. private short[] masks;
  41. public OPCListImpl(){
  42. this.tag = Parameter.Originating_Point_Code_List;
  43. }
  44. protected OPCListImpl(byte[] value) {
  45. this.tag = Parameter.Originating_Point_Code_List;
  46. int count = 0;
  47. int arrSize = 0;
  48. pointCodes = new int[(value.length / 4)];
  49. masks = new short[(value.length / 4)];
  50. while (count < value.length) {
  51. masks[arrSize] = value[count++];
  52. pointCodes[arrSize] = 0;
  53. pointCodes[arrSize] |= value[count++] & 0xFF;
  54. pointCodes[arrSize] <<= 8;
  55. pointCodes[arrSize] |= value[count++] & 0xFF;
  56. pointCodes[arrSize] <<= 8;
  57. pointCodes[arrSize++] |= value[count++] & 0xFF;
  58. }
  59. this.value = value;
  60. }
  61. protected OPCListImpl(int[] pointCodes, short[] masks) {
  62. this.tag = Parameter.Originating_Point_Code_List;
  63. this.pointCodes = pointCodes;
  64. this.masks = masks;
  65. encode();
  66. }
  67. private void encode() {
  68. // create byte array taking into account data, point codes and
  69. // indicators;
  70. this.value = new byte[(pointCodes.length * 4)];
  71. int count = 0;
  72. int arrSize = 0;
  73. // encode routing context
  74. while (count < value.length) {
  75. value[count++] = (byte) (masks[arrSize]);
  76. value[count++] = (byte) (pointCodes[arrSize] >>> 16);
  77. value[count++] = (byte) (pointCodes[arrSize] >>> 8);
  78. value[count++] = (byte) (pointCodes[arrSize++]);
  79. }
  80. }
  81. @Override
  82. protected byte[] getValue() {
  83. return this.value;
  84. }
  85. public short[] getMasks() {
  86. return this.masks;
  87. }
  88. public int[] getPointCodes() {
  89. return this.pointCodes;
  90. }
  91. @Override
  92. public String toString() {
  93. return String.format("OPCList pointCode=%s mask=%s", Arrays.toString(this.pointCodes),
  94. Arrays.toString(this.masks));
  95. }
  96. /**
  97. * XML Serialization/Deserialization
  98. */
  99. protected static final XMLFormat<OPCListImpl> RC_XML = new XMLFormat<OPCListImpl>(OPCListImpl.class) {
  100. @Override
  101. public void read(javolution.xml.XMLFormat.InputElement xml, OPCListImpl opc) throws XMLStreamException {
  102. int size = xml.getAttribute(ARRAY_SIZE).toInt();
  103. opc.pointCodes = new int[size];
  104. opc.masks = new short[size];
  105. for (int i = 0; i < opc.pointCodes.length; i++) {
  106. opc.pointCodes[i] = xml.get(OPC);
  107. }
  108. for (int i = 0; i < opc.masks.length; i++) {
  109. opc.masks[i] = xml.get(MASK);
  110. }
  111. opc.encode();
  112. }
  113. @Override
  114. public void write(OPCListImpl opc, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
  115. xml.setAttribute(ARRAY_SIZE, opc.pointCodes.length);
  116. for (int i : opc.pointCodes) {
  117. xml.add(i, OPC);
  118. }
  119. for (short s : opc.masks) {
  120. xml.add(s, MASK);
  121. }
  122. }
  123. };
  124. }