[libpostgresql-jdbc-java] 02/14: In Blob.getBytes(long position, int length) position is an offset starting at 1, not zero as the driver was previously doing.
Emmanuel Bourg
ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:19:12 UTC 2017
This is an automated email from the git hooks/post-receive script.
ebourg-guest pushed a commit to tag REL8_0_312
in repository libpostgresql-jdbc-java.
commit 965d14885768750f664cd62f59b3f25d5e48dba9
Author: Kris Jurka <books at ejurka.com>
Date: Sun May 8 23:17:48 2005 +0000
In Blob.getBytes(long position, int length) position is an offset
starting at 1, not zero as the driver was previously doing.
Thanks to Emmanuel Bernard for the report.
---
org/postgresql/jdbc2/AbstractJdbc2Blob.java | 13 +++++++---
org/postgresql/test/jdbc2/BlobTest.java | 37 +++++++++++++++--------------
2 files changed, 29 insertions(+), 21 deletions(-)
diff --git a/org/postgresql/jdbc2/AbstractJdbc2Blob.java b/org/postgresql/jdbc2/AbstractJdbc2Blob.java
index 230ec19..3a1147b 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2Blob.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2Blob.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java,v 1.7 2005/01/11 08:25:45 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java,v 1.7.2.1 2005/02/15 08:55:50 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,6 +16,10 @@ import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
+import org.postgresql.util.GT;
+import org.postgresql.util.PSQLState;
+import org.postgresql.util.PSQLException;
+
public abstract class AbstractJdbc2Blob
{
private LargeObject lo;
@@ -38,7 +42,10 @@ public abstract class AbstractJdbc2Blob
public byte[] getBytes(long pos, int length) throws SQLException
{
- lo.seek((int)pos, LargeObject.SEEK_SET);
+ if (pos < 1) {
+ throw new PSQLException(GT.tr("LOB positioning offsets start at 1."), PSQLState.INVALID_PARAMETER_VALUE);
+ }
+ lo.seek((int)(pos-1), LargeObject.SEEK_SET);
return lo.read(length);
}
@@ -55,7 +62,7 @@ public abstract class AbstractJdbc2Blob
*/
public long position(Blob pattern, long start) throws SQLException
{
- return position(pattern.getBytes(0, (int)pattern.length()), start);
+ return position(pattern.getBytes(1, (int)pattern.length()), start);
}
}
diff --git a/org/postgresql/test/jdbc2/BlobTest.java b/org/postgresql/test/jdbc2/BlobTest.java
index cc9ab4c..7cb80c2 100644
--- a/org/postgresql/test/jdbc2/BlobTest.java
+++ b/org/postgresql/test/jdbc2/BlobTest.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/BlobTest.java,v 1.14 2005/01/11 08:25:48 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/BlobTest.java,v 1.14.2.1 2005/04/28 14:18:09 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -28,7 +28,6 @@ public class BlobTest extends TestCase
private static final int LOOP = 0; // LargeObject API using loop
private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream
- private static final int JDBC_STREAM = 2; // JDBC API using OutputStream
public BlobTest(String name)
{
@@ -39,10 +38,12 @@ public class BlobTest extends TestCase
{
con = TestUtil.openDB();
TestUtil.createTable(con, "testblob", "id name,lo oid");
+ con.setAutoCommit(false);
}
protected void tearDown() throws Exception
{
+ con.setAutoCommit(true);
TestUtil.dropTable(con, "testblob");
TestUtil.closeDB(con);
}
@@ -75,9 +76,6 @@ public class BlobTest extends TestCase
*/
public void testUploadBlob_LOOP() throws Exception
{
- con.setAutoCommit(false);
- assertTrue(!con.getAutoCommit());
-
assertTrue(uploadFile("build.xml", LOOP) > 0);
// Now compare the blob & the file. Note this actually tests the
@@ -85,8 +83,6 @@ public class BlobTest extends TestCase
assertTrue(compareBlobsLOAPI());
assertTrue(compareBlobs());
assertTrue(compareClobs());
-
- con.setAutoCommit(true);
}
/*
@@ -94,16 +90,28 @@ public class BlobTest extends TestCase
*/
public void testUploadBlob_NATIVE() throws Exception
{
- con.setAutoCommit(false);
- assertTrue(!con.getAutoCommit());
-
assertTrue(uploadFile("build.xml", NATIVE_STREAM) > 0);
// Now compare the blob & the file. Note this actually tests the
// InputStream implementation!
assertTrue(compareBlobs());
+ }
- con.setAutoCommit(true);
+ public void testGetBytesOffset() throws Exception
+ {
+ assertTrue(uploadFile("build.xml", NATIVE_STREAM) > 0);
+
+ Statement stmt = con.createStatement();
+ ResultSet rs = stmt.executeQuery("SELECT lo FROM testblob");
+ assertTrue(rs.next());
+
+ Blob lob = rs.getBlob(1);
+ byte data[] = lob.getBytes(2,4);
+ assertEquals(data.length, 4);
+ assertEquals(data[0], '?');
+ assertEquals(data[1], 'x');
+ assertEquals(data[2], 'm');
+ assertEquals(data[3], 'l');
}
/*
@@ -147,13 +155,6 @@ public class BlobTest extends TestCase
os.close();
break;
- case JDBC_STREAM:
- File f = new File(file);
- PreparedStatement ps = con.prepareStatement(TestUtil.insertSQL("testblob", "?"));
- ps.setBinaryStream(1, fis, (int) f.length());
- ps.execute();
- break;
-
default:
assertTrue("Unknown method in uploadFile", false);
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git
More information about the pkg-java-commits
mailing list