PageRenderTime 46ms CodeModel.GetById 16ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://mobicents.googlecode.com/
Java | 175 lines | 124 code | 19 blank | 32 comment | 13 complexity | e54061cdc39f01e4674d0eb057016ac0 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.io.InputStream;
 27import java.io.OutputStream;
 28
 29import org.mobicents.protocols.ss7.map.api.MAPException;
 30import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 31import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 32
 33/**
 34 * 
 35 * @author amit bhayani
 36 * @author sergey vetyutnev
 37 * 
 38 */
 39public abstract class TbcdString implements MAPAsnPrimitive {
 40
 41	protected static int DIGIT_1_MASK = 0x0F;
 42	protected static int DIGIT_2_MASK = 0xF0;
 43
 44	public static String decodeString(InputStream ansIS, int length) throws IOException, MAPParsingComponentException {
 45		StringBuilder s = new StringBuilder();
 46		for (int i1 = 0; i1 < length; i1++) {
 47			int b = ansIS.read();
 48
 49			int digit1 = (b & DIGIT_1_MASK);
 50			if (digit1 == 15) {
 51				// this is mask
 52			} else {
 53				s.append(decodeNumber(digit1));
 54			}
 55
 56			int digit2 = ((b & DIGIT_2_MASK) >> 4);
 57			if (digit2 == 15) {
 58				// this is mask
 59			} else {
 60				s.append(decodeNumber(digit2));
 61			}
 62		}
 63
 64		return s.toString();
 65	}
 66
 67	public static void encodeString(OutputStream asnOs, String data) throws MAPException {
 68		char[] chars = data.toCharArray();
 69		for (int i = 0; i < chars.length; i = i + 2) {
 70			char a = chars[i];
 71
 72			int digit1 = encodeNumber(a);
 73			int digit2;
 74			if ((i + 1) == chars.length) {
 75				// add the filler instead
 76				digit2 = 15;
 77			} else {
 78				char b = chars[i + 1];
 79				digit2 = encodeNumber(b);
 80			}
 81
 82			int digit = (digit2 << 4) | digit1;
 83
 84			try {
 85				asnOs.write(digit);
 86			} catch (IOException e) {
 87				throw new MAPException("Error when encoding TbcdString: " + e.getMessage(), e);
 88			}
 89		}
 90		
 91	}
 92	
 93	protected static int encodeNumber(char c) throws MAPException {
 94		switch (c) {
 95		case '0':
 96			return 0;
 97		case '1':
 98			return 1;
 99		case '2':
100			return 2;
101		case '3':
102			return 3;
103		case '4':
104			return 4;
105		case '5':
106			return 5;
107		case '6':
108			return 6;
109		case '7':
110			return 7;
111		case '8':
112			return 8;
113		case '9':
114			return 9;
115		case '*':
116			return 10;
117		case '#':
118			return 11;
119		case 'a':
120			return 12;
121		case 'b':
122			return 13;
123		case 'c':
124			return 14;
125		default:
126			throw new MAPException(
127					"char should be between 0 - 9, *, #, a, b, c for Telephony Binary Coded Decimal String. Received "
128							+ c);
129
130		}
131	}
132
133	protected static char decodeNumber(int i) throws MAPParsingComponentException {
134		switch (i) {
135		case 0:
136			return '0';
137		case 1:
138			return '1';
139		case 2:
140			return '2';
141		case 3:
142			return '3';
143		case 4:
144			return '4';
145		case 5:
146			return '5';
147		case 6:
148			return '6';
149		case 7:
150			return '7';
151		case 8:
152			return '8';
153		case 9:
154			return '9';
155		case 10:
156			return '*';
157		case 11:
158			return '#';
159		case 12:
160			return 'a';
161		case 13:
162			return 'b';
163		case 14:
164			return 'c';
165			// case 15:
166			// return 'd';
167		default:
168			throw new MAPParsingComponentException(
169					"Integer should be between 0 - 15 for Telephony Binary Coded Decimal String. Received "
170							+ i, MAPParsingComponentExceptionReason.MistypedParameter);
171
172		}
173	}
174}
175