XStream
  1. XStream
  2. XSTR-176

Unexpected end of ZLIB input stream

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.1.1
    • Component/s: None
    • Labels:
      None
    • JDK version and platform:
      Sun 1.4.1_07 on Windows 2000

      Description

      Can I wrap plain java incoming and outgoing streams to implement compression and still have XStream work ?

      Versions: xstream-1.1.jar
      xpp3-1.1.2a.jar,
      also tried xpp3_min-1.1.3.4.I.jar

      Testing compression and serialization by wrapping streams (see code below), compression using java.util.zip package and serialization using java serialization works, but when I substitute xstream as serialization mechanism I see the following error. (XStream 1.1 works fine when no compression is used);

      "Unexpected end of ZLIB input stream
      at com.thoughtworks.xstream.io.xml.XppReader.next(XppReader.java:83)
      at com.thoughtworks.xstream.io.xml.XppReader.read(XppReader.java:133)
      at com.thoughtworks.xstream.io.xml.XppReader.moveDown(XppReader.java:92)
      at com.thoughtworks.xstream.io.xml.XppReader.<init>(XppReader.java:29)
      at com.thoughtworks.xstream.io.xml.XppDriver.createReader(XppDriver.java:22)
      Code:
      ..."

      Code:

      ///////////////////////////////////////////////////////////
      XSTREAM TEST CLASS
      ///////////////////////////////////////////////////////////

      package au.gov.wa.dpi.transport.accesslayer.serialization;

      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.OutputStream;
      import java.io.OutputStreamWriter;

      import com.thoughtworks.xstream.XStream;

      public class XmlSerializer implements SerializationStrategy {

      /** XStream xml serialization stream factory. */
      private XStream xstream;

      /**

      • Default Constructor for Implementation class for Xml Serialization
      • services
        */
        public XmlSerializer() { xstream = new XStream(); }

      /**

      • Implement the xml serialization marshalling mechanism.
      • @param outputStream
      • OutputStream to write serialization output
      • @throws IOException
      • when binary serialization fails
        */
        public ObjectOutputStream serialize(OutputStream outputStream) throws IOException {

      if (outputStream == null)

      { // Throw exception for null input stream throw new IllegalArgumentException("Invalid null outputStream parameter."); }

      // Pass Xml Serialization stream
      return xstream.createObjectOutputStream(new OutputStreamWriter(outputStream));
      }

      /**
      * J2SE Unmarshalling call from passed input stream.
      *
      * @param inputStream
      * InputStream to write deserialization output
      * @return Object having been deserialized
      * @throws IOException
      * when binary serialization fails
      */
      public ObjectInputStream deserialize(InputStream inputStream) throws IOException {

      if (inputStream == null) { // Throw exception for null input stream throw new IllegalArgumentException("Invalid null inputstream parameter."); }

      // Pass Xml deserialization stream
      return xstream.createObjectInputStream(new InputStreamReader(inputStream));
      }
      }

      ///////////////////////////////////////////////////////////
      COMPRESSION CLASS
      ///////////////////////////////////////////////////////////

      package au.gov.wa.dpi.transport.accesslayer.compression;

      import java.io.InputStream;
      import java.io.OutputStream;
      import java.util.zip.Deflater;
      import java.util.zip.DataFormatException;
      import java.util.zip.InflaterInputStream;
      import java.util.zip.DeflaterOutputStream;

      /**
      * This compression implementation leverages the java SDK Zlib mechanism.
      *
      * @author Gavin Bayfield
      */

      public class ZlibZip implements CompressionStrategy {

      /**
      * Deflate the passed payload. Returns the passed byte array parameter back
      * if null or empty.
      *
      * @param outputStream
      * OutputStream
      * @return OutputStream
      */
      public OutputStream compress(OutputStream outputStream) {

      if (outputStream == null) { // Throw exception for null input stream throw new IllegalArgumentException("Invalid null outputStream parameter."); }

      // Use explicit deflator
      return new DeflaterOutputStream(outputStream, new Deflater(Deflater.BEST_COMPRESSION));
      }

      /**

      • Inflate the passed payload. Returns the passed byte array parameter back
      • if null or empty.
      • @param inputStream
      • InputStream
      • @return InputStream
      • @throws DataFormatException
      • for binary serialization failure
        */
        public InputStream decompress(InputStream inputStream) throws DataFormatException {

      if (inputStream == null)

      { // Throw exception for null input stream throw new IllegalArgumentException("Invalid null inputStream parameter."); }

      // Wrap inputstream with inflater
      return new InflaterInputStream(inputStream);
      }
      }
      ////////////////////////////////////////////////////////

      Now... in http request handler
      ////////////////////////////////////////////////////////

      ...
      // Byte array output stream for processing
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      // Wrap with compression stream
      // XStream works if no compression implemented here (just returns stream)
      // Comprssion mechanism is java.util.zip
      OutputStream outputStream = compressor.compress(baos);

      // This class Always does a selected implementation of serialization
      // Serialize the request object
      ObjectOutputStream oos = serializer.serialize(outputStream);
      oos.writeObject(request);
      oos.flush();
      oos.close();

      // Convert data to input stream for http client transmission
      byte[] bytes = baos.toByteArray();
      logger.debug("Outgoing payload size is " + bytes.length);

      ...

      // On client side logging ...
      // With java serialization output is 446 bytes etc
      // With xstream serialation, byte array size always 2(two) bytes as if stream is not closed ??? or maybase cant wrap streams with the xstream ObjectOutputStream implementation ???

      Any reply would be greatly appreciated... thanks...
      Regards Gavin Bayfield

        People

        • Assignee:
          Unassigned
          Reporter:
          Gavin Bayfield
        • Votes:
          0 Vote for this issue
          Watchers:
          1 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: