PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/parameter/RoutingContextImpl.java

http://mobicents.googlecode.com/
Java | 132 lines | 80 code | 20 blank | 32 comment | 4 complexity | 9cc1c58a58c17d33f9d58de71b4e83ec 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.util.Arrays;
 26
 27import javolution.xml.XMLFormat;
 28import javolution.xml.XMLSerializable;
 29import javolution.xml.stream.XMLStreamException;
 30
 31import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
 32import org.mobicents.protocols.ss7.m3ua.parameter.RoutingContext;
 33
 34/**
 35 * 
 36 * @author amit bhayani
 37 * 
 38 */
 39public class RoutingContextImpl extends ParameterImpl implements RoutingContext, XMLSerializable {
 40
 41	private static final String ARRAY_SIZE = "size";
 42	private static final String ROUTING_CONTEXT = "rc";
 43
 44	private long[] rcs = null;
 45	private byte[] value;
 46	
 47	public RoutingContextImpl(){
 48		this.tag = Parameter.Routing_Context;
 49	}
 50
 51	protected RoutingContextImpl(byte[] value) {
 52		this.tag = Parameter.Routing_Context;
 53
 54		int count = 0;
 55		int arrSize = 0;
 56		rcs = new long[(value.length / 4)];
 57
 58		while (count < value.length) {
 59			rcs[arrSize] = 0;
 60			rcs[arrSize] |= value[count++] & 0xFF;
 61			rcs[arrSize] <<= 8;
 62			rcs[arrSize] |= value[count++] & 0xFF;
 63			rcs[arrSize] <<= 8;
 64			rcs[arrSize] |= value[count++] & 0xFF;
 65			rcs[arrSize] <<= 8;
 66			rcs[arrSize++] |= value[count++] & 0xFF;
 67		}
 68
 69		this.value = value;
 70	}
 71
 72	protected RoutingContextImpl(long[] routingcontexts) {
 73		this.tag = Parameter.Routing_Context;
 74		rcs = routingcontexts;
 75		encode();
 76	}
 77
 78	private void encode() {
 79		// create byte array taking into account data, point codes and
 80		// indicators;
 81		this.value = new byte[(rcs.length * 4)];
 82		int count = 0;
 83		int arrSize = 0;
 84		// encode routing context
 85		while (count < value.length) {
 86			value[count++] = (byte) (rcs[arrSize] >>> 24);
 87			value[count++] = (byte) (rcs[arrSize] >>> 16);
 88			value[count++] = (byte) (rcs[arrSize] >>> 8);
 89			value[count++] = (byte) (rcs[arrSize++]);
 90		}
 91	}
 92
 93	public long[] getRoutingContexts() {
 94		return this.rcs;
 95	}
 96
 97	@Override
 98	protected byte[] getValue() {
 99		return value;
100	}
101
102	@Override
103	public String toString() {
104		return String.format("RoutingContext rc=%s", Arrays.toString(rcs));
105	}
106
107	/**
108	 * XML Serialization/Deserialization
109	 */
110	protected static final XMLFormat<RoutingContextImpl> RC_XML = new XMLFormat<RoutingContextImpl>(
111			RoutingContextImpl.class) {
112
113		@Override
114		public void read(javolution.xml.XMLFormat.InputElement xml, RoutingContextImpl rc) throws XMLStreamException {
115			int size = xml.getAttribute(ARRAY_SIZE).toInt();
116			rc.rcs = new long[size];
117			size = 0;
118			while (xml.hasNext()) {
119				rc.rcs[size++] = xml.get(ROUTING_CONTEXT, Long.class);
120			}
121			rc.encode();
122		}
123
124		@Override
125		public void write(RoutingContextImpl rc, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
126			xml.setAttribute(ARRAY_SIZE, rc.rcs.length);
127			for (Long l : rc.rcs) {
128				xml.add(l, ROUTING_CONTEXT, Long.class);
129			}
130		}
131	};
132}