org.postgresql.largeobject.LargeObject Java Examples

The following examples show how to use org.postgresql.largeobject.LargeObject. 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);
    }
}
 
Example #5
Source File: PostgresLargeObjectManager.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public int size(Object largeObject) throws SQLException {
   if (shouldUseReflection) {
      try {
         Method method = largeObject.getClass().getMethod("size");
         return (int) method.invoke(largeObject);
      } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
      }
   } else {
      return ((LargeObject) largeObject).size();
   }
}
 
Example #6
Source File: PostgresLargeObjectManager.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void close(Object largeObject) throws SQLException {
   if (shouldUseReflection) {
      try {
         Method method = largeObject.getClass().getMethod("close");
         method.invoke(largeObject);
      } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
      }
   } else {
      ((LargeObject) largeObject).close();
   }
}
 
Example #7
Source File: PostgresLargeObjectManager.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public byte[] read(Object largeObject, int length) throws SQLException {
   if (shouldUseReflection) {
      try {
         Method method = largeObject.getClass().getMethod("read", int.class);
         return (byte[]) method.invoke(largeObject, length);
      } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
      }
   } else {
      return ((LargeObject) largeObject).read(length);
   }
}
 
Example #8
Source File: PostgresLargeObjectManager.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void write(Object largeObject, byte[] data) throws SQLException {
   if (shouldUseReflection) {
      try {
         Method method = largeObject.getClass().getMethod("write", byte[].class);
         method.invoke(largeObject, data);
      } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
      }
   } else {
      ((LargeObject) largeObject).write(data);
   }
}
 
Example #9
Source File: PostgresLargeObjectManager.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void seek(Object largeObject, int position) throws SQLException {
   if (shouldUseReflection) {
      try {
         Method method = largeObject.getClass().getMethod("seek", int.class);
         method.invoke(largeObject, position);
      } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
      }
   } else {
      ((LargeObject) largeObject).seek(position);
   }
}
 
Example #10
Source File: PostgresLargeObjectManager.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void truncate(Object largeObject, int position) throws SQLException {
   if (shouldUseReflection) {
      try {
         Method method = largeObject.getClass().getMethod("truncate", int.class);
         method.invoke(largeObject, position);
      } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
         throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
      }
   } else {
      ((LargeObject) largeObject).truncate(position);
   }
}