Permalink
Browse files

refactor: replace some usages of initCause (#1037)

replace some usages of initCause with appropriate constructor
  • Loading branch information...
AlexElin authored and davecramer committed Jan 12, 2018
1 parent 2e8c2b6 commit 0c29823ad8eca86e3b8f27bcee5f116d41e367f9
@@ -69,20 +69,24 @@ private void checkClosed() throws IOException {
}
@Override
public int available() throws IOException {
checkClosed();
return (buf != null ? len - at : 0);
}
@Override
public int read() throws IOException {
checkClosed();
return gotBuf() ? buf[at++] : -1;
}
@Override
public int read(byte[] buf) throws IOException {
return read(buf, 0, buf.length);
}
@Override
public int read(byte[] buf, int off, int siz) throws IOException {
checkClosed();
int got = 0;
@@ -93,15 +97,14 @@ public int read(byte[] buf, int off, int siz) throws IOException {
return got == 0 && !didReadSomething ? -1 : got;
}
@Override
public byte[] readFromCopy() throws SQLException {
byte[] result = buf;
try {
if (gotBuf()) {
if (at > 0 || len < buf.length) {
byte[] ba = new byte[len - at];
for (int i = at; i < len; i++) {
ba[i - at] = buf[i];
}
System.arraycopy(buf, at, ba, 0, len - at);
result = ba;
}
at = len; // either partly or fully returned, buffer is exhausted
@@ -117,6 +120,7 @@ public int read(byte[] buf, int off, int siz) throws IOException {
return readFromCopy();
}
@Override
public void close() throws IOException {
// Don't complain about a double close.
if (op == null) {
@@ -127,34 +131,38 @@ public void close() throws IOException {
try {
op.cancelCopy();
} catch (SQLException se) {
IOException ioe = new IOException("Failed to close copy reader.");
ioe.initCause(se);
throw ioe;
throw new IOException("Failed to close copy reader.", se);
}
}
op = null;
}
@Override
public void cancelCopy() throws SQLException {
op.cancelCopy();
}
@Override
public int getFormat() {
return op.getFormat();
}
@Override
public int getFieldFormat(int field) {
return op.getFieldFormat(field);
}
@Override
public int getFieldCount() {
return op.getFieldCount();
}
@Override
public boolean isActive() {
return op != null && op.isActive();
}
@Override
public long getHandledRowCount() {
return op.getHandledRowCount();
}
@@ -65,6 +65,7 @@ public PGCopyOutputStream(CopyIn op, int bufferSize) {
copyBuffer = new byte[bufferSize];
}
@Override
public void write(int b) throws IOException {
checkClosed();
if (b < 0 || b > 255) {
@@ -74,18 +75,18 @@ public void write(int b) throws IOException {
write(singleByteBuffer, 0, 1);
}
@Override
public void write(byte[] buf) throws IOException {
write(buf, 0, buf.length);
}
@Override
public void write(byte[] buf, int off, int siz) throws IOException {
checkClosed();
try {
writeToCopy(buf, off, siz);
} catch (SQLException se) {
IOException ioe = new IOException("Write to copy failed.");
ioe.initCause(se);
throw ioe;
throw new IOException("Write to copy failed.", se);
}
}
@@ -95,6 +96,7 @@ private void checkClosed() throws IOException {
}
}
@Override
public void close() throws IOException {
// Don't complain about a double close.
if (op == null) {
@@ -104,25 +106,23 @@ public void close() throws IOException {
try {
endCopy();
} catch (SQLException se) {
IOException ioe = new IOException("Ending write to copy failed.");
ioe.initCause(se);
throw ioe;
throw new IOException("Ending write to copy failed.", se);
}
op = null;
}
@Override
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;
throw new IOException("Unable to flush stream", e);
}
}
@Override
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
@@ -137,30 +137,37 @@ public void writeToCopy(byte[] buf, int off, int siz) throws SQLException {
}
}
@Override
public int getFormat() {
return op.getFormat();
}
@Override
public int getFieldFormat(int field) {
return op.getFieldFormat(field);
}
@Override
public void cancelCopy() throws SQLException {
op.cancelCopy();
}
@Override
public int getFieldCount() {
return op.getFieldCount();
}
@Override
public boolean isActive() {
return op.isActive();
}
@Override
public void flushCopy() throws SQLException {
op.flushCopy();
}
@Override
public long endCopy() throws SQLException {
if (at > 0) {
op.writeToCopy(copyBuffer, 0, at);
@@ -169,6 +176,7 @@ public long endCopy() throws SQLException {
return getHandledRowCount();
}
@Override
public long getHandledRowCount() {
return op.getHandledRowCount();
}
@@ -51,6 +51,7 @@
this.allGeneratedRows = !expectGeneratedKeys ? null : new ArrayList<List<byte[][]>>();
}
@Override
public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tuples,
ResultCursor cursor) {
// If SELECT, then handleCommandStatus call would just be missing
@@ -73,6 +74,7 @@ public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tup
latestGeneratedRows = tuples;
}
@Override
public void handleCommandStatus(String status, int updateCount, long insertOID) {
if (latestGeneratedRows != null) {
// We have DML. Decrease resultIndex that was just increased in handleResultRows
@@ -125,6 +127,7 @@ private void updateGeneratedKeys() {
allGeneratedRows.clear();
}
@Override
public void handleWarning(SQLWarning warning) {
pgStatement.addWarning(warning);
}
@@ -145,15 +148,15 @@ public void handleError(SQLException newError) {
BatchUpdateException batchException = new BatchUpdateException(
GT.tr("Batch entry {0} {1} was aborted: {2} Call getNextException to see other errors in the batch.",
resultIndex, queryString, newError.getMessage()),
newError.getSQLState(), uncompressUpdateCount());
batchException.initCause(newError);
newError.getSQLState(), uncompressUpdateCount(), newError);
super.handleError(batchException);
}
resultIndex++;
super.handleError(newError);
}
@Override
public void handleCompletion() throws SQLException {
updateGeneratedKeys();
SQLException batchException = getException();
@@ -163,9 +166,8 @@ public void handleCompletion() throws SQLException {
BatchUpdateException newException = new BatchUpdateException(
batchException.getMessage(),
batchException.getSQLState(),
uncompressUpdateCount()
);
newException.initCause(batchException.getCause());
uncompressUpdateCount(),
batchException.getCause());
SQLException next = batchException.getNextException();
if (next != null) {
newException.setNextException(next);
@@ -17,6 +17,7 @@
super(statement, queries, parameterLists, false);
}
@Override
public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tuples, ResultCursor cursor) {
/* ignore */
}

0 comments on commit 0c29823

Please sign in to comment.