StringSegment.java

  1. package fr.sii.ogham.sms.splitter;

  2. import java.io.UnsupportedEncodingException;
  3. import java.nio.charset.Charset;

  4. /**
  5.  * A segment initialized from a string. The charset used to encode the string to
  6.  * a byte array is also tracked in order to indicate which encoding was used.
  7.  *
  8.  * @author AurĂ©lien Baudet
  9.  *
  10.  */
  11. public class StringSegment implements Segment {
  12.     private final String message;
  13.     private final String charsetName;
  14.     private final byte[] bytes;

  15.     /**
  16.      * Initializes the segment with a message. The default charset of the
  17.      * platform is used.
  18.      *
  19.      * WARNING: As the default charset for the platform is used, it may lead to
  20.      * inconsistencies.
  21.      *
  22.      * @param message
  23.      *            the text for the segment
  24.      */
  25.     public StringSegment(String message) {
  26.         super();
  27.         this.message = message;
  28.         charsetName = Charset.defaultCharset().name();
  29.         bytes = message.getBytes(Charset.defaultCharset());
  30.     }

  31.     /**
  32.      * Initializes the segment with a message. The message string is encoded to
  33.      * a byte array using the provided charset.
  34.      *
  35.      * @param message
  36.      *            the text for the segment
  37.      * @param charset
  38.      *            the charset to use for encoding the string to a byte array
  39.      */
  40.     public StringSegment(String message, Charset charset) {
  41.         super();
  42.         this.message = message;
  43.         charsetName = charset.name();
  44.         bytes = message.getBytes(charset);
  45.     }

  46.     /**
  47.      * Initializes the segment with a message. The message string is encoded to
  48.      * a byte array using the provided charset name.
  49.      *
  50.      * @param message
  51.      *            the text for the segment
  52.      * @param charsetName
  53.      *            the charset name to use for encoding the string to a byte
  54.      *            array
  55.      * @throws UnsupportedEncodingException
  56.      *             when the charset could not be found
  57.      */
  58.     public StringSegment(String message, String charsetName) throws UnsupportedEncodingException {
  59.         super();
  60.         this.message = message;
  61.         this.charsetName = charsetName;
  62.         bytes = message.getBytes(charsetName);
  63.     }

  64.     @Override
  65.     public byte[] getBytes() {
  66.         return bytes;
  67.     }

  68.     /**
  69.      * @return the original message
  70.      */
  71.     public String getMessage() {
  72.         return message;
  73.     }

  74.     /**
  75.      * @return the charset name used to encode the original message string to a
  76.      *         byte array
  77.      */
  78.     public String getCharsetName() {
  79.         return charsetName;
  80.     }

  81. }