March 5th, 2014

The Data Goes Round and Round

For the test project that I'm working on, I needed to take our binary data and present it in XML in exactly the format that we currently use, except that I had to do it in Java.

Ok, I can make that happen:

  • Open the document file with Apache POI.
  • Write the header for the XML document with the StAX XMLStreamWriter.
  • Write the header for a stream to the XML document with the XMLStreamWriter.
  • Open a stream from the document and read it into a byte array.
  • Compress it with the Deflater class.
  • Write the compressed and uncompressed length of the byte array to the XML document with the XMLStreamWriter.
  • Wrap the resulting compressed byte array in a ByteArrayInputStream.
  • Wrap that in a Base64InputStream to encode it.
  • Wrap that in a InputStreamReader to convert it to characters.
  • Now read characters from the InputStreamReader and write them out to the XMLStreamWriter, wrapped in an opening and closing tag.
  • Repeat until all streams are in the XML stream.

    I had to play a few games with the property sets, because I had used a different method to put those into the original XML response, but I eventually sorted that out.

    This is obviously non-optimum, because I really don't want to have to have both the compressed and uncompressed versions of the stream in memory at the same time, but since the current XML response is formatted that way, we'll live with it for the time being.