From: Michael Barker <mike@middlesoft.co.uk>
To: pgsql-jdbc@postgresql.org
Subject: [Patch] JDBC3 Blob support
Date: Sat, 19 Feb 2005 17:58:14 -0800
Message-ID: <1108864694.5883.5.camel@corona> (raw)
Hi,
I have attached a patch that provides support for JDBC 3 Blobs for the
PostgreSQL driver.
JUnit tests are included. The only method that is not implemented is
truncate(). I couldn't find a simple efficient method for doing this
via the LargeObject API.
I am posting off list. Please email me direct if you have any queries.
Regards,
Michael Barker
Attachments:blob-support.patch (text/x-patch, 6.9K) download
? .settings
? blob-support.patch
? org/postgresql/test/jdbc3/Jdbc3BlobTest.java
Index: build.properties
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/build.properties,v
retrieving revision 1.6
diff -u -r1.6 build.properties
--- build.properties 2 Feb 2005 20:07:24 -0000 1.6+++ build.properties 19 Feb 2005 17:45:45 -0000@@ -8,3 +8,6 @@
fullversion=8.1devel
def_pgport=5432
enable_debug=yes
+user=mike+password=1gisu2t+
Index: org/postgresql/jdbc2/AbstractJdbc2Blob.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Blob.java,v
retrieving revision 1.8
diff -u -r1.8 AbstractJdbc2Blob.java
--- org/postgresql/jdbc2/AbstractJdbc2Blob.java 15 Feb 2005 08:56:25 -0000 1.8+++ org/postgresql/jdbc2/AbstractJdbc2Blob.java 19 Feb 2005 17:45:46 -0000@@ -9,13 +9,15 @@
*/
package org.postgresql.jdbc2;
-import org.postgresql.PGConnection;-import org.postgresql.largeobject.LargeObject;-import org.postgresql.largeobject.LargeObjectManager;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
+import org.postgresql.PGConnection;+import org.postgresql.largeobject.LargeObject;+import org.postgresql.largeobject.LargeObjectManager;+import org.postgresql.util.GT;+
public abstract class AbstractJdbc2Blob
{
private LargeObject lo;
@@ -42,20 +44,131 @@
return lo.read(length);
}
- /*- * For now, this is not implemented.+ + + /**+ * Iterate over the buffer looking for the specified pattern+ * + * @param pattern A pattern of bytes to search the blob for.+ * @param start The position to start reading from.
*/
public long position(byte[] pattern, long start) throws SQLException
{
- throw org.postgresql.Driver.notImplemented(this.getClass(), "position(byte[],long)");+ assertPosition(start, pattern.length);+ + int position = 0;+ int patternIdx = 0;+ long result = -1;+ int tmpPosition = 0;+ + for (LOIterator i = new LOIterator(start); i.hasNext(); position++)+ {+ byte b = i.next();+ if (b == pattern[patternIdx])+ {+ if (patternIdx == 0)+ {+ tmpPosition = position; + }+ patternIdx++;+ if (patternIdx == pattern.length)+ {+ result = tmpPosition;+ break;+ }+ }+ else+ {+ patternIdx = 0;+ }+ }+ + return result;+ }+ + /**+ * Iterates over a large object returning byte values. Will buffer+ * the data from the large object.+ * + *+ */+ private class LOIterator+ {+ private static final int BUFFER_SIZE = 8096;+ private byte buffer[] = new byte[BUFFER_SIZE];+ private int idx = BUFFER_SIZE;+ private int numBytes = BUFFER_SIZE;+ + public LOIterator(long start) throws SQLException+ {+ lo.seek((int) start);+ }+ + public boolean hasNext() throws SQLException+ {+ boolean result = false;+ if (idx < numBytes)+ {+ result = true;+ }+ else+ {+ numBytes = lo.read(buffer, 0, BUFFER_SIZE);+ idx = 0;+ result = (numBytes > 0);+ }+ return result;+ }+ + private byte next()+ {+ return buffer[idx++];+ }
}
+ - /*- * This should be simply passing the byte value of the pattern Blob+ /**+ * This is simply passing the byte value of the pattern Blob
*/
public long position(Blob pattern, long start) throws SQLException
{
return position(pattern.getBytes(0, (int)pattern.length()), start);
}
+ /**+ * Expose large object to derived classes.+ * @return+ */+ protected LargeObject getLO()+ {+ return lo;+ }+ + /**+ * Throws an exception if the pos value exceeds the max value by+ * which the large object API can index.+ * + * @param pos Position to write at.+ */+ protected void assertPosition(long pos) throws SQLException+ {+ assertPosition(pos, 0);+ }+ + /**+ * Throws an exception if the pos value exceeds the max value by+ * which the large object API can index.+ * + * @param pos Position to write at.+ * @param len number of bytes to write.+ */+ protected void assertPosition(long pos, long len) throws SQLException+ {+ if (pos + len > Integer.MAX_VALUE)+ {+ String err = GT.tr( "PostgreSQL Blobs can only index to: {0}", new Integer(Integer.MAX_VALUE)); + throw new SQLException(err);+ }+ }+
}
Index: org/postgresql/jdbc3/AbstractJdbc3Blob.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc3/AbstractJdbc3Blob.java,v
retrieving revision 1.6
diff -u -r1.6 AbstractJdbc3Blob.java
--- org/postgresql/jdbc3/AbstractJdbc3Blob.java 15 Feb 2005 08:56:26 -0000 1.6+++ org/postgresql/jdbc3/AbstractJdbc3Blob.java 19 Feb 2005 17:45:46 -0000@@ -10,7 +10,9 @@
package org.postgresql.jdbc3;
-import java.sql.*;+import java.sql.SQLException;++import org.postgresql.largeobject.LargeObject;
public abstract class AbstractJdbc3Blob extends org.postgresql.jdbc2.AbstractJdbc2Blob
{
@@ -37,10 +39,12 @@
*/
public int setBytes(long pos, byte[] bytes) throws SQLException
{
- throw org.postgresql.Driver.notImplemented(this.getClass(), "setBytes(long,byte[]");+ return setBytes(pos, bytes, 0, bytes.length);
}
- /**+ ++ /**
* Writes all or part of the given <code>byte</code> array to the
* <code>BLOB</code> value that this <code>Blob</code> object represents
* and returns the number of bytes written.
@@ -63,7 +67,11 @@
*/
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException
{
- throw org.postgresql.Driver.notImplemented(this.getClass(), "setBytes(long,byte[],int,int)");+ assertPosition(pos);+ LargeObject lo = getLO();+ lo.seek((int) pos);+ lo.write(bytes, offset, len);+ return len;
}
/**
@@ -82,7 +90,10 @@
*/
public java.io.OutputStream setBinaryStream(long pos) throws SQLException
{
- throw org.postgresql.Driver.notImplemented(this.getClass(), "setBinaryStream(long)");+ assertPosition(pos);+ LargeObject lo = getLO();+ lo.seek((int) pos);+ return lo.getOutputStream();
}
/**
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: mike@middlesoft.co.uk
Subject: Re: [Patch] JDBC3 Blob support
In-Reply-To: <1108864694.5883.5.camel@corona>
References: <1108864694.5883.5.camel@corona>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox