PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 176 lines | 105 code | 31 blank | 40 comment | 39 complexity | 62f9bf6bcab4c06364ff2ea328470630 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.ByteBuffer;
 26import java.nio.CharBuffer;
 27import java.nio.charset.Charset;
 28import java.nio.charset.CharsetEncoder;
 29import java.nio.charset.CoderResult;
 30import java.util.BitSet;
 31
 32/**
 33 * 
 34 * @author amit bhayani
 35 * @author sergey vetyutnev
 36 * 
 37 */
 38public class GSMCharsetEncoder extends CharsetEncoder {
 39
 40	private	int bitpos = 0;
 41	private	int carryOver;
 42	private	GSMCharset cs;
 43	private GSMCharsetEncodingData encodingData;
 44
 45	// The mask to check if corresponding bit in read byte is 1 or 0 and hence
 46	// store it i BitSet accordingly
 47	byte[] mask = new byte[] { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };
 48
 49	// BitSet to hold the bits of passed char to be encoded
 50	BitSet bitSet = new BitSet();
 51
 52	static final byte ESCAPE = 0x1B;
 53
 54	protected GSMCharsetEncoder(Charset cs, float averageBytesPerChar,
 55			float maxBytesPerChar) {
 56		super(cs, averageBytesPerChar, maxBytesPerChar);
 57		implReset();
 58		this.cs = (GSMCharset) cs;
 59
 60		if (encodingData != null)
 61			encodingData.totalSeptetCount = 0;
 62	}
 63
 64	public void setGSMCharsetEncodingData(GSMCharsetEncodingData encodingData) {
 65		this.encodingData = encodingData;
 66	}
 67
 68	public GSMCharsetEncodingData getGSMCharsetEncodingData() {
 69		return this.encodingData;
 70	}
 71
 72	@Override
 73	protected void implReset() {
 74		bitpos = 0;
 75		carryOver = 0;
 76		bitSet.clear();
 77	}
 78
 79	/**
 80	 * TODO :
 81	 */
 82	@Override
 83	protected CoderResult implFlush(ByteBuffer out) {
 84
 85		if (!out.hasRemaining()) {
 86			return CoderResult.OVERFLOW;
 87		}
 88		return CoderResult.UNDERFLOW;
 89	}
 90
 91	byte rawData = 0;
 92
 93	@Override
 94	protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
 95
 96		if (this.encodingData != null && this.encodingData.leadingBuffer != null) {
 97			int septetCount = (this.encodingData.leadingBuffer.length * 8 + 6) / 7;
 98			bitpos = septetCount % 8;
 99			this.encodingData.totalSeptetCount = septetCount; 
100			for (int ind = 0; ind < this.encodingData.leadingBuffer.length; ind++) {
101				out.put(this.encodingData.leadingBuffer[ind]);
102			}
103		}
104		
105		char lastChar = ' ';
106		while (in.hasRemaining()) {
107
108			// Read the first char
109			char c = in.get();
110			lastChar = c;
111
112			boolean found = false;
113			// searching a char in the main character table
114			for (int i = 0; i < this.cs.mainTable.length; i++) {
115				if (this.cs.mainTable[i] == c) {
116					found = true;
117					this.putByte(i, out);
118					break;
119				}
120			}
121			
122			// searching a char in the extension character table
123			if (!found && this.cs.extensionTable != null) {
124				for (int i = 0; i < this.cs.mainTable.length; i++) {
125					if (this.cs.extensionTable[i] == c) {
126						found = true;
127						this.putByte(GSMCharsetEncoder.ESCAPE, out);
128						this.putByte(i, out);
129						break;
130					}
131				}
132			}
133
134			if (!found) {
135				// found no suitable symbol - encode a space char
136				this.putByte(0x20, out);
137			}
138		}
139
140		if (bitpos != 0) {
141			// USSD: replace 7-bit pad with <CR>
142			if (this.encodingData != null && this.encodingData.ussdStyleEncoding && bitpos == 7)
143				carryOver |= 0x1A;
144
145			// writing a carryOver data
146			out.put((byte) carryOver);
147		} else {
148
149			// USSD: adding extra <CR> if the last symbol is <CR> and no padding
150			if (this.encodingData != null && this.encodingData.ussdStyleEncoding && lastChar == '\r')
151				out.put((byte) 0x0D);
152		}
153
154		return CoderResult.UNDERFLOW;
155	}
156
157	private void putByte(int data, ByteBuffer out) {
158
159		if (bitpos == 0) {
160			carryOver = data;
161		} else {
162			int i1 = data << (8 - bitpos);
163			out.put((byte) (i1 | carryOver));
164			carryOver = data >>> bitpos;
165		}
166
167		bitpos++;
168		if (bitpos == 8) {
169			bitpos = 0;
170		}
171
172		if (this.encodingData != null)
173			this.encodingData.totalSeptetCount++;
174	}
175}
176