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
{
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);
}
*/
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);
}
}
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 ;-)
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)
{
}
}
+ 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
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);
}