pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oscar Picasso <oscpro@videotron.ca>
To: pgsql-jdbc@postgresql.org
Subject: Blob and unknown length
Date: Tue, 30 Aug 2005 12:14:45 -0400
Message-ID: <431485F5.1010407@videotron.ca> (raw)

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





Message-ID: <431485F5.1010407@videotron.ca>
Permalink:  ../431485F5.1010407@videotron.ca/
Also on:    postgresql.org/message-id/431485F5.1010407@videotron.ca

 · 

reply

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: oscpro@videotron.ca
  Subject: Re: Blob and unknown length
  In-Reply-To: <431485F5.1010407@videotron.ca>
  References: <431485F5.1010407@videotron.ca>

* 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