code: public static final String escape(String s) {
          StringBuilder buf = new StringBuilder();
          for (int i=0; i<s.length(); ) {
              int c = Character.codePointAt(s, i);
              i += UTF16.getCharCount(c);
              if (c >= ' ' && c <= 0x007F) {
                  if (c == '\\') {
                      buf.append("\\\\"); // That is, "\\"
                  } else {
                      buf.append((char)c);
                  }
              } else {
                  boolean four = c <= 0xFFFF;
                  buf.append(four ? "\\u" : "\\U");
                  buf.append(hex(c, four ? 4 : 8));
              }
          }
          return buf.toString();
      }
docs: Convert characters outside the range U+0020 to U+007F to
  Unicode escapes, and convert backslash to a double backslash.