1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-24 09:27:52 +03:00

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.
This commit is contained in:
Kris Jurka
2005-05-08 23:16:58 +00:00
parent 0053e290d9
commit 43418e8702
3 changed files with 29 additions and 11 deletions

View 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.

View 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);
}
}

View 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);
}