use of org.postgresql.largeobject.LargeObject in project syndesis by syndesisio.
the class SqlFileStore method doWritePostgres.
private 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);
}
}
use of org.postgresql.largeobject.LargeObject in project activemq-artemis by apache.
the class PostgresSequentialSequentialFileDriver method getPostGresLargeObjectSize.
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;
}
use of org.postgresql.largeobject.LargeObject in project activemq-artemis by apache.
the class PostgresSequentialSequentialFileDriver method writeToFile.
@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;
}
}
use of org.postgresql.largeobject.LargeObject in project activemq-artemis by apache.
the class PostgresSequentialSequentialFileDriver method readFromFile.
@Override
public int readFromFile(JDBCSequentialFile file, ByteBuffer bytes) throws SQLException {
LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
LargeObject largeObject = null;
long oid = getOID(file);
synchronized (connection) {
try {
connection.setAutoCommit(false);
largeObject = lobjManager.open(oid, LargeObjectManager.READ);
int readLength = (int) calculateReadLength(largeObject.size(), bytes.remaining(), file.position());
if (readLength > 0) {
if (file.position() > 0)
largeObject.seek((int) file.position());
byte[] data = largeObject.read(readLength);
bytes.put(data);
}
largeObject.close();
connection.commit();
return readLength;
} catch (SQLException e) {
connection.rollback();
throw e;
}
}
}
use of org.postgresql.largeobject.LargeObject 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;
}
Aggregations