From 43418e8702ac3d900725e17931691911031c7b66 Mon Sep 17 00:00:00 2001 From: Kris Jurka Date: Sun, 8 May 2005 23:16:58 +0000 Subject: [PATCH] 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. --- .../jdbc/org/postgresql/errors.properties | 1 + .../postgresql/jdbc2/AbstractJdbc2Blob.java | 9 ++++-- .../org/postgresql/test/jdbc2/BlobTest.java | 30 +++++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/interfaces/jdbc/org/postgresql/errors.properties b/src/interfaces/jdbc/org/postgresql/errors.properties index 2ede089016e..63cc075a2ce 100644 --- a/src/interfaces/jdbc/org/postgresql/errors.properties +++ b/src/interfaces/jdbc/org/postgresql/errors.properties @@ -111,3 +111,4 @@ postgresql.format.baddate:The date given: {0} does not match the format required postgresql.format.badtime:The time given: {0} does not match the format required: {1}. postgresql.format.badtimestamp:The timestamp given {0} does not match the format required: {1}. postgresql.input.field.gt0:The maximum field size must be a value greater than or equal to 0. +postgresql.blob.badpos:LOB positioning offsets start at 1. diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java b/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java index 27e10af2b95..a1dcda9609e 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java @@ -6,6 +6,8 @@ import org.postgresql.largeobject.LargeObjectManager; import java.io.InputStream; import java.sql.Blob; import java.sql.SQLException; +import org.postgresql.util.PSQLState; +import org.postgresql.util.PSQLException; public abstract class AbstractJdbc2Blob { @@ -31,7 +33,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("postgresql.blob.badpos", PSQLState.INVALID_PARAMETER_VALUE); + } + lo.seek((int)(pos-1), LargeObject.SEEK_SET); return lo.read(length); } @@ -48,7 +53,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/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java index 84d6cdc8eaa..cc07c931c41 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java @@ -8,7 +8,7 @@ import java.sql.*; import org.postgresql.largeobject.*; /* - * $Id: BlobTest.java,v 1.9 2003/08/15 18:45:11 barry Exp $ + * $Id: BlobTest.java,v 1.9.2.1 2005/05/08 23:16:58 jurka Exp $ * * Some simple tests based on problems reported by users. Hopefully these will * help prevent previous problems from re-occuring ;-) @@ -21,7 +21,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) { @@ -90,6 +89,26 @@ public class BlobTest extends TestCase } } + public void testGetBytesOffset() throws Exception + { + con.setAutoCommit(false); + 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'); + + con.setAutoCommit(false); + } + /* * Helper - uploads a file into a blob using old style methods. We use this * because it always works, and we can use it as a base to test the new @@ -131,13 +150,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); } -- 2.39.5