code: public static byte[] escape(byte[] auth, boolean quote) {
          int escapeCount = 0;
  
          for (int i = 0; i < auth.length; i++) {
              if (auth[i] == '"' || auth[i] == '\\') {
                  escapeCount++;
              }
          }
  
          if (escapeCount > 0 || quote) {
              byte[] escapedAuth = new byte[auth.length + escapeCount + (quote ? 2 : 0)];
              int index = quote ? 1 : 0;
              for (int i = 0; i < auth.length; i++) {
                  if (auth[i] == '"' || auth[i] == '\\') {
                      escapedAuth[index++] = '\\';
                  }
                  escapedAuth[index++] = auth[i];
              }
  
              if (quote) {
                  escapedAuth[0] = '"';
                  escapedAuth[escapedAuth.length - 1] = '"';
              }
  
              auth = escapedAuth;
          }
          return auth;
      }
docs: Properly escapes an authorization string. The string can be quoted if
  desired.
  
  @param auth  authorization string, as UTF-8 encoded bytes
  @param quote true to wrap escaped authorization in quotes
  @return escaped authorization string