public void writeToCopy(byte[] buf, int off, int siz) throws SQLException { if (at > 0 && siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf op.writeToCopy(copyBuffer, 0, at); at = 0; } if (siz > copyBuffer.length) { // would still not fit into buf, so just pass it through op.writeToCopy(buf, off, siz); } else { // fits into our buf, so save it there System.arraycopy(buf, off, copyBuffer, at, siz); at += siz; } }
public void writeToCopy(byte[] buf, int off, int siz) throws SQLException { if(at > 0 && siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf op.writeToCopy(copyBuffer, 0, at); at = 0; } if(siz > copyBuffer.length) { // would still not fit into buf, so just pass it through op.writeToCopy(buf, off, siz); } else { // fits into our buf, so save it there System.arraycopy(buf, off, copyBuffer, at, siz); at += siz; } }
public void flush() throws IOException { try { op.writeToCopy(copyBuffer, 0, at); at = 0; op.flushCopy(); } catch (SQLException e) { IOException ioe = new IOException("Unable to flush stream"); ioe.initCause(e); throw ioe; } }
public void flush() throws IOException { try { op.writeToCopy(copyBuffer, 0, at); at = 0; op.flushCopy(); } catch (SQLException e) { IOException ioe = new IOException("Unable to flush stream"); ioe.initCause(e); throw ioe; } }
public long endCopy() throws SQLException { if (at > 0) { op.writeToCopy(copyBuffer, 0, at); } op.endCopy(); return getHandledRowCount(); }
public long endCopy() throws SQLException { if(at > 0) { op.writeToCopy(copyBuffer, 0, at); } op.endCopy(); return getHandledRowCount(); }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
/** * Use COPY FROM STDIN for very fast copying from an InputStream into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of bytes to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon input stream or database connection failure */ public long copyIn(final String sql, InputStream from, int bufferSize) throws SQLException, IOException { byte[] buf = new byte[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while( (len = from.read(buf)) > 0 ) { cp.writeToCopy(buf, 0, len); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } } }
/** * Use COPY FROM STDIN for very fast copying from an InputStream into a database table. * * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of bytes to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon input stream or database connection failure */ public long copyIn(final String sql, InputStream from, int bufferSize) throws SQLException, IOException { byte[] buf = new byte[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ((len = from.read(buf)) >= 0) { if (len > 0) { cp.writeToCopy(buf, 0, len); } } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if (cp.isActive()) { cp.cancelCopy(); } } } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ((len = from.read(cbuf)) >= 0) { if (len > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if (cp.isActive()) { cp.cancelCopy(); } } }
public void writeToCopy(byte[] buf, int off, int siz) throws SQLException { if(at > 0 && siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf op.writeToCopy(copyBuffer, 0, at); at = 0; } if(siz > copyBuffer.length) { // would still not fit into buf, so just pass it through op.writeToCopy(buf, off, siz); } else { // fits into our buf, so save it there System.arraycopy(buf, off, copyBuffer, at, siz); at += siz; } }
public void writeToCopy(byte[] buf, int off, int siz) throws SQLException { if(at > 0 && siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf op.writeToCopy(copyBuffer, 0, at); at = 0; } if(siz > copyBuffer.length) { // would still not fit into buf, so just pass it through op.writeToCopy(buf, off, siz); } else { // fits into our buf, so save it there System.arraycopy(buf, off, copyBuffer, at, siz); at += siz; } }
public void flush() throws IOException { try { op.writeToCopy(copyBuffer, 0, at); at = 0; op.flushCopy(); } catch (SQLException e) { IOException ioe = new IOException("Unable to flush stream"); ioe.initCause(e); throw ioe; } }
public void flush() throws IOException { try { op.writeToCopy(copyBuffer, 0, at); at = 0; op.flushCopy(); } catch (SQLException e) { IOException ioe = new IOException("Unable to flush stream"); ioe.initCause(e); throw ioe; } }
public long endCopy() throws SQLException { if(at > 0) { op.writeToCopy(copyBuffer, 0, at); } op.endCopy(); return getHandledRowCount(); }
public long endCopy() throws SQLException { if(at > 0) { op.writeToCopy(copyBuffer, 0, at); } op.endCopy(); return getHandledRowCount(); }
/** * Use COPY FROM STDIN for very fast copying from an InputStream into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of bytes to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon input stream or database connection failure */ public long copyIn(final String sql, InputStream from, int bufferSize) throws SQLException, IOException { byte[] buf = new byte[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while( (len = from.read(buf)) > 0 ) { cp.writeToCopy(buf, 0, len); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of characters to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from, int bufferSize) throws SQLException, IOException { char[] cbuf = new char[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
// your code CopyManager copyManager = new CopyManager((BaseConnection) new DataSource().connect()); FileReader from = ... // different name! int bufferSize = 65536; // here starts the copy of the driver's implementation of the copyIn() method. char[] cbuf = new char[bufferSize]; int len; // if you store the instance of the CopyIn interface in an instance variable you // should be able to call cancelCopy() on it CopyIn cp = copyManager.copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); }