Large objects are supported in the standard JDBC specification. However, that interface is limited, and the API provided by PostgreSQL™ allows for random access to the objects contents, as if it was a local file.
The org.postgresql.largeobject package provides to Java the libpq C interface's large object API. It consists of two classes, LargeObjectManager, which deals with creating, opening and deleting large objects, and LargeObject which deals with an individual object.
public class LargeObject extends Object java.lang.Object | +----org.postgresql.largeobject.LargeObject
This class implements the large object interface to PostgreSQL™.
It provides the basic methods required to run the interface, plus a pair of methods that provide InputStream and OutputStream classes for this object.
Normally, client code would use the methods in BLOB to access large objects.
However, sometimes lower level access to Large Objects is required, that is not supported by the JDBC specification.
Refer to org.postgresql.largeobject.LargeObjectManager on how to gain access to a Large Object, or how to create one.
See Also: LargeObjectManager
Indicates a seek from the beginning of a file
Indicates a seek from the current position
Indicates a seek from the end of a file
public int getOID()
Returns the OID of this LargeObject
public void close() throws SQLException
This method closes the object. You must not call methods in this object after this is called.
public byte[] read(int len) throws SQLException
Reads some data from the object, and return as a byte[] array
public int read(byte buf[],
int off,
int len) throws SQLException
Reads some data from the object into an existing array
Parameters:
public void write(byte buf[]) throws SQLException
Writes an array to the object
public void write(byte buf[],
int off,
int len) throws SQLException
Writes some data from an array to the object
Parameters:
public class LargeObjectManager extends Object java.lang.Object | +----org.postgresql.largeobject.LargeObjectManager
This class implements the large object interface to PostgreSQL™. It provides methods that allow client code to create, open and delete large objects from the database. When opening an object, an instance of org.postgresql.largeobject.LargeObject is returned, and its methods then allow access to the object.
This class can only be created by org.postgresql.PGConnection. To get access to this class, use the following segment of code:
import org.postgresql.largeobject.*; Connection conn; LargeObjectManager lobj; // ... code that opens a connection ... lobj = ((org.postgresql.PGConnection)myconn).getLargeObjectAPI();
Normally, client code would use the BLOB methods to access large objects. However, sometimes lower level access to Large Objects is required, that is not supported by the JDBC specification.
Refer to org.postgresql.largeobject.LargeObject on how to manipulate the contents of a Large Object.
public LargeObject open(int oid) throws SQLException
This opens an existing large object, based on its OID. This method assumes that READ and WRITE access is required (the default).
public LargeObject open(int oid,
int mode) throws SQLException
This opens an existing large object, based on its OID, and allows setting the access mode.
public int create() throws SQLException
This creates a large object, returning its OID. It defaults to READWRITE for the new object's attributes.
public int create(int mode) throws SQLException
This creates a large object, returning its OID, and sets the access mode.
public void delete(int oid) throws SQLException
This deletes a large object.
public void unlink(int oid) throws SQLException
This deletes a large object. It is identical to the delete method, and is supplied as the C API uses “unlink”.