57bd19bce03d9eccd3d5c5ce0147d7a6c6cfa2df
[packages/precise/mcollective.git] / lib / mcollective / vendor / json / java / src / json / ext / StringEncoder.java
1 package json.ext;
2
3 import org.jruby.exceptions.RaiseException;
4 import org.jruby.runtime.ThreadContext;
5 import org.jruby.util.ByteList;
6
7 /**
8  * An encoder that reads from the given source and outputs its representation
9  * to another ByteList. The source string is fully checked for UTF-8 validity,
10  * and throws a GeneratorError if any problem is found.
11  */
12 final class StringEncoder extends ByteListTranscoder {
13     private final boolean asciiOnly;
14
15     // Escaped characters will reuse this array, to avoid new allocations
16     // or appending them byte-by-byte
17     private final byte[] aux =
18         new byte[] {/* First unicode character */
19                     '\\', 'u', 0, 0, 0, 0,
20                     /* Second unicode character (for surrogate pairs) */
21                     '\\', 'u', 0, 0, 0, 0,
22                     /* "\X" characters */
23                     '\\', 0};
24     // offsets on the array above
25     private static final int ESCAPE_UNI1_OFFSET = 0;
26     private static final int ESCAPE_UNI2_OFFSET = ESCAPE_UNI1_OFFSET + 6;
27     private static final int ESCAPE_CHAR_OFFSET = ESCAPE_UNI2_OFFSET + 6;
28     /** Array used for code point decomposition in surrogates */
29     private final char[] utf16 = new char[2];
30
31     private static final byte[] HEX =
32             new byte[] {'0', '1', '2', '3', '4', '5', '6', '7',
33                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
34
35     StringEncoder(ThreadContext context, boolean asciiOnly) {
36         super(context);
37         this.asciiOnly = asciiOnly;
38     }
39
40     void encode(ByteList src, ByteList out) {
41         init(src, out);
42         append('"');
43         while (hasNext()) {
44             handleChar(readUtf8Char());
45         }
46         quoteStop(pos);
47         append('"');
48     }
49
50     private void handleChar(int c) {
51         switch (c) {
52         case '"':
53         case '\\':
54             escapeChar((char)c);
55             break;
56         case '\n':
57             escapeChar('n');
58             break;
59         case '\r':
60             escapeChar('r');
61             break;
62         case '\t':
63             escapeChar('t');
64             break;
65         case '\f':
66             escapeChar('f');
67             break;
68         case '\b':
69             escapeChar('b');
70             break;
71         default:
72             if (c >= 0x20 && c <= 0x7f ||
73                     (c >= 0x80 && !asciiOnly)) {
74                 quoteStart();
75             } else {
76                 quoteStop(charStart);
77                 escapeUtf8Char(c);
78             }
79         }
80     }
81
82     private void escapeChar(char c) {
83         quoteStop(charStart);
84         aux[ESCAPE_CHAR_OFFSET + 1] = (byte)c;
85         append(aux, ESCAPE_CHAR_OFFSET, 2);
86     }
87
88     private void escapeUtf8Char(int codePoint) {
89         int numChars = Character.toChars(codePoint, utf16, 0);
90         escapeCodeUnit(utf16[0], ESCAPE_UNI1_OFFSET + 2);
91         if (numChars > 1) escapeCodeUnit(utf16[1], ESCAPE_UNI2_OFFSET + 2);
92         append(aux, ESCAPE_UNI1_OFFSET, 6 * numChars);
93     }
94
95     private void escapeCodeUnit(char c, int auxOffset) {
96         for (int i = 0; i < 4; i++) {
97             aux[auxOffset + i] = HEX[(c >>> (12 - 4 * i)) & 0xf];
98         }
99     }
100
101     @Override
102     protected RaiseException invalidUtf8() {
103          return Utils.newException(context, Utils.M_GENERATOR_ERROR,
104                  "source sequence is illegal/malformed utf-8");
105     }
106 }