From do-not-reply at jboss.org Tue Nov 30 09:30:46 2010
Content-Type: multipart/mixed; boundary="===============4260531313124255632=="
MIME-Version: 1.0
From: do-not-reply at jboss.org
To: hornetq-commits at lists.jboss.org
Subject: [hornetq-commits] JBoss hornetq SVN: r9951 -
branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils.
Date: Tue, 30 Nov 2010 09:30:46 -0500
Message-ID: <201011301430.oAUEUkqE000315@svn01.web.mwc.hst.phx2.redhat.com>
--===============4260531313124255632==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Author: gaohoward
Date: 2010-11-30 09:30:46 -0500 (Tue, 30 Nov 2010)
New Revision: 9951
Added:
branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/Inf=
laterWriter.java
Modified:
branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/Def=
laterReader.java
branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/Inf=
laterReader.java
Log:
added InflaterWriter
Modified: branches/Branch_Large_Message_Compression/src/main/org/hornetq/ut=
ils/DeflaterReader.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/De=
flaterReader.java 2010-11-30 11:51:12 UTC (rev 9950)
+++ branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/De=
flaterReader.java 2010-11-30 14:30:46 UTC (rev 9951)
@@ -22,6 +22,8 @@
* A DeflaterReader
* The reader takes an inputstream and compress it.
* Not for concurrent use.
+
+ * @author Howard Gao
*
*/
public class DeflaterReader
Modified: branches/Branch_Large_Message_Compression/src/main/org/hornetq/ut=
ils/InflaterReader.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/In=
flaterReader.java 2010-11-30 11:51:12 UTC (rev 9950)
+++ branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/In=
flaterReader.java 2010-11-30 14:30:46 UTC (rev 9951)
@@ -26,6 +26,7 @@
* It takes an compressed input stream and decompressed it as it is being =
read.
* Not for concurrent use.
*
+ * @author Howard Gao
*/
public class InflaterReader extends InputStream
{
Added: branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils=
/InflaterWriter.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/In=
flaterWriter.java (rev 0)
+++ branches/Branch_Large_Message_Compression/src/main/org/hornetq/utils/In=
flaterWriter.java 2010-11-30 14:30:46 UTC (rev 9951)
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2010 Red Hat, Inc.
+ * Red Hat licenses this file to you under the Apache License, version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package org.hornetq.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+
+/**
+ * A InflaterWriter
+ * =
+ * This class takes an OutputStream. Compressed bytes =
+ * can directly be written into this class. The class will
+ * decompress the bytes and write them to the output stream.
+ * =
+ * Not for concurrent use.
+ * =
+ * @author Howard Gao
+ *
+ */
+public class InflaterWriter extends OutputStream
+{
+ private Inflater inflater =3D new Inflater();
+ private OutputStream output;
+ =
+ private byte[] writeBuffer =3D new byte[1024];
+ private int writePointer =3D 0;
+ =
+ private byte[] outputBuffer =3D new byte[writeBuffer.length*2];
+ =
+ public InflaterWriter(OutputStream output)
+ {
+ this.output =3D output;
+ }
+
+ /*
+ * Write a compressed byte.
+ */
+ @Override
+ public void write(int b) throws IOException
+ {
+ writeBuffer[writePointer] =3D (byte)(b & 0xFF);
+ writePointer++;
+ =
+ if (writePointer =3D=3D writeBuffer.length)
+ {
+ writePointer =3D 0;
+ try
+ {
+ doWrite();
+ }
+ catch (DataFormatException e)
+ {
+ throw new IOException("Error decompressing data", e);
+ }
+ }
+ }
+ =
+ @Override
+ public void close() throws IOException
+ {
+ if (writePointer > 0)
+ {
+ inflater.setInput(writeBuffer, 0, writePointer);
+ try
+ {
+ int n =3D inflater.inflate(outputBuffer);
+ while (n > 0)
+ {
+ output.write(outputBuffer, 0, n);
+ n =3D inflater.inflate(outputBuffer);
+ }
+ output.close();
+ }
+ catch (DataFormatException e)
+ {
+ throw new IOException(e);
+ }
+ }
+ }
+ =
+ private void doWrite() throws DataFormatException, IOException
+ {
+ inflater.setInput(writeBuffer);
+ int n =3D inflater.inflate(outputBuffer);
+ =
+ while (n > 0)
+ {
+ output.write(outputBuffer, 0, n);
+ n =3D inflater.inflate(outputBuffer);
+ }
+ }
+ =
+ public static void main(String[] args) throws IOException
+ {
+ String inputString =3D "blahblahblah??blahblahblahblahblah??blablahb=
lah??blablahblah??bla";
+ byte[] input =3D inputString.getBytes("UTF-8");
+ byte[] output =3D new byte[30];
+ Deflater compresser =3D new Deflater();
+ compresser.setInput(input);
+ compresser.finish();
+ int compressedDataLength =3D compresser.deflate(output);
+ System.err.println("compress len: " + compressedDataLength);
+
+ byte[] zipBytes =3D new byte[compressedDataLength];
+ =
+ System.arraycopy(output, 0, zipBytes, 0, compressedDataLength);
+ ByteArrayInputStream byteInput =3D new ByteArrayInputStream(zipBytes=
);
+ =
+ ByteArrayOutputStream byteOutput =3D new ByteArrayOutputStream();
+ InflaterWriter writer =3D new InflaterWriter(byteOutput);
+ =
+ byte[] zipBuffer =3D new byte[12];
+ =
+ int n =3D byteInput.read(zipBuffer);
+ while (n > 0)
+ {
+ System.out.println("Writing: " + n);
+ writer.write(zipBuffer, 0, n);
+ n =3D byteInput.read(zipBuffer);
+ }
+
+ writer.close();
+ =
+ byte[] outcome =3D byteOutput.toByteArray();
+ String outStr =3D new String(outcome);
+ =
+ System.out.println("Outcome: " + outStr);
+ =
+ }
+
+}
--===============4260531313124255632==--