01: package uk.org.ponder.stringutil;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.OutputStreamWriter;
05:
06: import uk.org.ponder.util.UniversalRuntimeException;
07:
08: // stub implementation simply passes through to SUN implementation
09: // until time or necessity to make efficient implementation like
10: // ByteToCharUTF8
11: public class CharToByteUTF8 {
12: /**
13: * Converts the supplied string into a byte array holding its UTF-8 encoded
14: * form. This method call is currently very inefficient and simply passes
15: * through to the SUN implementation.
16: *
17: * @param toconvert A string to be converted to UTF-8.
18: * @return A byte array holding a UTF-8 encoded version of the input string.
19: */
20: public static byte[] convert(String toconvert) {
21: ByteArrayOutputStream baos = new ByteArrayOutputStream();
22: try {
23: OutputStreamWriter osw = new OutputStreamWriter(baos,
24: "UTF8");
25: osw.write(toconvert);
26: osw.close();
27: } catch (Exception uee) {
28: throw UniversalRuntimeException.accumulate(uee,
29: "Critical error: UTF-8 encoder not present");
30: }
31: return baos.toByteArray();
32: }
33: }
|