Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4.2.2 create blob from unknown-length input stream

Hi i want to create a blob in hibernate from an inputstream, but i don't know the length of the stream.

Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(stream, length)

how can i crate a blob without knowing the length of the stream?

EDIT1

in older hibernate versions it was possible

http://viralpatel.net/blogs/tutorial-save-get-blob-object-spring-3-mvc-hibernate/

Blob blob = Hibernate.createBlob(file.getInputStream());

EDIT2

ok but it had an buggy implementation

return new SerializableBlob( new BlobImpl( stream, stream.available() ) );

stream.available isn't the real size

EDIT 3

i tried

session.doWork(new Work() {
            @Override
            public void execute(Connection conn) throws SQLException {
            LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI();

but conn is just a NewProxyConnection from c3p0.

like image 289
wutzebaer Avatar asked Nov 11 '13 14:11

wutzebaer


1 Answers

Here is what i'm using now

Session currentSession = getSessionFactory().getCurrentSession();
Blob blob = Hibernate.getLobCreator(currentSession).createBlob(new byte[0]);
OutputStream setBinaryStream = blob.setBinaryStream(1);
Utils.fastChannelCopy(input, setBinaryStream);
setBinaryStream.close();
like image 141
wutzebaer Avatar answered Nov 04 '22 03:11

wutzebaer