| 1 | package fr.sii.ogham.sms.splitter; | |
| 2 | ||
| 3 | import java.io.UnsupportedEncodingException; | |
| 4 | import java.nio.charset.Charset; | |
| 5 | ||
| 6 | /** | |
| 7 | * A segment initialized from a string. The charset used to encode the string to | |
| 8 | * a byte array is also tracked in order to indicate which encoding was used. | |
| 9 | * | |
| 10 | * @author Aurélien Baudet | |
| 11 | * | |
| 12 | */ | |
| 13 | public class StringSegment implements Segment { | |
| 14 | private final String message; | |
| 15 | private final String charsetName; | |
| 16 | private final byte[] bytes; | |
| 17 | ||
| 18 | /** | |
| 19 | * Initializes the segment with a message. The default charset of the | |
| 20 | * platform is used. | |
| 21 | * | |
| 22 | * WARNING: As the default charset for the platform is used, it may lead to | |
| 23 | * inconsistencies. | |
| 24 | * | |
| 25 | * @param message | |
| 26 | * the text for the segment | |
| 27 | */ | |
| 28 | public StringSegment(String message) { | |
| 29 | super(); | |
| 30 | this.message = message; | |
| 31 | charsetName = Charset.defaultCharset().name(); | |
| 32 | bytes = message.getBytes(Charset.defaultCharset()); | |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * Initializes the segment with a message. The message string is encoded to | |
| 37 | * a byte array using the provided charset. | |
| 38 | * | |
| 39 | * @param message | |
| 40 | * the text for the segment | |
| 41 | * @param charset | |
| 42 | * the charset to use for encoding the string to a byte array | |
| 43 | */ | |
| 44 | public StringSegment(String message, Charset charset) { | |
| 45 | super(); | |
| 46 | this.message = message; | |
| 47 | charsetName = charset.name(); | |
| 48 | bytes = message.getBytes(charset); | |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * Initializes the segment with a message. The message string is encoded to | |
| 53 | * a byte array using the provided charset name. | |
| 54 | * | |
| 55 | * @param message | |
| 56 | * the text for the segment | |
| 57 | * @param charsetName | |
| 58 | * the charset name to use for encoding the string to a byte | |
| 59 | * array | |
| 60 | * @throws UnsupportedEncodingException | |
| 61 | * when the charset could not be found | |
| 62 | */ | |
| 63 | public StringSegment(String message, String charsetName) throws UnsupportedEncodingException { | |
| 64 | super(); | |
| 65 | this.message = message; | |
| 66 | this.charsetName = charsetName; | |
| 67 | bytes = message.getBytes(charsetName); | |
| 68 | } | |
| 69 | ||
| 70 | @Override | |
| 71 | public byte[] getBytes() { | |
| 72 |
1
1. getBytes : replaced return value with null for fr/sii/ogham/sms/splitter/StringSegment::getBytes → NO_COVERAGE |
return bytes; |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * @return the original message | |
| 77 | */ | |
| 78 | public String getMessage() { | |
| 79 |
1
1. getMessage : replaced return value with "" for fr/sii/ogham/sms/splitter/StringSegment::getMessage → NO_COVERAGE |
return message; |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * @return the charset name used to encode the original message string to a | |
| 84 | * byte array | |
| 85 | */ | |
| 86 | public String getCharsetName() { | |
| 87 |
1
1. getCharsetName : replaced return value with "" for fr/sii/ogham/sms/splitter/StringSegment::getCharsetName → NO_COVERAGE |
return charsetName; |
| 88 | } | |
| 89 | ||
| 90 | } | |
Mutations | ||
| 72 |
1.1 |
|
| 79 |
1.1 |
|
| 87 |
1.1 |