All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restcomm.protocols.ss7.commonapp.datacoding.GSMCharsetEncoder Maven / Gradle / Ivy

There is a newer version: 10.0.36
Show newest version
/*
 * Mobius Software LTD
 * Copyright 2019, Mobius Software LTD and individual contributors
 * by the @authors tag.
 *
 * This program is free software: you can redistribute it and/or modify
 * under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see 
 */

package org.restcomm.protocols.ss7.commonapp.datacoding;

import java.nio.charset.CharacterCodingException;
import java.util.BitSet;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 *
 * @author amit bhayani
 * @author sergey vetyutnev
 * @author yulianoifa
 *
 */
public class GSMCharsetEncoder {

    private int bitpos = 0;
    private int carryOver;
    private GSMCharset cs;
    private GSMCharsetEncodingData encodingData;

    // The mask to check if corresponding bit in read byte is 1 or 0 and hence
    // store it i BitSet accordingly
    byte[] mask = new byte[] { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 };

    // BitSet to hold the bits of passed char to be encoded
    BitSet bitSet = new BitSet();

    static final byte ESCAPE = 0x1B;

    protected GSMCharsetEncoder(GSMCharset cs) {
        this.cs = cs;
    }

    public void setGSMCharsetEncodingData(GSMCharsetEncodingData encodingData) {
        this.encodingData = encodingData;
    }

    public GSMCharsetEncodingData getGSMCharsetEncodingData() {
        return this.encodingData;
    }

    byte rawData = 0;

    protected void encodeLoop(String in, ByteBuf out) {
        if (this.encodingData != null && this.encodingData.leadingBytes != null) {
        	if (this.encodingData.encodingStyle != Gsm7EncodingStyle.bit8_smpp_style) {
                int septetCount = (this.encodingData.leadingBytes * 8 + 6) / 7;
                bitpos = septetCount % 8;
                this.encodingData.totalSeptetCount = septetCount;
            }
        }

        char lastChar = ' ';
        for(int j=0;j
                if (this.encodingData != null && this.encodingData.encodingStyle == Gsm7EncodingStyle.bit7_ussd_style
                        && bitpos == 7)
                    carryOver |= 0x1A;

                // writing a carryOver data
                out.writeByte((byte) carryOver);
            } else {
                // USSD: adding extra  if the last symbol is  and no padding
                if (this.encodingData != null && this.encodingData.encodingStyle == Gsm7EncodingStyle.bit7_ussd_style
                        && lastChar == '\r')
                    out.writeByte((byte) 0x0D);
            }
        }
    }

    private void putByte(int data, ByteBuf out) {
        if (this.encodingData != null && this.encodingData.encodingStyle == Gsm7EncodingStyle.bit8_smpp_style) {
            out.writeByte((byte) data);
        } else {

            if (bitpos == 0) {
                carryOver = data;
            } else {
                int i1 = data << (8 - bitpos);
                out.writeByte((byte) (i1 | carryOver));
                carryOver = data >>> bitpos;
            }

            bitpos++;
            if (bitpos == 8) {
                bitpos = 0;
            }

            if (this.encodingData != null)
                this.encodingData.totalSeptetCount++;
        }
    }
    
    public final ByteBuf encode(String in) throws CharacterCodingException {
    	ByteBuf out = Unpooled.buffer();
    	if (in.length() == 0)
    		return out;
            
    	encodeLoop(in, out);
    	return out;
    }  
    
    public final void encode(String in,ByteBuf out) throws CharacterCodingException {
    	if (in.length() == 0)
    		return;
            
    	encodeLoop(in, out);    	
    }  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy