PageRenderTime 35ms CodeModel.GetById 1ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/LMSIImpl.java

http://mobicents.googlecode.com/
Java | 184 lines | 122 code | 36 blank | 26 comment | 15 complexity | ba9b0de98bb38efe1b7a7f959171e98c 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.map.primitives;
 24
 25import java.io.IOException;
 26import java.util.Arrays;
 27
 28import org.mobicents.protocols.asn.AsnException;
 29import org.mobicents.protocols.asn.AsnInputStream;
 30import org.mobicents.protocols.asn.AsnOutputStream;
 31import org.mobicents.protocols.asn.Tag;
 32import org.mobicents.protocols.ss7.map.api.MAPException;
 33import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 34import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 35import org.mobicents.protocols.ss7.map.api.primitives.LMSI;
 36
 37/**
 38 * 
 39 * @author sergey vetyutnev
 40 * 
 41 */
 42public class LMSIImpl implements LMSI, MAPAsnPrimitive {
 43	
 44	private byte[] data;
 45
 46	
 47	public LMSIImpl() {
 48	}
 49	
 50	public LMSIImpl(byte[] data) {
 51		this.data = data;
 52	}
 53
 54
 55	public int getTag() {
 56		return Tag.STRING_OCTET;
 57	}
 58
 59	@Override
 60	public int getTagClass() {
 61		return Tag.CLASS_UNIVERSAL;
 62	}
 63
 64	@Override
 65	public boolean getIsPrimitive() {
 66		return true;
 67	}
 68
 69	@Override
 70	public byte[] getData() {
 71		return this.data;
 72	}
 73	
 74
 75	@Override
 76	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 77
 78		try {
 79			int length = ansIS.readLength();
 80			this._decode(ansIS, length);
 81		} catch (IOException e) {
 82			throw new MAPParsingComponentException("IOException when decoding LMSI: " + e.getMessage(), e,
 83					MAPParsingComponentExceptionReason.MistypedParameter);
 84		}
 85	}
 86
 87	@Override
 88	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
 89
 90		try {
 91			this._decode(ansIS, length);
 92		} catch (IOException e) {
 93			throw new MAPParsingComponentException("IOException when decoding LMSI: " + e.getMessage(), e,
 94					MAPParsingComponentExceptionReason.MistypedParameter);
 95		}
 96	}
 97
 98	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
 99		
100		if (length != 4)
101			throw new MAPParsingComponentException("Error decoding LMSI: the LMSI field must contain 4 octets. Contains: " + length,
102					MAPParsingComponentExceptionReason.MistypedParameter);
103
104		try {
105			this.data = new byte[4];
106			ansIS.read(this.data);
107		} catch (IOException e) {
108			throw new MAPParsingComponentException("IOException when decoding LMSI: " + e.getMessage(), e,
109					MAPParsingComponentExceptionReason.MistypedParameter);
110		}
111	}
112	
113	@Override
114	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
115		
116		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
117	}
118
119	@Override
120	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
121		
122		try {
123			asnOs.writeTag(tagClass, true, tag);
124			int pos = asnOs.StartContentDefiniteLength();
125			this.encodeData(asnOs);
126			asnOs.FinalizeContent(pos);
127		} catch (AsnException e) {
128			throw new MAPException("AsnException when encoding LMSI: " + e.getMessage(), e);
129		}
130	}
131
132	@Override
133	public void encodeData(AsnOutputStream asnOs) throws MAPException {
134
135		if (this.data == null)
136			throw new MAPException("Error while encoding the LMSI: data is not defined");
137
138		if (this.data.length != 4)
139			throw new MAPException("Error while encoding the LMSI: data field length must equale 4");
140
141		asnOs.write(this.data);
142	}
143
144	@Override
145	public String toString() {
146		return "LMSI [Data= " + this.printDataArr() + "]";
147	}
148	
149	@Override
150	public int hashCode() {
151		final int prime = 31;
152		int result = 1;
153		result = prime * result + Arrays.hashCode(data);
154		return result;
155	}
156
157	@Override
158	public boolean equals(Object obj) {
159		if (this == obj)
160			return true;
161		if (obj == null)
162			return false;
163		if (getClass() != obj.getClass())
164			return false;
165		LMSIImpl other = (LMSIImpl) obj;
166		if (!Arrays.equals(data, other.data))
167			return false;
168		return true;
169	}
170
171	private String printDataArr() {
172		StringBuilder sb = new StringBuilder();
173		if( this.data!=null ) {
174			for( int b : this.data ) {
175				sb.append(b);
176				sb.append(" ");
177			}
178		}
179		
180		return sb.toString();
181	}
182}
183
184