/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/encoding/AlphabetEncoding.java
Java | 134 lines | 54 code | 11 blank | 69 comment | 13 complexity | 8c49fac263e4f790966095f6cd2204d8 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.smpp.encoding; 24 25import java.io.UnsupportedEncodingException; 26 27import org.mobicents.protocols.smpp.SMPPRuntimeException; 28 29/** 30 * SMS Alphabet to Java String mapping interface. Implementations of this 31 * interface convert Java Unicode strings into a series of bytes representing 32 * the String in a particular SMS alphabet. 33 */ 34public class AlphabetEncoding extends AbstractMessageEncoding<String> { 35 private String charset; 36 37 /** 38 * Create a new alphabet encoding. 39 * @param dcs The data coding value to be used for this encoding. 40 */ 41 protected AlphabetEncoding(int dcs) { 42 super(dcs); 43 } 44 45 /** 46 * Get the character set in use by this alpabet encoding (if any). 47 * @return The character set in use by this alphabet encoding. This method 48 * may return <code>null</code> if the implementation is not using a JVM- 49 * supported character set. 50 */ 51 public String getCharset() { 52 return charset; 53 } 54 55 /** 56 * Convert SMS message text into a Java String. Implementations of this 57 * method <b>must </b> support decoding <code>null</code>. In such cases, 58 * the String "" will be returned. 59 */ 60 public String decode(byte[] data) { 61 if (data != null) { 62 return decode(data, 0, data.length); 63 } else { 64 return ""; 65 } 66 } 67 68 /** 69 * Convert SMS message text into a Java String. 70 * @param data The bytes to decode. 71 * @param offset The offset within the data to begin decoding characters. 72 * @param length The number of bytes to decode to characters. 73 * @throws NullPointerException If <tt>data</tt> is <tt>null</tt>. 74 */ 75 public String decode(byte[] data, int offset, int length) { 76 try { 77 if (data != null) { 78 return new String(data, offset, length, charset); 79 } else { 80 throw new NullPointerException("Data cannot be null"); 81 } 82 } catch (UnsupportedEncodingException x) { 83 // Shouldn't happen - setCharset should have detected this. 84 throw new RuntimeException(); 85 } 86 } 87 88 /** 89 * Convert a Java String into SMS message text. Implementations of this 90 * method <b>must </b> support encoding a <code>null</code> string. In 91 * such cases, a byte array of length 0 will be returned. 92 */ 93 public byte[] encode(String string) { 94 try { 95 if (string != null) { 96 return string.getBytes(charset); 97 } 98 } catch (UnsupportedEncodingException x) { 99 // Shouldn't happen - setCharset should have detected this. 100 throw new RuntimeException(); 101 } 102 return new byte[0]; 103 } 104 105 /** 106 * Get the number of bytes a particular string would encode as on the 107 * wire. 108 * @return The number of bytes <code>string</code> would encode to. 109 */ 110 public int getEncodedSize(String string) { 111 if (string != null) { 112 return encode(string).length; 113 } else { 114 return 0; 115 } 116 } 117 118 /** 119 * Set the charset of this alphabet encoding. Sub-classes can use this 120 * to create new instances of alphabet encoding for character sets that 121 * are supported by the JVM. This method can only be called once. 122 * Subsequent calls will throw a RuntimeException. 123 * @param charset The character set to use for encoding and decoding. 124 * @throws UnsupportedEncodingException If the JVM does not support the 125 * specified character set. 126 */ 127 protected void setCharset(String charset) throws UnsupportedEncodingException { 128 if (this.charset != null) { 129 throw new SMPPRuntimeException("Cannot change charset."); 130 } 131 new String("probe").getBytes(charset); 132 this.charset = charset; 133 } 134}