code: public static void quote(StringBuffer buf, String s)
      {
          synchronized(buf)
          {
              buf.append('"');
              for (int i=0;i<s.length();i++)
              {
                  char c = s.charAt(i);
                  if (c=='"')
                  {   
                      buf.append("\\\"");
                      continue;
                  }
                  if (c=='\\')
                  {   
                      buf.append("\\\\");
                      continue;
                  }
                  buf.append(c);
                  continue;
              }
              buf.append('"');
          }
      }
docs: Quote a string into a StringBuffer.
  @param buf The StringBuffer
  @param s The String to quote.