In Blob.getBytes(long position, int length) position is an offset
authorKris Jurka <books@ejurka.com>
Sun, 8 May 2005 23:16:58 +0000 (23:16 +0000)
committerKris Jurka <books@ejurka.com>
Sun, 8 May 2005 23:16:58 +0000 (23:16 +0000)
starting at 1, not zero as the driver was previously doing.

Thanks to Emmanuel Bernard for the report.

src/interfaces/jdbc/org/postgresql/errors.properties
src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java
src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java

index 2ede089016eaba935ff5189cd89d6182dc1c1ceb..63cc075a2ce415114d8494cb620af9b89b7aeef6 100644 (file)
@@ -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.
index 27e10af2b95cd37e840564eed8739e1b18453fea..a1dcda9609e36667433d3969b682c6807424055f 100644 (file)
@@ -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);
    }
 
 }
index 84d6cdc8eaa35e5e499701df1eb472328b62073d..cc07c931c41aa0da401e64435f345ac0fe2c6c55 100644 (file)
@@ -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);
        }