Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need a DataHandler?

What is the main reason of using a javax.activation.DataHandler?

Is it to facilitate the transfer of objects, that do not implement Serializable, over the network?

I.e. for instance I have seen the conversion of files, from local file systems, to bytes and then create a DataHandler with these bytes and transfer the DataHandler over the network.
Is this the reason that one would use DataHandler?

like image 918
Cratylus Avatar asked Mar 10 '11 17:03

Cratylus


2 Answers

I'll lead off with the start of the description from the API entry for DataHandler:

The DataHandler class provides a consistent interface to data available in many different sources and formats. It manages simple stream to string conversions and related operations using DataContentHandlers.

Admittedly, that's not the clearest description. DataHandler has to do with XML and SOAP, which you can see from the the use tab of its API page. Like you, I've used it to represent data about an uploaded file as it's being sent from one web service component to another for processing.

The Transferable interface that DataHandler implements is not exactly referring to "transfer" of the kind serialization deals with. It's about transfer of information between separate components in a program, or separate programs, not saving an object for later use. See the API entry for Transferable for more. You'll notice that it links to the Drag 'n' Drop Java Tutorial, which has little to do with DataHandler but does illustrate a use of Transferable.

like image 198
Pops Avatar answered Nov 11 '22 07:11

Pops


There are also performance considerations i.e. using a javax.activation.DataHandler for a SOAP attachment will improve performance.

e.g. as mentioned by Oracle "...Improved Performance: Informal tests have shown that using DataHandler wrappers doubles throughput for image/gif MIME types, and multiplies throughput by approximately 1.5 for text/xml or java.awt.Image for image/* types...." this is from LINK

Other references

  • A discussion of Base64 encoding vs. binary attachment types MTOM and DataHandler LINK
  • Apache CXF on MTOM LINK
like image 35
Wayne Earnshaw Avatar answered Nov 11 '22 07:11

Wayne Earnshaw