/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/message/param/IntegerParamDescriptor.java

http://mobicents.googlecode.com/ · Java · 93 lines · 55 code · 10 blank · 28 comment · 3 complexity · 4f50ee8693599ff93b05f63e39c9df17 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.smpp.message.param;
  23. import java.io.IOException;
  24. import org.mobicents.protocols.smpp.util.PacketDecoder;
  25. import org.mobicents.protocols.smpp.util.PacketEncoder;
  26. /**
  27. * Mandatory parameter descriptor for an integer.
  28. * @version $Id: IntegerParamDescriptor.java 457 2009-01-15 17:37:42Z orank $
  29. */
  30. public class IntegerParamDescriptor extends AbstractDescriptor {
  31. private static final long serialVersionUID = 2L;
  32. /**
  33. * Number of bytes to read for the integer. Default is 1.
  34. */
  35. private int length;
  36. public IntegerParamDescriptor(int length) {
  37. this.length = length;
  38. }
  39. public int getLengthSpecifier() {
  40. return -1;
  41. }
  42. public int sizeOf(Object obj) {
  43. return length;
  44. }
  45. public void writeObject(Object obj, PacketEncoder encoder) throws IOException {
  46. if (!(obj instanceof Number)) {
  47. throw new IllegalArgumentException("Invalid object type.");
  48. }
  49. long value = ((Number) obj).longValue();
  50. switch (length) {
  51. case 8:
  52. encoder.writeInt8(value);
  53. break;
  54. case 4:
  55. encoder.writeUInt4(value);
  56. break;
  57. case 2:
  58. encoder.writeUInt2((int) value);
  59. break;
  60. default:
  61. encoder.writeUInt1((int) value);
  62. break;
  63. }
  64. }
  65. public Object readObject(PacketDecoder decoder, int length) {
  66. Number value;
  67. switch (this.length) {
  68. case 8:
  69. value = new Long(decoder.readInt8());
  70. break;
  71. case 4:
  72. value = new Long(decoder.readUInt4());
  73. break;
  74. case 2:
  75. value = new Integer(decoder.readUInt2());
  76. break;
  77. default:
  78. value = new Integer(decoder.readUInt1());
  79. break;
  80. }
  81. return value;
  82. }
  83. }