title: Encoding problems while extracting EXI-compressed XML
body: <p>The code below is an attempt to simplify the setup required to perform EXI compression and decompression using <a href="http://exificient.sourceforge.net/" rel="nofollow">EXIficient</a></p>
  
  <pre><code>class ExiCompressionUtils {
      static Transformer transformer = TransformerFactory.newInstance().newTransformer()
  
      static byte[] compress(String xml) {
          ByteArrayOutputStream exiOS = new ByteArrayOutputStream()
          EXIResult exiResult = new EXIResult(outputStream : exiOS)
  
          XMLReader xmlReader = XMLReaderFactory.createXMLReader()
          xmlReader.contentHandler = exiResult.handler
          xmlReader.parse(new InputSource(new StringReader(xml)))
  
          def compressed = exiOS.toByteArray()
          exiOS.close()
          return compressed
      }
  
      static String extract(byte[] compressed) {
          SAXSource exiSource = new SAXSource(new InputSource(new ByteArrayInputStream(compressed)))
          exiSource.setXMLReader(exiSource.reader)
  
          ByteArrayOutputStream exiOS = new ByteArrayOutputStream()
          transformer.transform(exiSource, new StreamResult(exiOS))  // fails here
          def extracted = exiOS.toString()
          exiOS.close()
          return compressed
      }
  }
  </code></pre>
  
  <p>The below test fails with <code>ERROR:  'Invalid byte 1 of 1-byte UTF-8 sequence.'</code></p>
  
  <pre><code>@Test
  void testExiCompression() {
      def xml = '&lt;Root&gt;&lt;Child id="1"&gt;Text&lt;/Child&gt;&lt;EmptyTag/&gt;&lt;/Root&gt;'
      def compressed = ExiCompressionUtils.compress(xml)
      assert ExiCompressionUtils.extract(compressed) == xml
  } 
  </code></pre>
  
  <p>Any encoding experts out there that can get to the bottom of this?</p>