title: How to convert an InputStream to a DataHandler?
body: <p>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 <code>getBytes</code> on our result set:</p>
  
  <pre><code>byte[] bytes = resultSet.getBytes(1);
  ...
  </code></pre>
  
  <p>This byte array was then converted into a <code>DataHandler</code> using the obvious constructor:</p>
  
  <pre><code>dataHandler=new DataHandler(bytes,"application/octet-stream");
  </code></pre>
  
  <p>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 <code>DataHandler</code> out of that simply requires too much memory.</p>
  
  <p>My immediate idea is to retrieve a stream of the data in the database with <code>getBinaryStream</code> and somehow convert that <code>InputStream</code> into a <code>DataHandler</code> in a memory-efficient way.  Unfortunately it doesn't seem like there's a direct way to convert an <code>InputStream</code> into a <code>DataHandler</code>. Another idea I've been playing with is reading chunks of data from the <code>InputStream</code> and writing them to the <code>OutputStream</code> of the <code>DataHandler</code>. But... I can't find a way to create an "empty" <code>DataHandler</code> that returns a non-null <code>OutputStream</code> when I call <code>getOutputStream</code>...</p>
  
  <p>Has anyone done this? I'd appreciate any help you can give me or leads in the right direction.</p>