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

http://mobicents.googlecode.com/ · Java · 97 lines · 27 code · 15 blank · 55 comment · 2 complexity · 1df8954aed889f9329ca9267116f326c 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.nio.ByteBuffer;
  24. import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
  25. /**
  26. * @author amit bhayani
  27. * @author kulikov
  28. */
  29. public abstract class ParameterImpl implements Parameter {
  30. protected volatile short tag;
  31. protected volatile short length;
  32. public short getTag() {
  33. return tag;
  34. }
  35. protected abstract byte[] getValue();
  36. // public void encode(OutputStream out) throws IOException {
  37. // // obtain encoded value
  38. // byte[] value = getValue();
  39. //
  40. // // encode tag
  41. // out.write((byte) (tag >> 8));
  42. // out.write((byte) (tag));
  43. //
  44. // // encode length including value, tag and length field itself
  45. // length = (short) (value.length + 4);
  46. //
  47. // out.write((byte) (length >> 8));
  48. // out.write((byte) (length));
  49. //
  50. // // encode value
  51. // out.write(value);
  52. // }
  53. public void write(ByteBuffer buffer) {
  54. // obtain encoded value
  55. byte[] value = getValue();
  56. // encode tag
  57. buffer.put((byte) (tag >> 8));
  58. buffer.put((byte) (tag));
  59. // encode length including value, tag and length field itself
  60. length = (short) (value.length + 4);
  61. buffer.put((byte) (length >> 8));
  62. buffer.put((byte) (length));
  63. // encode value
  64. buffer.put(value);
  65. /*
  66. * The total length of a parameter (including Tag, Parameter Length, and
  67. * Value fields) MUST be a multiple of 4 octets. If the length of the
  68. * parameter is not a multiple of 4 octets, the sender pads the
  69. * Parameter at the end (i.e., after the Parameter Value field) with all
  70. * zero octets. The length of the padding is NOT included in the
  71. * parameter length field. A sender MUST NOT pad with more than 3
  72. * octets. The receiver MUST ignore the padding octets.
  73. */
  74. int remainder = (4 - length % 4);
  75. if (remainder < 4) {
  76. while (remainder > 0) {
  77. buffer.put((byte) 0x00);
  78. remainder--;
  79. }
  80. }
  81. }
  82. }