I was parsing an XML that has an image that is Base64 encoded. I would like to extract the image and parse the remainder of the XML. The code I have written to extract the image is as below:
private void saveFormImage(String imageText) throws IOException {
FileOutputStream fos = null;
try {
Base64 base64=new Base64();
byte decoded[]=base64.decode(imageText.getBytes());
File file = new File(<file loc>);
fos = new FileOutputStream(file);
fos.write(decoded);
} finally {
IOUtils.closeQuietly(fos);
}
}
I use JDOM to parse the XML and obtain the imageText first as a String and pass the string to this method. I then use the Apache codec library to decode the Base64 encoded data and store into a file.
Is this the best way to do this? This is not awfully fast. It finishes in about 2s. Is there a faster and memory efficient way of doing this?
As updated in a comment below - Is there a way to pipe the data from the XML directly onto an OutputStream and just decoding a buffer in memory? Is this a more memory efficient way of doing things? Or should this matter when the XML size would be max 2.5 MB.