use of org.postgresql.largeobject.LargeObjectManager in project com.revolsys.open by revolsys.
the class PostgreSQLJdbcBlobFieldDefinition method setPreparedStatementValue.
@Override
public int setPreparedStatementValue(final PreparedStatement statement, final int parameterIndex, final Object value) throws SQLException {
if (value == null) {
final int sqlType = getSqlType();
statement.setNull(parameterIndex, sqlType);
} else {
Blob blob;
if (value instanceof Blob) {
blob = (Blob) value;
statement.setBlob(parameterIndex, blob);
} else {
InputStream in;
if (value instanceof InputStream) {
in = (InputStream) value;
} else if (value instanceof byte[]) {
final byte[] bytes = (byte[]) value;
in = new ByteArrayInputStream(bytes);
} else if (value instanceof CharSequence) {
final String string = ((CharSequence) value).toString();
final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
in = new ByteArrayInputStream(bytes);
} else {
try {
final Resource resource = Resource.getResource(value);
in = resource.newBufferedInputStream();
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException(value.getClass() + " not valid for a blob column");
}
}
try {
final PGConnection pgConnection = (PGConnection) ((DelegatingConnection<?>) statement.getConnection()).getInnermostDelegate();
final LargeObjectManager lobManager = pgConnection.getLargeObjectAPI();
final long lobId = lobManager.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE);
final LargeObject lob = lobManager.open(lobId, LargeObjectManager.WRITE);
try {
final byte[] buffer = new byte[2048];
int readCount = 0;
while ((readCount = in.read(buffer, 0, 2048)) > 0) {
lob.write(buffer, 0, readCount);
}
} finally {
lob.close();
}
statement.setLong(parameterIndex, lobId);
} catch (final IOException e) {
Exceptions.throwUncheckedException(e);
}
}
}
return parameterIndex + 1;
}
use of org.postgresql.largeobject.LargeObjectManager in project syndesis by syndesisio.
the class SqlFileStore method doReadPostgres.
/**
* Postgres does not allow to read from the large object after the connection has been closed.
*/
private InputStream doReadPostgres(String path) {
Handle h = dbi.open();
try {
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());
} else {
h.close();
return null;
}
} catch (SQLException e) {
IOUtils.closeQuietly(h);
throw DaoException.launderThrowable(e);
}
}
Aggregations