3 import java
.io
.UnsupportedEncodingException
;
6 * EncodedString is a wrapper class which wraps a String, but replaces all
7 * methods using a string representation of a character encoding with
8 * ones using the CharacterEncoding class, thereby guaranteeing
9 * validity and eliminating the need to throw any exceptions.
11 * @author Kevin Scaldeferri
15 public class EncodedString
{
16 private String myString
;
17 private CharacterEncoding myEncoding
;
19 public static String
bytesToString(byte[] bytes
, CharacterEncoding enc
) {
22 ret
= new String(bytes
, enc
.getEncodingString());
23 } catch (UnsupportedEncodingException e
) {
24 throw new IllegalArgumentException("Unknown Encoding");
25 } catch (NullPointerException e
) {
31 public static String
bytesToString(byte[] bytes
, int offset
,
32 int length
, CharacterEncoding enc
) {
35 ret
= new String(bytes
, offset
, length
, enc
.getEncodingString());
36 } catch (UnsupportedEncodingException e
) {
37 throw new IllegalArgumentException("Unknown Encoding");
38 } catch (NullPointerException e
) {
44 public static byte[] getBytes(String string
, CharacterEncoding enc
) {
47 ret
= string
.getBytes(enc
.getEncodingString());
48 } catch (UnsupportedEncodingException e
) {
49 throw new IllegalArgumentException("Unknown Encoding");
50 } catch (NullPointerException e
) {
56 public EncodedString(String string
, CharacterEncoding enc
) {
61 public EncodedString(byte[] bytes
, CharacterEncoding enc
) {
62 myString
= bytesToString(bytes
, enc
);
66 public EncodedString(byte[] bytes
, int offset
,
67 int length
, CharacterEncoding enc
)
69 myString
= bytesToString(bytes
, offset
, length
, enc
);
73 public byte[] getBytes() { return getBytes(myString
, myEncoding
); }
74 public String
toString() { return myString
; }