Review use of the javax.activation.DataHandler object

On Liberty, each DataHandler object can be written to an output stream only once. Writing a DataHandler object to an OutputStream object more than once results in an empty file. After you call the javax.activation.DataHandler.writeTo(OutputStream) method, you cannot pass the DataHandler object to another method, return it, or store it for later use.

As a workaround, you can create a new DataHandler object and initialize the DataHandler object with the content that was already retrieved from the existing DataHandler object using the writeTo method. For example:

File f = new File("received_image");
if (f.exists()) {
f.delete();
}

FileOutputStream fos = new FileOutputStream(f);

// Write the DataHandler object to the output stream.
img_in.writeTo(fos);

// Create a new DataHandler object and initialize it with
// the content that was retrieved using the writeTo method above.

FileDataSource fos_out = new FileDataSource(f);

DataHandler img_out = new DataHandler(fos_out);


return img_out;