This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to annotated tag REL9_3_1102
in repository libpostgresql-jdbc-java.

commit df4f8367bcf35ffa946ba0ec4e5ec78255166216
Author: Dave Cramer <[email protected]>
Date:   Tue Jul 8 15:58:09 2014 -0400

    backpatch exception during close of fully read stream from romank0
---
 org/postgresql/copy/PGCopyInputStream.java         | 16 ++--
 .../test/jdbc4/PGCopyInputStreamTest.java          | 92 ++++++++++++++++++++++
 2 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/org/postgresql/copy/PGCopyInputStream.java 
b/org/postgresql/copy/PGCopyInputStream.java
index 8450475..f540541 100644
--- a/org/postgresql/copy/PGCopyInputStream.java
+++ b/org/postgresql/copy/PGCopyInputStream.java
@@ -115,12 +115,14 @@ public class PGCopyInputStream extends InputStream 
implements CopyOut {
         if (op == null)
             return;
 
-        try {
-            op.cancelCopy();
-        } catch(SQLException se) {
-            IOException ioe = new IOException("Failed to close copy reader.");
-            ioe.initCause(se);
-            throw ioe;
+        if (op.isActive()) {
+               try {
+                   op.cancelCopy();
+               } catch(SQLException se) {
+                   IOException ioe = new IOException("Failed to close copy 
reader.");
+                   ioe.initCause(se);
+                   throw ioe;
+               }
         }
         op = null;
     }
@@ -142,7 +144,7 @@ public class PGCopyInputStream extends InputStream 
implements CopyOut {
     }
 
     public boolean isActive() {
-        return op.isActive();
+        return op != null && op.isActive();
     }
 
     public long getHandledRowCount() {
diff --git a/org/postgresql/test/jdbc4/PGCopyInputStreamTest.java 
b/org/postgresql/test/jdbc4/PGCopyInputStreamTest.java
new file mode 100644
index 0000000..7c5f921
--- /dev/null
+++ b/org/postgresql/test/jdbc4/PGCopyInputStreamTest.java
@@ -0,0 +1,92 @@
+package org.postgresql.test.jdbc4;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import org.postgresql.PGConnection;
+import org.postgresql.copy.PGCopyInputStream;
+import org.postgresql.test.TestUtil;
+
+import junit.framework.TestCase;
+
+public class PGCopyInputStreamTest extends TestCase {
+    private Connection _conn;
+    private PGCopyInputStream sut;
+
+    public PGCopyInputStreamTest(String name) {
+        super(name);
+    }
+
+    protected void setUp() throws Exception {
+        _conn = TestUtil.openDB();
+        TestUtil.createTable(_conn, "cpinstreamtest", "i int");
+    }
+
+    protected void tearDown() throws SQLException {
+       silentlyCloseStream(sut);
+        TestUtil.dropTable(_conn, "cpinstreamtest");
+        TestUtil.closeDB(_conn);
+    }
+    
+    public void testReadBytesCorrectlyHandlesEof() throws SQLException, 
IOException {
+       insertSomeData();
+       
+       sut = new PGCopyInputStream((PGConnection) _conn, "COPY cpinstreamtest 
(i) TO STDOUT WITH (FORMAT CSV, HEADER false)");
+       
+       byte[] buf = new byte[100]; // large enough to read everything on the 
next step
+       assertTrue(sut.read(buf) > 0);
+       
+       assertEquals(-1, sut.read(buf));
+    }
+
+    public void testReadBytesCorrectlyReadsDataInChunks() throws SQLException, 
IOException {
+       insertSomeData();
+       
+       sut = new PGCopyInputStream((PGConnection) _conn, "COPY (select i from 
cpinstreamtest order by i asc) TO STDOUT WITH (FORMAT CSV, HEADER false)");
+       
+       byte[] buf = new byte[2]; // small enough to read in multiple chunks
+       StringBuilder result = new StringBuilder(100);
+       int chunks = 0;
+       while (sut.read(buf) > 0) {
+               result.append(new String(buf));
+               ++chunks;
+       }
+       
+       assertEquals(4, chunks);
+       assertEquals("0\n1\n2\n3\n", result.toString());
+    }
+    
+    public void testStreamCanBeClosedAfterReadUp() throws SQLException, 
IOException {
+       insertSomeData();
+       
+       sut = new PGCopyInputStream((PGConnection) _conn, "COPY (select i from 
cpinstreamtest order by i asc) TO STDOUT WITH (FORMAT CSV, HEADER false)");
+       
+       byte[] buff = new byte[100];
+               while(sut.read(buff) > 0);
+               
+               sut.close();
+    }
+    
+       private void silentlyCloseStream(PGCopyInputStream sut) {
+               if (sut != null) {
+                       try {
+                               if (sut.isActive()) {
+                                       sut.close();
+                               }
+                       } catch (IOException e) {
+                               // intentionally ignore 
+                       }
+               }
+       }
+
+       private void insertSomeData() throws SQLException {
+               PreparedStatement pstmt = _conn.prepareStatement("insert into 
cpinstreamtest (i) values (?)");
+               for (int i = 0; i < 4; ++i) {
+               pstmt.setInt(1, i);
+               pstmt.addBatch();       
+               }
+               pstmt.executeBatch();
+       }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git

_______________________________________________
pkg-java-commits mailing list
[email protected]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

Reply via email to