PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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