<commit_before>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];
    }
}
<commit_msg>Remove the throw of SMPPRuntimeException - simply allow the NullPointerException to happen.<commit_after>package ie.omk.smpp.util;

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 String charset;

    /**
     * Create a new alphabet encoding.
     * @param dcs The data coding value to be used for this encoding.
     */
    protected AlphabetEncoding(int dcs) {
        super(dcs);
    }

    /**
     * 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) {
        try {
            if (data != null) {
                return new String(data, offset, length, charset);
            }
        } catch (UnsupportedEncodingException x) {
            // Shouldn't happen - setCharset should have detected this.
            throw new RuntimeException();
        }
        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) {
        try {
            if (string != null) {
                return string.getBytes(charset);
            }
        } catch (UnsupportedEncodingException x) {
            // Shouldn't happen - setCharset should have detected this.
            throw new RuntimeException();
        }
        return new byte[0];
    }

    /**
     * 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. This method can only be called once
     * @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 {
        if (this.charset != null) {
            throw new RuntimeException("Cannot change charset.");
        }
        new String("probe").getBytes(charset);
        this.charset = charset;
    }
}
{'fraction_non_alphanumeric': 0.08234928527035426, 'fraction_numerical': 0.0009322560596643878, 'mean_word_length': 2.342159916926272, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970893', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1625, 'n_words': 652}