package ie.omk.smpp.util;

import ie.omk.smpp.SMPPRuntimeException;

import java.io.UnsupportedEncodingException;

/**
 * SMS Alphabet to Java String mapping interface. Implementations of this
 * interface convert Java Unicode strings into a series of bytes representing
 * the String in a particular SMS alphabet.
 */
public class AlphabetEncoding extends MessageEncoding {
    
    private static final String BAD_IMPLEMENTATION =
        "Missing charset in implementation of AlphabetEncoding "
        + AlphabetEncoding.class.getName();
        
    private String charset;
    
    protected AlphabetEncoding(int dcs) {
        super(dcs);
    }

    /**
     * Set the charset of this alphabet encoding. Sub-classes can use this
     * to create new instances of alphabet encoding for character sets that
     * are supported by the JVM.
     * @param charset The character set to use for encoding and decoding.
     * @throws UnsupportedEncodingException If the JVM does not support the
     * specified character set.
     */
    protected void setCharset(String charset) throws UnsupportedEncodingException {
        new String("probe").getBytes(charset);
        this.charset = charset;
    }
    
    /**
     * Get the character set in use by this alpabet encoding (if any).
     * @return The character set in use by this alphabet encoding. This method
     * may return <code>null</code> if the implementation is not using a JVM-
     * supported character set.
     */
    public String getCharset() {
        return charset;
    }

    /**
     * Convert SMS message text into a Java String. Implementations of this
     * method <b>must </b> support decoding <code>null</code>. In such cases,
     * the String "" will be returned.
     */
    public String decodeString(byte[] data) {
        return decodeString(data, 0, data.length);
    }

    /**
     * Convert SMS message text into a Java String. Implementations of this
     * method <b>must </b> support decoding <code>null</code>. In such cases,
     * the String "" will be returned.
     */
    public String decodeString(byte[] data, int offset, int length) {
        if (charset == null) {
            throw new SMPPRuntimeException(BAD_IMPLEMENTATION);
        }
        try {
            if (data != null) {
                return new String(data, offset, length, charset);
            }
        } catch (UnsupportedEncodingException x) {
            // Will already have been detected by the constructor.
        }
        return "";
    }

    /**
     * Convert a Java String into SMS message text. Implementations of this
     * method <b>must </b> support encoding a <code>null</code> string. In
     * such cases, a byte array of length 0 will be returned.
     */
    public byte[] encodeString(String string) {
        if (charset == null) {
            throw new SMPPRuntimeException(BAD_IMPLEMENTATION);
        }
        try {
            if (string != null) {
                return string.getBytes(charset);
            }
        } catch (java.io.UnsupportedEncodingException x) {
            // Will already have been detected by the constructor.
        }
        return new byte[0];
    }
}


@@ -24,50 +24,8 @@
 l;%0A%0A
-import ie.omk.smpp.SMPPRuntimeException;%0A%0A
 impo
@@ -326,21 +326,16 @@
 oding %7B%0A
-    %0A
     priv
@@ -342,820 +342,218 @@
 ate 
-static final String BAD_IMPLEMENTATION =%0A        %22Missing charset in implementation of AlphabetEncoding %22%0A        + AlphabetEncoding.class.getName();%0A        %0A    private String charset;%0A    %0A    protected AlphabetEncoding(int dcs) %7B%0A        super(dcs);%0A    %7D%0A%0A    /**%0A     * Set the charset of this alphabet encoding. Sub-classes can use this%0A     * to create new instances of alphabet encoding for character sets that%0A     * are supported by the JVM.%0A     * @param charset The character set to use for encoding and decoding.%0A     * @throws UnsupportedEncodingException If the JVM does not supp
+String charset;%0A%0A    /**%0A     * Create a new alphabet encoding.%0A     * @param dcs The data coding value to be used f
 or
-t
  th
-e%0A     * specified character set.%0A     */%0A    protected void setCharset(String charset) throws UnsupportedEncodingException %7B%0A        new String(%22probe%22).getBytes(charset);%0A        this.charset = charset
+is encoding.%0A     */%0A    protected AlphabetEncoding(int dcs) %7B%0A        super(dcs)
 ;%0A    %7D%0A
     
@@ -548,20 +548,16 @@
 ;%0A    %7D%0A
-    
 %0A    /**
@@ -1485,113 +1485,8 @@
 ) %7B%0A
-        if (charset == null) %7B%0A            throw new SMPPRuntimeException(BAD_IMPLEMENTATION);%0A        %7D%0A
     
@@ -1673,39 +1673,58 @@
      // 
-Will already
+Shouldn't happen - setCharset should
  have 
-been 
 detected
@@ -1724,35 +1724,63 @@
 etected 
-by the constructor.
+this.%0A            throw new RuntimeException();
 %0A       
@@ -2089,113 +2089,8 @@
 ) %7B%0A
-        if (charset == null) %7B%0A            throw new SMPPRuntimeException(BAD_IMPLEMENTATION);%0A        %7D%0A
     
@@ -2213,24 +2213,16 @@
  catch (
-java.io.
 Unsuppor
@@ -2266,103 +2266,849 @@
  // 
-Will already have been detected by the constructor.%0A        %7D%0A        return new byte%5B0%5D;%0A    %7D%0A
+Shouldn't happen - setCharset should have detected this.%0A            throw new RuntimeException();%0A        %7D%0A        return new byte%5B0%5D;%0A    %7D%0A%0A    /**%0A     * Set the charset of this alphabet encoding. Sub-classes can use this%0A     * to create new instances of alphabet encoding for character sets that%0A     * are supported by the JVM. This method can only be called once%0A     * @param charset The character set to use for encoding and decoding.%0A     * @throws UnsupportedEncodingException If the JVM does not support the%0A     * specified character set.%0A     */%0A    protected void setCharset(String charset) throws UnsupportedEncodingException %7B%0A        if (this.charset != null) %7B%0A            throw new RuntimeException(%22Cannot change charset.%22);%0A        %7D%0A        new String(%22probe%22).getBytes(charset);%0A        this.charset = charset;%0A    
 %7D%0A
+%7D
 %0A

Remove the throw of SMPPRuntimeException - simply allow the NullPointerException to happen.