/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/util/PacketDecoderImpl.java

http://mobicents.googlecode.com/ · Java · 151 lines · 102 code · 24 blank · 25 comment · 2 complexity · d7ec9ccbbe7949f7aff7033dc272ff13 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.util;
  23. import java.text.ParseException;
  24. import org.mobicents.protocols.smpp.Address;
  25. import org.mobicents.protocols.smpp.ErrorAddress;
  26. import org.mobicents.protocols.smpp.message.SMPPProtocolException;
  27. /**
  28. * Implementation of the packet decoder.
  29. * @version $Id: PacketDecoderImpl.java 457 2009-01-15 17:37:42Z orank $
  30. */
  31. public class PacketDecoderImpl implements PacketDecoder {
  32. private static final SMPPDateFormat DATE_FORMAT = new SMPPDateFormat();
  33. private byte[] bytes;
  34. private int pos;
  35. public PacketDecoderImpl() {
  36. }
  37. public PacketDecoderImpl(byte[] bytes) {
  38. this.bytes = bytes;
  39. }
  40. public PacketDecoderImpl(byte[] bytes, int parsePosition) {
  41. this.bytes = bytes;
  42. this.pos = parsePosition;
  43. }
  44. public byte[] getBytes() {
  45. return bytes;
  46. }
  47. public void setBytes(byte[] bytes) {
  48. this.bytes = bytes;
  49. }
  50. public int getParsePosition() {
  51. return pos;
  52. }
  53. public void setParsePosition(int pos) {
  54. this.pos = pos;
  55. }
  56. public int getAvailableBytes() {
  57. return bytes.length - pos;
  58. }
  59. public byte readByte() {
  60. int index = pos;
  61. pos++;
  62. return bytes[index];
  63. }
  64. public String readCString() {
  65. String str = SMPPIO.readCString(bytes, pos);
  66. pos += str.length() + 1;
  67. return str;
  68. }
  69. public String readString(int length) {
  70. String str = SMPPIO.readString(bytes, pos, length);
  71. pos += str.length();
  72. return str;
  73. }
  74. public int readUInt1() {
  75. int value = SMPPIO.readUInt1(bytes, pos);
  76. pos++;
  77. return value;
  78. }
  79. public int readUInt2() {
  80. int value = SMPPIO.readUInt2(bytes, pos);
  81. pos += 2;
  82. return value;
  83. }
  84. public long readUInt4() {
  85. long value = SMPPIO.readUInt4(bytes, pos);
  86. pos += 4;
  87. return value;
  88. }
  89. public long readInt8() {
  90. long value = SMPPIO.readInt8(bytes, pos);
  91. pos += 8;
  92. return value;
  93. }
  94. public Address readAddress() {
  95. Address address = new Address();
  96. address.readFrom(this);
  97. return address;
  98. }
  99. public ErrorAddress readErrorAddress() {
  100. ErrorAddress errorAddress = new ErrorAddress();
  101. errorAddress.readFrom(this);
  102. return errorAddress;
  103. }
  104. public SMPPDate readDate() {
  105. SMPPDate date = null;
  106. String str = null;
  107. try {
  108. str = readCString();
  109. if (str.length() > 0) {
  110. date = (SMPPDate) DATE_FORMAT.parseObject(str);
  111. }
  112. } catch (ParseException x) {
  113. throw new SMPPProtocolException("Cannot parse date value: " + str, x);
  114. }
  115. return date;
  116. }
  117. public byte[] readBytes(int length) {
  118. int startIndex = pos;
  119. if (startIndex + length > bytes.length) {
  120. throw new ArrayIndexOutOfBoundsException(startIndex + length);
  121. }
  122. byte[] copy = new byte[length];
  123. System.arraycopy(bytes, startIndex, copy, 0, length);
  124. pos += length;
  125. return copy;
  126. }
  127. }