X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 279538BA10D for ; Sat, 19 Feb 2005 17:48:53 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 14691-04 for ; Sat, 19 Feb 2005 17:48:43 +0000 (GMT) Received: from ranger.systems.pipex.net (ranger.systems.pipex.net [62.241.162.32]) by svr1.postgresql.org (Postfix) with ESMTP id AC9848BA54F for ; Sat, 19 Feb 2005 17:48:42 +0000 (GMT) Received: from corona (81-179-218-238.dsl.pipex.com [81.179.218.238]) by ranger.systems.pipex.net (Postfix) with ESMTP id 161F9E0001D5 for ; Sat, 19 Feb 2005 17:48:40 +0000 (GMT) Subject: [Patch] JDBC3 Blob support From: Michael Barker To: pgsql-jdbc@postgresql.org Content-Type: multipart/mixed; boundary="=-Iix5Rieb0rwBKYl2EmWZ" Date: Sat, 19 Feb 2005 17:58:14 -0800 Message-Id: <1108864694.5883.5.camel@corona> Mime-Version: 1.0 X-Mailer: Evolution 2.0.3 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.202 tagged_above=0 required=5 tests=DATE_IN_FUTURE_06_12 X-Spam-Level: * X-Archive-Number: 200502/136 X-Sequence-Number: 12316 --=-Iix5Rieb0rwBKYl2EmWZ Content-Type: text/plain Content-Transfer-Encoding: 7bit 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 --=-Iix5Rieb0rwBKYl2EmWZ Content-Description: Content-Disposition: attachment; filename=blob-support.patch Content-Type: text/x-patch; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit ? .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 byte array to the * BLOB value that this Blob 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(); } /** --=-Iix5Rieb0rwBKYl2EmWZ--