Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an InputStream to a DataHandler?

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.

like image 491
pcorey Avatar asked May 13 '10 21:05

pcorey


People also ask

How do I change InputStream to FileInputStream?

URL resource = classLoader. getResource("resource. ext"); File file = new File(resource. toURI()); FileInputStream input = new FileInputStream(file); // ...

How do you convert an InputStream to byteArray in Java?

Example 1: Java Program to Convert InputStream to Byte Arraybyte[] array = stream. readAllBytes(); Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.

How do you convert an InputStream into String in Java?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.


2 Answers

An implementation of answer from "Kathy Van Stone":

At first create helper class, which create DataSource from InputStream:

public class InputStreamDataSource implements DataSource {     private InputStream inputStream;      public InputStreamDataSource(InputStream inputStream) {         this.inputStream = inputStream;     }      @Override     public InputStream getInputStream() throws IOException {         return inputStream;     }      @Override     public OutputStream getOutputStream() throws IOException {         throw new UnsupportedOperationException("Not implemented");     }      @Override     public String getContentType() {         return "*/*";     }      @Override     public String getName() {         return "InputStreamDataSource";     } } 

And then you can create DataHandler from InputStream:

DataHandler dataHandler = new DataHandler(new InputStreamDataSource(inputStream)) 

imports:

import javax.activation.DataSource; import java.io.OutputStream; import java.io.InputStream; 
like image 93
bugs_ Avatar answered Sep 22 '22 21:09

bugs_


I also ran into this issue. If your source data is a byte[] Axis already has a class that wraps the InputStream and creates a DataHandler object. Here is the code

//this constructor takes byte[] as input ByteArrayDataSource rawData= new ByteArrayDataSource(resultSet.getBytes(1)); DataHandler data= new DataHandler(rawData); yourObject.setData(data); 

Related imports

import javax.activation.DataHandler; import org.apache.axiom.attachments.ByteArrayDataSource; 

Hope it helps!

like image 37
Jorge Pombar Avatar answered Sep 19 '22 21:09

Jorge Pombar