|
16 | 16 | import java.math.BigInteger;
|
17 | 17 | import java.math.RoundingMode;
|
18 | 18 | import java.nio.ByteBuffer;
|
| 19 | +import java.nio.charset.Charset; |
19 | 20 | import java.nio.file.Files;
|
20 | 21 | import java.nio.file.LinkOption;
|
21 | 22 | import java.nio.file.Path;
|
@@ -2979,6 +2980,57 @@ else if (encoding.equals("utf32")) {
|
2979 | 2980 | }
|
2980 | 2981 | }
|
2981 | 2982 |
|
| 2983 | + public static String decode8(SixModelObject buf, String csName, ThreadContext tc) { |
| 2984 | + ByteBuffer bb; |
| 2985 | + if (buf instanceof VMArrayInstance_i8) { |
| 2986 | + VMArrayInstance_i8 bufi8 = (VMArrayInstance_i8)buf; |
| 2987 | + bb = bufi8.slots != null |
| 2988 | + ? ByteBuffer.wrap(bufi8.slots, bufi8.start, bufi8.elems) |
| 2989 | + : ByteBuffer.allocate(0); |
| 2990 | + } |
| 2991 | + else { |
| 2992 | + int n = (int)buf.elems(tc); |
| 2993 | + bb = ByteBuffer.allocate(n); |
| 2994 | + for (int i = 0; i < n; i++) { |
| 2995 | + buf.at_pos_native(tc, i); |
| 2996 | + bb.put((byte)tc.native_i); |
| 2997 | + } |
| 2998 | + } |
| 2999 | + return Charset.forName(csName).decode(bb).toString(); |
| 3000 | + } |
| 3001 | + public static String decode(SixModelObject buf, String encoding, ThreadContext tc) { |
| 3002 | + if (encoding.equals("utf8")) { |
| 3003 | + return decode8(buf, "UTF-8", tc); |
| 3004 | + } |
| 3005 | + else if (encoding.equals("ascii")) { |
| 3006 | + return decode8(buf, "US-ASCII", tc); |
| 3007 | + } |
| 3008 | + else if (encoding.equals("iso-8859-1")) { |
| 3009 | + return decode8(buf, "ISO-8859-1", tc); |
| 3010 | + } |
| 3011 | + else if (encoding.equals("utf16")) { |
| 3012 | + int n = (int)buf.elems(tc); |
| 3013 | + StringBuilder sb = new StringBuilder(n); |
| 3014 | + for (int i = 0; i < n; i++) { |
| 3015 | + buf.at_pos_native(tc, i); |
| 3016 | + sb.append((char)tc.native_i); |
| 3017 | + } |
| 3018 | + return sb.toString(); |
| 3019 | + } |
| 3020 | + else if (encoding.equals("utf32")) { |
| 3021 | + int n = (int)buf.elems(tc); |
| 3022 | + StringBuilder sb = new StringBuilder(n); |
| 3023 | + for (int i = 0; i < n; i++) { |
| 3024 | + buf.at_pos_native(tc, i); |
| 3025 | + sb.appendCodePoint((int)tc.native_i); |
| 3026 | + } |
| 3027 | + return sb.toString(); |
| 3028 | + } |
| 3029 | + else { |
| 3030 | + throw ExceptionHandling.dieInternal(tc, "Unknown encoding '" + encoding + "'"); |
| 3031 | + } |
| 3032 | + } |
| 3033 | + |
2982 | 3034 | private static final int CCLASS_ANY = 65535;
|
2983 | 3035 | private static final int CCLASS_UPPERCASE = 1;
|
2984 | 3036 | private static final int CCLASS_LOWERCASE = 2;
|
|
0 commit comments