code: public static void appendString(final StringBuffer buf, final String s) {
          buf.append('\"');
          for (int i = 0; i < s.length(); ++i) {
              char c = s.charAt(i);
              if (c == '\n') {
                  buf.append("\\n");
              } else if (c == '\r') {
                  buf.append("\\r");
              } else if (c == '\\') {
                  buf.append("\\\\");
              } else if (c == '"') {
                  buf.append("\\\"");
              } else if (c < 0x20 || c > 0x7f) {
                  buf.append("\\u");
                  if (c < 0x10) {
                      buf.append("000");
                  } else if (c < 0x100) {
                      buf.append("00");
                  } else if (c < 0x1000) {
                      buf.append('0');
                  }
                  buf.append(Integer.toString(c, 16));
              } else {
                  buf.append(c);
              }
          }
          buf.append('\"');
      }
docs: Appends a quoted string to a given buffer.
  
  @param buf
  the buffer where the string must be added.
  @param s
  the string to be added.