PageRenderTime 14ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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