billroper: (Default)
[personal profile] billroper
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.

    Whee!
  • Profile

    billroper: (Default)
    billroper

    January 2026

    S M T W T F S
         1 2 3
    4 5 6 7 8910
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 3031

    Most Popular Tags

    Style Credit

    Expand Cut Tags

    No cut tags
    Page generated Feb. 1st, 2026 07:20 am
    Powered by Dreamwidth Studios