public static String decode(String text) {
        if (text == null)
            return null;
        StringBuilder buf = new StringBuilder(text.length());
        final int limit = text.length();
        for (int i = 0; i < limit; i++) {
            char ch = text.charAt(i);

            // Handle unescaped characters
            if (ch != '\\') {
                buf.append(ch);
                continue;
            }

            // Get next char
            if (++i >= limit)
                throw new IllegalArgumentException(
                        "illegal trailing '\\' in encoded string");
            ch = text.charAt(i);

            // Check for backslash escape
            if (ch == '\\') {
                buf.append(ch);
                continue;
            }

            // Must be unicode escape
            if (ch != 'u')
                throw new IllegalArgumentException(
                        "illegal escape sequence '\\" + ch
                                + "' in encoded string");

            // Decode hex value
            int value = 0;
            for (int j = 0; j < 4; j++) {
                if (++i >= limit)
                    throw new IllegalArgumentException(
                            "illegal truncated '\\u' escape sequence in encoded string");
                int nibble = Character.digit(text.charAt(i), 16);
                if (nibble == -1) {
                    throw new IllegalArgumentException(
                            "illegal escape sequence '"
                                    + text.substring(i - j - 2, i - j + 4)
                                    + "' in encoded string");
                }
                // assert nibble >= 0 && nibble <= 0xf;
                value = (value << 4) | nibble;
            }

            // Append decodec character
            buf.append((char) value);
        }
        return buf.toString();
    }