Search code examples
javaweb-servicesencodingdatahandler

Already Encoded String as DataHandler


So after looking at a similar SO Post about String and DataHandlers, I have run into a problem in terms of efficiency. I have a String coming into my web service that is already encoded. I have the need to convert this String into a DataHandler to put into a POJO and send off to another service. When I use new ByteArrayDataSource(String, type) it is encoding the already encoded String (a 2nd time).

To work around this I am decoding the input, and then passing the created byte[] into new ByteArrayDataSource(byte[], type). I am worried about the performance hit this will create when having to decode and re-encode images, when I already have an encoded version being passed in. Is there a way to create a DataHandler (or DataSource) from an already encoded String without needing the "MIME type", so I don't have to decode to encode?

I have tried using FileDataSource(String) but this was not working, with the same input used in a working ByteArrayDataSource(byte[], String) with decoded Strings. I also tried it passing in a blank string for the type using ByteArrayDataSource(String, type).


Solution

  • After a bit more searching I came across an Apache library that does this, but I am not sure how much more efficient it would be. Apache Axis2 has a ConverterUtil class that looks to have a lot of binary manipulation methods for Strings.

    Looking into that code a bit more, the code for convertToDataHandler(String) just passes the String along to the convertToBase64Binary(String) function. This is just creating a new ByteArrayDataSource from the String by decoding it and then going forward. So the util is just wrapping up the functionality I am already doing.