code: private String escapeSpecialCharacters(String s) {
          StringBuilder sb = new StringBuilder();
          boolean needEscape = s.length() == 0; // escape empty strings
          for (int a = 0; a < s.length(); a++) {
              char c = s.charAt(a);
              if (c == '\n' || c == '\r' || c == 0 || c == COPY_COMMAND_DELIMITER.charAt(0)) {
                  needEscape = true;
              }
              if (c == ESCAPE || c == QUOTE) {
                  needEscape = true;
                  sb.append(ESCAPE);
              }
              sb.append(c);
          }
          if (needEscape) {
              return QUOTE + sb.toString() + QUOTE;
          }
          return s;
      }
docs: this follows the PostgreSQL rules at https://www.postgresql.org/docs/current/static/sql-copy.html#AEN77663
  "If the value contains the delimiter character, the QUOTE character, the NULL string, a carriage return,
  or line feed character, then the whole value is prefixed and suffixed by the QUOTE character,
  and any occurrence within the value of a QUOTE character or the ESCAPE character is preceded
  by the escape character."