PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/encoding/AlphabetEncoding.java

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