Author: ron.sigal(a)jboss.com
Date: 2009-08-14 21:16:22 -0400 (Fri, 14 Aug 2009)
New Revision: 5337
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/compress/CompressingMarshaller.java
Log:
JBREM-1077: Made changes suggested by Doychin Bondzhev; reuses GZIPOutputStream and
BufferedOutputStream, just replacing the Deflater with each call.
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/compress/CompressingMarshaller.java
===================================================================
---
remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/compress/CompressingMarshaller.java 2009-08-15
00:59:57 UTC (rev 5336)
+++
remoting2/branches/2.2/src/main/org/jboss/remoting/marshal/compress/CompressingMarshaller.java 2009-08-15
01:16:22 UTC (rev 5337)
@@ -22,18 +22,19 @@
package org.jboss.remoting.marshal.compress;
-import org.jboss.remoting.marshal.Marshaller;
-import org.jboss.remoting.marshal.VersionedMarshaller;
-import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
-import org.jboss.remoting.serialization.SerializationStreamFactory;
-
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
+import java.util.zip.Deflater;
import java.util.zip.GZIPOutputStream;
+import org.jboss.remoting.marshal.Marshaller;
+import org.jboss.remoting.marshal.VersionedMarshaller;
+import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
+import org.jboss.remoting.serialization.SerializationStreamFactory;
+
/**
* <code>CompressingMarshaller</code> and
<code>CompressingUnMarshaller</code> are a general
* purpose compressing marshaller / decompressing unmarshaller pair based on Java's
GZIP facilities.
@@ -79,6 +80,13 @@
wrappedMarshaller = marshaller;
}
+ public OutputStream getMarshallingStream(OutputStream outputStream) throws
IOException
+ {
+ GZIPOutputStream gzos = new SelfCleaningGZipOutputStream(outputStream);
+ DecomposableBufferedOutputStream bos = new DecomposableBufferedOutputStream(gzos);
+ return bos;
+ }
+
/**
* Writes compressed, marshalled form of <code>dataObject</code> to
<code>output</code>.
@@ -89,9 +97,19 @@
*/
public void write(Object dataObject, OutputStream output, int version) throws
IOException
{
- output.flush();
- GZIPOutputStream gzos = new SelfCleaningGZipOutputStream(output);
- BufferedOutputStream bos = new BufferedOutputStream(gzos);
+ SelfCleaningGZipOutputStream gzos = null;
+ DecomposableBufferedOutputStream bos = null;
+ if (output instanceof DecomposableBufferedOutputStream)
+ {
+ bos = (DecomposableBufferedOutputStream) output;
+ gzos = (SelfCleaningGZipOutputStream) bos.getWrappedStream();
+ gzos.refreshDeflater();
+ }
+ else
+ {
+ gzos = new SelfCleaningGZipOutputStream(output);
+ bos = new DecomposableBufferedOutputStream(gzos);
+ }
ObjectOutputStream oos =
SerializationStreamFactory.getManagerInstance(getSerializationType()).createOutput(bos);
if(wrappedMarshaller != null)
@@ -129,10 +147,25 @@
*/
static class SelfCleaningGZipOutputStream extends GZIPOutputStream
{
+ boolean used;
+
public SelfCleaningGZipOutputStream(OutputStream out) throws IOException
{
super(out);
}
+
+ void refreshDeflater()
+ {
+ if (used)
+ {
+ def = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
+ crc.reset();
+ }
+ else
+ {
+ used = true;
+ }
+ }
/**
* Writes remaining compressed data to the output stream and closes the underlying
stream.
@@ -144,5 +177,23 @@
def.end(); // This will release all resources used by zlib native code
}
}
+
+ static class DecomposableBufferedOutputStream extends BufferedOutputStream
+ {
+ DecomposableBufferedOutputStream(OutputStream out, int size)
+ {
+ super(out, size);
+ }
+
+ DecomposableBufferedOutputStream(OutputStream out)
+ {
+ super(out);
+ }
+
+ OutputStream getWrappedStream()
+ {
+ return out;
+ }
+ }
}
Show replies by date