org.postgresql.largeobject.LargeObjectManager Java Examples

The following examples show how to use org.postgresql.largeobject.LargeObjectManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PostgreSqlUtil.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    * Extract the Large Object Input Stream from PostgreSQL
    *
    * @param resultSet
    *            the Result Set to extract the blob from
    * @param columnIndex
    *            the index of column
    * @return the Large Object Input Stream from PostgreSQL
    * @throws SQLException
    */
   public static InputStream getPostgreSqlnputStream(ResultSet resultSet,
    int columnIndex) throws SQLException {
InputStream in;
Statement statement = resultSet.getStatement();
Connection conn = statement.getConnection();

// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection) conn)
	.getLargeObjectAPI();
long oid = resultSet.getLong(columnIndex);

if (oid < 1) {
    return null;
}

LargeObject obj = lobj.open(oid, LargeObjectManager.READ);

in = obj.getInputStream();
return in;
   }
 
Example #2
Source File: PostgreSqlUtil.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
    * Create a Large Object to set the PostgreSQL OID with
    *
    * @param preparedStatement
    *            the Prepared Statement
    * @param parameterIndex
    *            the parameter index
    * @param in
    *            The Input Stream to use
    * @param connection
    *            the JDBC Connection
    * @throws SQLException
    * @throws IOException
    */
   public static void setPostgreSqlParameterWithLargeObject(
    PreparedStatement preparedStatement, int parameterIndex,
    InputStream in, Connection connection)
    throws SQLException, IOException {
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection) connection)
	.getLargeObjectAPI();

// Create a new large object
long oid = lobj
	.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE);

// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);

try (OutputStream out = obj.getOutputStream();) {
    IOUtils.copy(in, out);
} finally {
    // IOUtils.closeQuietly(out);
    // Close the large object
    obj.close();
}

preparedStatement.setLong(parameterIndex, oid);
   }
 
Example #3
Source File: SqlFileStore.java    From syndesis with Apache License 2.0 6 votes vote down vote up
/**
 * Postgres does not allow to read from the large object after the connection has been closed.
 */
private InputStream doReadPostgres(String path) {
    try (Handle h = dbi.open()) {
        h.getConnection().setAutoCommit(false);

        List<Map<String, Object>> res = h.select("SELECT data FROM filestore WHERE path=?", path);

        Optional<Long> oid = res.stream()
            .map(row -> row.get("data"))
            .map(Long.class::cast)
            .findFirst();

        if (oid.isPresent()) {
            LargeObjectManager lobj = getPostgresConnection(h.getConnection()).getLargeObjectAPI();
            LargeObject obj = lobj.open(oid.get(), LargeObjectManager.READ);
            return new HandleCloserInputStream(h, obj.getInputStream());
        }

        return null;
    } catch (SQLException e) {
        throw DaoException.launderThrowable(e);
    }
}
 
Example #4
Source File: SqlFileStore.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static void doWritePostgres(Handle h, String path, InputStream file) {
    doDelete(h, path);
    try {
        LargeObjectManager lobj = getPostgresConnection(h.getConnection()).getLargeObjectAPI();
        long oid = lobj.createLO();
        LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
        try (OutputStream lob = obj.getOutputStream()) {
            IOUtils.copy(file, lob);
        }

        h.insert("INSERT INTO filestore(path, data) values (?,?)", path, oid);
    } catch (IOException | SQLException ex) {
        throw DaoException.launderThrowable(ex);
    }
}