Skip site navigation (1) Skip section navigation (2)

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: (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc

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() {

			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 
insertStmt.setBlob(1, new BlobImpl());
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?



pgsql-jdbc by date

Next:From: Mark LewisDate: 2005-08-30 16:26:08
Subject: Re: max connection
Previous:From: Hina ChauhanDate: 2005-08-30 06:22:48
Subject: max connection

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group