views:

660

answers:

1

I'm working on a java web application in which files will be stored in a database. Originally we retrieved files already in the DB by simply calling getBytes on our result set:

byte[] bytes = resultSet.getBytes(1);
...

This byte array was then converted into a DataHandler using the obvious constructor:

dataHandler=new DataHandler(bytes,"application/octet-stream");

This worked great until we started trying to store and retrieve larger files. Dumping the entire file contents into a byte array and then building a DataHandler out of that simply requires too much memory.

My immediate idea is to retrieve a stream of the data in the database with getBinaryStream and somehow convert that InputStream into a DataHandler in a memory-efficient way. Unfortunately it doesn't seem like there's a direct way to convert an InputStream into a DataHandler. Another idea I've been playing with is reading chunks of data from the InputStream and writing them to the OutputStream of the DataHandler. But... I can't find a way to create an "empty" DataHandler that returns a non-null OutputStream when I call getOutputStream...

Has anyone done this? I'd appreciate any help you can give me or leads in the right direction.

+1  A: 

My approach would be to write a custom class implementing DataSource that wraps your InputStream. Then create the DataHandler giving it the created DataSource.

Kathy Van Stone
Ah, that's a great idea. I'll try that when I get a chance.
pcorey
I thought the same. But beware, that then the DataHandler must be used (consume its input), "inside you loop", while the ResultSet is open. For example, you cant probably pass the DataHandler object to an upper layer.
leonbloy
@leonbloy The stated goal was to process the data without copying it from result set. This implies that the result set must be open the entire time regardless of how you do it.
Kathy Van Stone