Refine search
protected long createBlob(int i, InputStream inputStream, long length) throws SQLException { LargeObjectManager lom = connection.getLargeObjectAPI(); long oid = lom.createLO(); LargeObject lob = lom.open(oid); OutputStream outputStream = lob.getOutputStream(); byte[] buf = new byte[4096]; try {
/** * <p>This deletes a large object.</p> * * <p>It is identical to the delete method, and is supplied as the C API uses unlink.</p> * * @param oid describing object to delete * @throws SQLException on error */ public void unlink(long oid) throws SQLException { delete(oid); }
/** * This creates a large object, returning its OID. * * <p>It defaults to READWRITE for the new object's attributes. * * @return oid of new object * @exception SQLException on error * @deprecated As of 8.3, replaced by {@link #createLO()} */ public int create() throws SQLException { return create(READWRITE); }
LargeObjectManager lom = getLargeObjectManager(conn); if (ob != null) { LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); long size = copy((InputStream) ob, os); lo.truncate((int) size); lo.close(); } else { lom.delete(oid); row.setInt(col, -1); conn.setAutoCommit(false); LargeObjectManager lom = getLargeObjectManager(conn); oid = lom.create(); LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); copy((InputStream)ob, os);
public void setBytes(int parameterIndex, byte[] x) throws SQLException { checkClosed(); if (null == x) { setNull(parameterIndex, Types.VARBINARY); return ; } if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports the bytea datatype for byte arrays byte[] copy = new byte[x.length]; System.arraycopy(x, 0, copy, 0, x.length); preparedParameters.setBytea( parameterIndex, copy, 0, x.length); } else { //Version 7.1 and earlier support done as LargeObjects LargeObjectManager lom = connection.getLargeObjectAPI(); long oid = lom.createLO(); LargeObject lob = lom.open(oid); lob.write(x); lob.close(); setLong(parameterIndex, oid); } }
public InputStream getBinaryStream(int columnIndex) throws SQLException { checkResultSet( columnIndex ); if (wasNullFlag) return null; if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports BinaryStream for all PG bytea type //As the spec/javadoc for this method indicate this is to be used for //large binary values (i.e. LONGVARBINARY) PG doesn't have a separate //long binary datatype, but with toast the bytea datatype is capable of //handling very large values. Thus the implementation ends up calling //getBytes() since there is no current way to stream the value from the server byte b[] = getBytes(columnIndex); if (b != null) return new ByteArrayInputStream(b); } else { // In 7.1 Handle as BLOBS so return the LargeObject input stream if ( fields[columnIndex - 1].getOID() == Oid.OID) { LargeObjectManager lom = connection.getLargeObjectAPI(); LargeObject lob = lom.open(getLong(columnIndex)); return lob.getInputStream(); } } return null; }
/** * This opens an existing large object, based on its OID. This method assumes that READ and WRITE * access is required (the default). * * @param oid of large object * @return LargeObject instance providing access to the object * @throws SQLException on error */ public LargeObject open(long oid) throws SQLException { return open(oid, READWRITE, false); }
blob = lom.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE); s.execute("DELETE FROM " + METADATA_TABLE + " WHERE key = '" + key + "'"); s.execute("INSERT INTO " + METADATA_TABLE + " (key, value) VALUES('" + key + "', 'BLOB: " + blob + "')"); LargeObject obj = lom.open(blob, LargeObjectManager.WRITE); obj.truncate(0); return new LargeObjectOutputStream(con, obj); } catch (SQLException e) {
@Override public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException { synchronized (connection) { LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI(); LargeObject largeObject = null; Long oid = getOID(file); try { connection.setAutoCommit(false); largeObject = lobjManager.open(oid, LargeObjectManager.WRITE); largeObject.seek(largeObject.size()); largeObject.write(data); largeObject.close(); connection.commit(); } catch (Exception e) { connection.rollback(); throw e; } return data.length; } }
public static void main(String[] args) throws SQLException, FileNotFoundException, IOException { Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test01", "postgres", "postgres"); conn.setAutoCommit(false); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select id, name, content from test"); LargeObjectManager lom = PGConnection.class.cast(conn).getLargeObjectAPI(); byte[] buffer = new byte[2048]; int s = 0; while(rs.next()) { File file = new File("/tmp", rs.getLong("id") + "_" + rs.getString("name")); FileOutputStream fos = new FileOutputStream(file); LargeObject lob = lom.open(rs.getLong("content"), LargeObjectManager.READ); while((s = lob.read(buffer, 0, buffer.length)) > 0) { fos.write(buffer, 0, buffer.length); } lob.close(); fos.close(); } conn.close(); }
private int getPostGresLargeObjectSize(JDBCSequentialFile file) throws SQLException { LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI(); int size = 0; Long oid = getOID(file); if (oid != null) { synchronized (connection) { try { connection.setAutoCommit(false); LargeObject largeObject = lobjManager.open(oid, LargeObjectManager.READ); size = largeObject.size(); largeObject.close(); connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } } } return size; } }
protected synchronized LargeObject getLo(boolean forWrite) throws SQLException { if (this.currentLo != null) { if (forWrite && !currentLoIsWriteable) { // Reopen the stream in read-write, at the same pos. int currentPos = this.currentLo.tell(); LargeObjectManager lom = conn.getLargeObjectAPI(); LargeObject newLo = lom.open(oid, LargeObjectManager.READWRITE); this.subLOs.add(this.currentLo); this.currentLo = newLo; if (currentPos != 0) { this.currentLo.seek(currentPos); } } return this.currentLo; } LargeObjectManager lom = conn.getLargeObjectAPI(); currentLo = lom.open(oid, forWrite ? LargeObjectManager.READWRITE : LargeObjectManager.READ); currentLoIsWriteable = forWrite; return currentLo; }
/** * <p>This creates a large object, returning its OID.</p> * * <p>It defaults to READWRITE for the new object's attributes.</p> * * @return oid of new object * @throws SQLException if something wrong happens */ public long createLO() throws SQLException { return createLO(READWRITE); }
public LargeObjectManager getLargeObjectAPI() throws SQLException { checkClosed(); if (largeobject == null) { largeobject = new LargeObjectManager(this); } return largeobject; }
LargeObjectManager lom = getLargeObjectManager(conn); if (ob != null) { LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); long size = copy((InputStream) ob, os); lo.truncate((int) size); lo.close(); } else { lom.delete(oid); row.setInt(col, -1); conn.setAutoCommit(false); LargeObjectManager lom = getLargeObjectManager(conn); oid = lom.create(); LargeObject lo = lom.open(oid, LargeObjectManager.WRITE); OutputStream os = lo.getOutputStream(); copy((InputStream)ob, os);
LargeObjectManager largeObjectManager = null; LargeObject largeObject = null; Long largeObjectRef = null; largeObjectManager = ((org.postgresql.PGConnection) connection).getLargeObjectAPI(); largeObjectRef = largeObjectManager.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE); largeObject = largeObjectManager.open(largeObjectRef, LargeObjectManager.WRITE); largeObject.write("string/file content to write into oid column".getBytes()); largeObject.close();
// Get the Large Object Manager to perform operations with LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); PreparedStatement ps = conn.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?"); ps.setString(1, "myimage.gif"); ResultSet rs = ps.executeQuery(); while (rs.next()) { // Open the large object for reading int oid = rs.getInt(1); LargeObject obj = lobj.open(oid, LargeObjectManager.READ); // Read the data byte buf[] = new byte[obj.size()]; obj.read(buf, 0, obj.size()); // Do something with the data read here // Close the object obj.close(); } rs.close(); ps.close();