PageRenderTime 43ms CodeModel.GetById 19ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 120 lines | 69 code | 15 blank | 36 comment | 0 complexity | 903fef9649ee85a5b0c25f7864035120 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;
 24
 25import java.nio.charset.Charset;
 26import java.nio.charset.CharsetDecoder;
 27import java.nio.charset.CharsetEncoder;
 28
 29/**
 30 * <p>
 31 * The encoding/decoding of 7 bits characters in USSD strings is used doing
 32 * GSMCharset.
 33 * </p>
 34 * <br/>
 35 * <p>
 36 * For further details look at GSM 03.38 Specs
 37 * </p>
 38 * 
 39 * @author amit bhayani
 40 * @author sergey vetyutnev
 41 * 
 42 */
 43public class GSMCharset extends Charset {
 44
 45	protected static final float averageCharsPerByte = 8 / 7f;
 46	protected static final float maxCharsPerByte = 2f;
 47
 48	protected static final float averageBytesPerChar = 2f;
 49	protected static final float maxBytesPerChar = 2f;
 50
 51	protected static final int BUFFER_SIZE = 256;
 52
 53	public static final byte ESCAPE = 0x1B;
 54	
 55	protected int[] mainTable; 
 56	protected int[] extensionTable; 
 57
 58	public GSMCharset(String canonicalName, String[] aliases) {
 59		this(canonicalName, aliases, BYTE_TO_CHAR_DefaultAlphabet, BYTE_TO_CHAR_DefaultAlphabetExtentionTable);
 60	}
 61
 62	public GSMCharset(String canonicalName, String[] aliases, int[] mainTable, int[] extentionTable) {
 63		super(canonicalName, aliases);
 64		
 65		this.mainTable = mainTable; 
 66		this.extensionTable = extentionTable; 
 67	}
 68
 69	@Override
 70	public boolean contains(Charset cs) {
 71		return this.getClass().isInstance(cs);
 72	}
 73
 74	@Override
 75	public CharsetDecoder newDecoder() {
 76		return new GSMCharsetDecoder(this, averageCharsPerByte, maxCharsPerByte);
 77	}
 78
 79	@Override
 80	public CharsetEncoder newEncoder() {
 81		return new GSMCharsetEncoder(this, averageBytesPerChar, maxBytesPerChar);
 82	}
 83
 84	// Look at http://www.unicode.org/Public/MAPPINGS/ETSI/GSM0338.TXT
 85	static final int[] BYTE_TO_CHAR_DefaultAlphabet = { 
 86		0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC, 
 87		0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5, 
 88		0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8, 
 89		0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9, 
 90		0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027, 
 91		0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, 
 92		0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 
 93		0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 
 94		0x00A1, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 
 95		0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 
 96		0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 
 97		0x0058, 0x0059, 0x005A, 0x00C4, 0x00D6, 0x00D1, 0x00DC, 0x00A7, 
 98		0x00BF, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 
 99		0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, 
100		0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 
101		0x0078, 0x0079, 0x007A, 0x00E4, 0x00F6, 0x00F1, 0x00FC, 0x00E0 };
102	static final int[] BYTE_TO_CHAR_DefaultAlphabetExtentionTable = { 
103		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
104		0x0000, 0x0000, 0x000C, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
105		0x0000, 0x0000, 0x0000, 0x0000, 0x005E, 0x0000, 0x0000, 0x0000, 
106		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
107		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
108		0x007B, 0x007D, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x005C, 
109		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
110		0x0000, 0x0000, 0x0000, 0x0000, 0x005B, 0x007E, 0x005D, 0x0000, 
111		0x007C, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
112		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
113		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
114		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
115		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x20AC, 0x0000, 0x0000, 
116		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
117		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
118		0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
119   };
120}