PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

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