Blob and unknown length

From: Oscar Picasso <oscpro(at)videotron(dot)ca>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Blob and unknown length
Date: 2005-08-30 16:14:45
Message-ID: 431485F5.1010407@videotron.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi,

I have the following requirement for blobs:
- use of standard JDBC API (not the LargeObject API);
- blobs can be potentially huge in size;
- the blob length is not known ahead of time;

I have tried many things and decided to use oid instead of bytea (I got
many OutOfMemoryExceptions with bytea).

To push the data into the db it seems to just work fine with the JDBC API.

But to pull the data from the db, I need to provide a custom Blob
implementation. At least that what I have understood.

To test the solution I have done the following custom Blob implementation.

...
public class BlobImpl implements Blob {
private final int limit = 10; // Change this value for testing
private int cur = 0;

public long length() throws SQLException {
return Integer.MAX_VALUE;
}

public InputStream getBinaryStream() throws SQLException {
return new InputStream() {

@Override
public int read() throws IOException {
return cur++ < limit ? 'a' : -1;
}};
}

// All other methods throw an Exception
...
}

And the code that uses the Blob implementation (no postgresql specific
code).
...
insertStmt.setBlob(1, new BlobImpl());
insertStmt.executeUpdate();
conn.commit();
...
It seems to works fine.

If I retrieve the inserted Blob from the database it's length is the
value of the limit field not the length() value.

So it seems I can use this kind of workaround to insert unknown length
data in the db. The driver seems to ignore the length() value when
inserting the data with the getBinaryStream().

Am I correct? If so it would be great. I will only use the custom Blob
implement for insertions and updates.

But are they some potential problem if I use this kind of implementation
the way I describe it?

Thanks

Oscar

Browse pgsql-jdbc by date

  From Date Subject
Next Message Mark Lewis 2005-08-30 16:26:08 Re: max connection
Previous Message Hina Chauhan 2005-08-30 06:22:48 max connection