docs: public long copyOut(final String sql, Writer to) throws SQLException, IOException {
      byte[] buf;
      CopyOut cp = copyOut(sql);
      try {
        while ((buf = cp.readFromCopy()) != null) {
          to.write(encoding.decode(buf));
        }
        return cp.getHandledRowCount();
      } catch (IOException ioEX) {
        // if not handled this way the close call will hang, at least in 8.2
        if (cp.isActive()) {
          cp.cancelCopy();
        }
        try { // read until exhausted or operation cancelled SQLException
          while ((buf = cp.readFromCopy()) != null) {
          }
        } catch (SQLException sqlEx) {
        } // typically after several kB
        throw ioEX;
      } finally { // see to it that we do not leave the connection locked
        if (cp.isActive()) {
          cp.cancelCopy();
        }
      }
    }