commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1159060 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ java/org/apache/commons/runtime/io/ java/org/apache/commons/runtime/net/ java/org/apache/commons/runtime/util/ java/org/apache/commons/runtime/util/bzip...
Date Thu, 18 Aug 2011 06:47:09 GMT
Author: mturk
Date: Thu Aug 18 06:47:08 2011
New Revision: 1159060

URL: http://svn.apache.org/viewvc?rev=1159060&view=rev
Log:
Add bzip2 filter streams

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/DeflateStream.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/InflateStream.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2DeflateStream.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2InflateStream.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcStream.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Impl.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java
    commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java Thu
Aug 18 06:47:08 2011
@@ -188,9 +188,11 @@ public final class Memory
     {
         if (size < 1L)
             throw new InvalidArgumentException();
+        if (!(ptr instanceof HeapPointer))
+            throw new RuntimeException();
         long p = realloc0(ptr.POINTER, size);
         if (p == 0L)
-            throw new OutOfMemoryError("Memory re-allocation failed");
+            throw new OutOfMemoryError();
         ptr.POINTER = p;
         ptr.PLENGTH = size;
     }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Reader.java
Thu Aug 18 06:47:08 2011
@@ -245,44 +245,4 @@ public interface Reader
     public int read(ByteBuffer buffer)
         throws IOException;
 
-    /**
-     * Reads at most {@code count} bytes from the current position in this
-     * stream and stores them in the {@code ByteBuffer} {@code buffer}
-     * starting at {@code offset}.
-     * Blocks until {@code count} bytes have been read, the end of the stream
-     * is reached or an exception is thrown.
-     * <p>
-     * {@code ByteBuffer} must be allocated using {@code allocatedirect()}
-     * or obtained from {@link DirectByteBuffer}.
-     * </p>
-     *
-     * @param buffer
-     *          The {@code ByteBuffer} in which to store the bytes read from
-     *          this stream.
-     * @param offset
-     *          The initial position in {@code buffer} to store the bytes read
-     *          from this stream.
-     * @param count
-     *          The maximum number of bytes to store in {@code buffer}.
-     *
-     * @return The number of bytes actually read or {@code -1} if the end of
-     *         the stream has been reached.
-     *
-     * @throws IndexOutOfBoundsException
-     *          If {@code offset < 0} or {@code count < 0}, or if
-     *          {@code offset + count} is greater than the size of
-     *          {@code buffer}.
-     * @throws ClosedDescriptorException
-     *          If this stream is closed.
-     * @throws OperationWouldBlockException
-     *          If the stream is in nonblocking mode and the operation
-     *          would block.
-     * @throws TimeoutException
-     *          If read operation times out.
-     * @throws IOException
-     *          If some other I/O error occurs.
-     */
-    public int read(ByteBuffer buffer, int offset, int count)
-        throws IndexOutOfBoundsException, IOException;
-
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Stream.java
Thu Aug 18 06:47:08 2011
@@ -202,15 +202,6 @@ public abstract class Stream
     }
 
     @Override
-    public final int read(ByteBuffer buffer)
-        throws IOException
-    {
-        if (!canRead())
-            throw new OperationNotSupportedException();
-        return read(buffer, buffer.position(), buffer.remaining());
-    }
-
-    @Override
     public final int write(byte[] buffer)
         throws IOException
     {
@@ -241,13 +232,4 @@ public abstract class Stream
         return write(pointer.address() + off, count);
     }
 
-    @Override
-    public final int write(ByteBuffer buffer)
-        throws IOException
-    {
-        if (!canWrite())
-            throw new OperationNotSupportedException();
-        return write(buffer, buffer.position(), buffer.remaining());
-    }
-
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Writer.java
Thu Aug 18 06:47:08 2011
@@ -215,41 +215,6 @@ public interface Writer extends Syncable
         throws IOException;
 
     /**
-     * Writes {@code count} bytes from the {@code ByteBuffer} {@code buffer}
-     * to this stream, starting at the current stream position and
-     * using {@code offset} as the first position within {@code buffer}
-     * to get bytes.
-     * <p>
-     * {@code ByteBuffer} must be allocated using {@code allocatedirect()}
-     * or obtained from {@link DirectByteBuffer}.
-     * </p>
-     *
-     * @param buffer
-     *          The {@code ByteBuffer} to write to this stream.
-     * @param offset
-     *          The index of the first byte in {@code buffer} to write.
-     * @param count
-     *          The number of bytes from {@code buffer} to write.
-     * @return The number of bytes actually written.
-     *
-     * @throws IndexOutOfBoundsException
-     *          If {@code offset < 0} or {@code count < 0}, or if
-     *          {@code offset + count} is greater than the size of
-     *          {@code buffer}.
-     * @throws ClosedDescriptorException
-     *          If this stream is closed.
-     * @throws OperationWouldBlockException
-     *          If the stream is in nonblocking mode and the operation
-     *          would block.
-     * @throws TimeoutException
-     *          If write operation times out.
-     * @throws IOException
-     *          If some other I/O error occurs.
-     */
-    public int write(ByteBuffer buffer, int offset, int count)
-        throws IndexOutOfBoundsException, IOException;
-
-    /**
      * Writes {@code count} arrays from the array of byte arrays {@code array}
      * to this stream, starting at the current stream pointer and using
      * {@code offset} as the first position within {@code array} to get bytes.

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcStream.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcStream.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/IpcStream.java
Thu Aug 18 06:47:08 2011
@@ -223,12 +223,12 @@ class IpcStream extends Stream
     }
 
     @Override
-    public int read(ByteBuffer buffer, int offset, int count)
+    public int read(ByteBuffer buffer)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return read3(nd, buffer, offset, count);
+        return read3(nd, buffer, buffer.position(), buffer.remaining());
     }
 
     // === Writer methods
@@ -261,12 +261,12 @@ class IpcStream extends Stream
     }
 
     @Override
-    public int write(ByteBuffer buffer, int offset, int count)
+    public int write(ByteBuffer buffer)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return write3(nd, buffer, offset, count);
+        return write3(nd, buffer, buffer.position(), buffer.remaining());
     }
 
     @Override

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/SocketStream.java
Thu Aug 18 06:47:08 2011
@@ -223,12 +223,12 @@ class SocketStream extends Stream
     }
 
     @Override
-    public int read(ByteBuffer buffer, int offset, int count)
+    public int read(ByteBuffer buffer)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return read3(nd, buffer, offset, count);
+        return read3(nd, buffer, buffer.position(), buffer.remaining());
     }
 
     // === Writer methods
@@ -261,12 +261,12 @@ class SocketStream extends Stream
     }
     
     @Override
-    public int write(ByteBuffer buffer, int offset, int count)
+    public int write(ByteBuffer buffer)
         throws IndexOutOfBoundsException, IOException
     {
         if (closed())
             throw new ClosedDescriptorException();
-        return write3(nd, buffer, offset, count);
+        return write3(nd, buffer, buffer.position(), buffer.remaining());
     }
 
     @Override

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/DeflateStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/DeflateStream.java?rev=1159060&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/DeflateStream.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/DeflateStream.java
Thu Aug 18 06:47:08 2011
@@ -0,0 +1,98 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.runtime.util;
+
+import java.io.Closeable;
+import java.io.IOException;
+import org.apache.commons.runtime.io.FilterStream;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.io.OperationNotSupportedException;
+
+/**
+ * General purpose compression stream class.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public abstract class DeflateStream extends FilterStream
+{
+
+    /**
+     * Compressor to be used.
+     */
+    protected Deflater def;
+
+    /**
+     * Creates a new compressor stream.
+     */
+    protected DeflateStream(Stream s, Deflater def)
+    {
+        super(s);
+        this.def = def;
+    }
+
+    /**
+     * Releases all memory associated with this compressor stream.
+     * <p>
+     * Any unused input or output is discarded. While this method is
+     * used by {@code finalize()}, it can be called explicitly in order to
+     * free native resources before the next GC cycle.
+     * After {@code close()} was called other methods will typically throw
+     * an {@code IllegalStateException}.
+     * </p>
+     * @see java.io.Closeable#close()
+     * @throws IOException if an I/O error occurs.
+     */
+    @Override
+    public void close()
+        throws IOException
+    {
+        try {
+            def.close();
+        }
+        finally {
+            // This will call stream.close()
+            super.close();
+        }
+    }
+
+    @Override
+    public boolean canSeek()
+    {
+        return false;
+    }
+
+    @Override
+    public long skip(long count)
+        throws IOException
+    {
+        throw new OperationNotSupportedException();
+    }
+
+    @Override
+    public boolean closed()
+    {
+        return stream.closed();
+    }
+
+    @Override
+    public boolean valid()
+    {
+        return stream.valid();
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/DeflateStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java
Thu Aug 18 06:47:08 2011
@@ -19,6 +19,7 @@ package org.apache.commons.runtime.util;
 import java.io.Closeable;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.InvalidArgumentException;
 import org.apache.commons.runtime.InvalidDataException;
 import org.apache.commons.runtime.InvalidRangeException;
@@ -218,6 +219,16 @@ public abstract class Deflater implement
     }
 
     /**
+     * Fill the internal buffer from the given stream.
+     * The method reads from the stream {@code s} up to
+     * the internal buffer size.
+     *
+     * @param the strem to use.
+     */
+    public abstract void setInput(Stream s)
+        throws IOException;
+
+    /**
      * Returns the total number of bytes of input consumed by
      * the {@code Deflater}.
      *
@@ -235,22 +246,26 @@ public abstract class Deflater implement
 
     /**
      * Compressees the data (previously passed to {@code setInput})
-     * into the internal buffer.
+     * using the internal buffer and writes them to the given stream.
      * A return value of {@code 0} indicates that {@code needsInput()} should
      * be called in order to determine if more input data is required.
      * <p>
      * This method is used with inflate streams and uses internal buffer
      * for holding the temporary compressed data.
      * </p>
+     *
+     * @param s the stream to use.
      * @return the actual number of bytes of compressed data
+     * 
      * @throws InvalidDataException if the provided data was invalid
      * @throws OutOfMemoryError if the memory allocation failed
      * @throws NullPointerException if the inflater does not have internal
      *          buffer.
      */
-    public abstract int deflate()
+    public abstract int deflate(Stream s)
         throws InvalidDataException,
-               OutOfMemoryError;
+               OutOfMemoryError,
+               IOException;
 
     /**
      * Compressees the data (previously passed to {@code setInput})
@@ -377,6 +392,6 @@ public abstract class Deflater implement
             throw new InvalidArgumentException();
         return deflate(b.address(), len);
     }
-               
+
 }
 

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/InflateStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/InflateStream.java?rev=1159060&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/InflateStream.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/InflateStream.java
Thu Aug 18 06:47:08 2011
@@ -0,0 +1,98 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.runtime.util;
+
+import java.io.Closeable;
+import java.io.IOException;
+import org.apache.commons.runtime.io.FilterStream;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.io.OperationNotSupportedException;
+
+/**
+ * General purpose decompression stream class.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public abstract class InflateStream extends FilterStream
+{
+
+    /**
+     * Decompressor to be used.
+     */
+    protected Inflater inf;
+
+    /**
+     * Creates a new decompressor.
+     */
+    protected InflateStream(Stream s, Inflater inf)
+    {
+        super(s);
+        this.inf = inf;
+    }
+
+    /**
+     * Releases all memory associated with this decompressor stream.
+     * <p>
+     * Any unused input or output is discarded. While this method is
+     * used by {@code finalize()}, it can be called explicitly in order to
+     * free native resources before the next GC cycle.
+     * After {@code close()} was called other methods will typically throw
+     * an {@code IllegalStateException}.
+     * </p>
+     * @see java.io.Closeable#close()
+     * @throws IOException if an I/O error occurs.
+     */
+    @Override
+    public void close()
+        throws IOException
+    {
+        try {
+            inf.close();
+        }
+        finally {
+            // This will call stream.close()
+            super.close();
+        }
+    }
+
+    @Override
+    public boolean canSeek()
+    {
+        return false;
+    }
+
+    @Override
+    public long skip(long count)
+        throws IOException
+    {
+        throw new OperationNotSupportedException();
+    }
+
+    @Override
+    public boolean closed()
+    {
+        return stream.closed();
+    }
+
+    @Override
+    public boolean valid()
+    {
+        return stream.valid();
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/InflateStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java
Thu Aug 18 06:47:08 2011
@@ -19,6 +19,7 @@ package org.apache.commons.runtime.util;
 import java.io.Closeable;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.InvalidArgumentException;
 import org.apache.commons.runtime.InvalidDataException;
 import org.apache.commons.runtime.InvalidRangeException;
@@ -193,6 +194,17 @@ public abstract class Inflater implement
     }
 
     /**
+     * Fill the internal buffer from the given stream.
+     * The method reads from the stream {@code s} up to
+     * the internal buffer size.
+     *
+     * @param the strem to use.
+     */
+    public abstract void setInput(Stream s)
+        throws IOException;
+
+
+    /**
      * Returns total number of bytes of input read by the {@code Inflater}.
      *
      * @return the total number of bytes read.
@@ -207,8 +219,8 @@ public abstract class Inflater implement
     public abstract long getTotalOut();
 
     /**
-     * Uncompresses bytes from current input and stores them in
-     * the internal buffer.
+     * Uncompresses bytes from current input using the internall buffer
+     * and writes them to the given stream.
      * A return value of {@code 0} indicates that {@code needsInput()} should
      * be called in order to determine if more input data is required.
      * <p>
@@ -216,6 +228,7 @@ public abstract class Inflater implement
      * for holding the temporary uncompressed data.
      * </p>
      *
+     * @param s the stream to use.
      * @return the actual number of bytes of uncompressed data
      *
      * @throws InvalidDataException if the provided data was invalid
@@ -223,9 +236,10 @@ public abstract class Inflater implement
      * @throws NullPointerException if the inflater does not have internal
      *          buffer.
      */
-    public abstract int inflate()
+    public abstract int inflate(Stream s)
         throws InvalidDataException,
-               OutOfMemoryError;
+               OutOfMemoryError,
+               IOException;
 
     /**
      * Uncompresses up to {@code len} bytes from current input and stores
@@ -372,6 +386,7 @@ public abstract class Inflater implement
             throw new InvalidArgumentException();
         return inflate(b.address(), len);
     }
-               
+
 }
 
+

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2DeflateStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2DeflateStream.java?rev=1159060&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2DeflateStream.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2DeflateStream.java
Thu Aug 18 06:47:08 2011
@@ -0,0 +1,170 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.runtime.util.bzip2;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.InvalidDataException;
+import org.apache.commons.runtime.InvalidRangeException;
+import org.apache.commons.runtime.OperationNotPermittedException;
+import org.apache.commons.runtime.OverflowException;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.Limits;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.io.FilterStream;
+import org.apache.commons.runtime.io.OperationNotSupportedException;
+import org.apache.commons.runtime.util.Deflater;
+import org.apache.commons.runtime.util.DeflateStream;
+
+/**
+ * Bzip2 compressor stream.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public final class Bzip2DeflateStream extends DeflateStream
+{
+
+    /**
+     * Creates a new Bzip2 compressor stream.
+     */
+    public Bzip2DeflateStream(Stream s)
+    {
+        this(s, new Bzip2Deflater(9, Bzip2Impl.DEFAULT_WORK_FACTOR, Limits.PAGESIZE));
+    }
+
+    /**
+     * Creates a new Bzip2 decompressor stream with the specified
+     * decompressor.
+     */
+    public Bzip2DeflateStream(Stream s, Deflater def)
+    {
+        super(s, def);
+    }
+
+    @Override
+    public boolean canRead()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean canWrite()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean eof()
+        throws IOException
+    {
+        return def.finished();
+    }
+
+    @Override
+    public int read()
+        throws IOException
+    {
+        throw new OperationNotSupportedException();
+    }
+
+    @Override
+    public int read(byte[] buffer, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        def.setInput(stream);
+        return def.deflate(buffer, offset, count);
+    }
+
+    @Override
+    public int read(long address, int count)
+        throws NullPointerException, IndexOutOfBoundsException, IOException
+    {
+        def.setInput(stream);
+        return def.deflate(address, count);
+    }
+
+    @Override
+    public int read(ByteBuffer buffer)
+        throws IndexOutOfBoundsException, IOException
+    {
+        def.setInput(stream);
+        return def.deflate(buffer);
+    }
+
+    // === Writer methods
+
+    @Override
+    public int write(int b)
+        throws IOException
+    {
+        throw new OperationNotSupportedException();
+    }
+
+    @Override
+    public int write(byte[] buffer, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        def.setInput(buffer, offset, count);
+        return def.deflate(stream);
+    }
+
+    @Override
+    public int write(long address, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        def.setInput(address, count);
+        return def.deflate(stream);
+    }
+
+    @Override
+    public int write(ByteBuffer buffer)
+        throws IndexOutOfBoundsException, IOException
+    {
+        def.setInput(buffer);
+        return def.deflate(stream);
+    }
+
+    @Override
+    public long write(byte[][] array, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        // Simulate vector write for now
+        long w = 0;
+        for (int i = 0; i < count; i++) {
+            def.setInput(array[offset + i]);
+            w += def.deflate(stream);
+        }
+        return w;
+    }
+
+    @Override
+    public long write(ByteBuffer[] array, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        // Simulate vector write for now
+        long w = 0;
+        for (int i = 0; i < count; i++) {
+            def.setInput(array[offset + i]);
+            w += def.deflate(stream);
+        }
+        return w;
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2DeflateStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java
Thu Aug 18 06:47:08 2011
@@ -25,6 +25,7 @@ import org.apache.commons.runtime.Invali
 import org.apache.commons.runtime.OperationNotPermittedException;
 import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.util.Deflater;
 
 /**
@@ -37,6 +38,7 @@ public class Bzip2Deflater extends Defla
 {
 
     private long      handle;
+    private long      buffer;
     private int       bufferSize;
 
     private static native int     deflate0(long stream)
@@ -103,6 +105,8 @@ public class Bzip2Deflater extends Defla
             throw new OutOfMemoryError();
         }
         this.bufferSize = bufferSize;
+        if (bufferSize > 0)
+            buffer = handle + Bzip2Impl.SIZEOF_BZ_STREAM;
     }
 
     @Override
@@ -168,6 +172,21 @@ public class Bzip2Deflater extends Defla
     }
 
     @Override
+    public synchronized void setInput(Stream s)
+        throws IOException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (buffer == 0L)
+            throw new NullPointerException();
+        if (Bzip2Impl.needsInput(handle)) {
+            int len = s.read(buffer, bufferSize);
+            if (len > 0)
+                Bzip2Impl.setInput3(handle, len);
+        }
+    }
+
+    @Override
     public synchronized long getTotalIn()
     {
         if (handle == 0L)
@@ -204,15 +223,19 @@ public class Bzip2Deflater extends Defla
     }
         
     @Override
-    public int deflate()
+    public int deflate(Stream s)
         throws InvalidDataException,
-               OutOfMemoryError
+               OutOfMemoryError,
+               IOException
     {
-        if (bufferSize == 0)
+        if (buffer == 0L)
             throw new NullPointerException();
         if (handle == 0L)
             throw new IllegalStateException();
-        return deflate0(handle);
+        int n = deflate0(handle);
+        if (n > 0)
+            n = s.write(buffer, n);
+        return n;
     }
 
     @Override

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Impl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Impl.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Impl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Impl.java
Thu Aug 18 06:47:08 2011
@@ -58,7 +58,7 @@ final class Bzip2Impl
     public  static native int     setInput0(long handle, byte[] buf, int off, int len);
     public  static native int     setInput1(long handle, ByteBuffer buf, int off, int len);
     public  static native int     setInput2(long handle, long buf, int len);
-    public  static native void    setInput3(long handle);
+    public  static native void    setInput3(long handle, int len);
         
     public  static native int     getAvailIn(long handle);
     public  static native long    getAvailOut(long handle);

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2InflateStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2InflateStream.java?rev=1159060&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2InflateStream.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2InflateStream.java
Thu Aug 18 06:47:08 2011
@@ -0,0 +1,170 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.runtime.util.bzip2;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.InvalidDataException;
+import org.apache.commons.runtime.InvalidRangeException;
+import org.apache.commons.runtime.OperationNotPermittedException;
+import org.apache.commons.runtime.OverflowException;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.Limits;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.io.FilterStream;
+import org.apache.commons.runtime.io.OperationNotSupportedException;
+import org.apache.commons.runtime.util.Inflater;
+import org.apache.commons.runtime.util.InflateStream;
+
+/**
+ * Bzip2 decompressor stream.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public final class Bzip2InflateStream extends InflateStream
+{
+
+    /**
+     * Creates a new Bzip2 decompressor stream.
+     */
+    public Bzip2InflateStream(Stream s)
+    {
+        this(s, new Bzip2Inflater(false, Limits.PAGESIZE));
+    }
+
+    /**
+     * Creates a new Bzip2 decompressor stream with the specified
+     * decompressor.
+     */
+    public Bzip2InflateStream(Stream s, Inflater inf)
+    {
+        super(s, inf);
+    }
+
+    @Override
+    public boolean canRead()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean canWrite()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean eof()
+        throws IOException
+    {
+        return inf.finished();
+    }
+
+    @Override
+    public int read()
+        throws IOException
+    {
+        throw new OperationNotSupportedException();
+    }
+
+    @Override
+    public int read(byte[] buffer, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        inf.setInput(stream);
+        return inf.inflate(buffer, offset, count);
+    }
+
+    @Override
+    public int read(long address, int count)
+        throws NullPointerException, IndexOutOfBoundsException, IOException
+    {
+        inf.setInput(stream);
+        return inf.inflate(address, count);
+    }
+
+    @Override
+    public int read(ByteBuffer buffer)
+        throws IndexOutOfBoundsException, IOException
+    {
+        inf.setInput(stream);
+        return inf.inflate(buffer);
+    }
+
+    // === Writer methods
+
+    @Override
+    public int write(int b)
+        throws IOException
+    {
+        throw new OperationNotSupportedException();
+    }
+
+    @Override
+    public int write(byte[] buffer, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        inf.setInput(buffer, offset, count);
+        return inf.inflate(stream);
+    }
+
+    @Override
+    public int write(long address, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        inf.setInput(address, count);
+        return inf.inflate(stream);
+    }
+
+    @Override
+    public int write(ByteBuffer buffer)
+        throws IndexOutOfBoundsException, IOException
+    {
+        inf.setInput(buffer);
+        return inf.inflate(stream);
+    }
+
+    @Override
+    public long write(byte[][] array, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        // Simulate vector write for now
+        long w = 0;
+        for (int i = 0; i < count; i++) {
+            inf.setInput(array[offset + i]);
+            w += inf.inflate(stream);
+        }
+        return w;
+    }
+
+    @Override
+    public long write(ByteBuffer[] array, int offset, int count)
+        throws IndexOutOfBoundsException, IOException
+    {
+        // Simulate vector write for now
+        long w = 0;
+        for (int i = 0; i < count; i++) {
+            inf.setInput(array[offset + i]);
+            w += inf.inflate(stream);
+        }
+        return w;
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2InflateStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java
Thu Aug 18 06:47:08 2011
@@ -25,6 +25,7 @@ import org.apache.commons.runtime.Invali
 import org.apache.commons.runtime.OperationNotPermittedException;
 import org.apache.commons.runtime.OverflowException;
 import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.util.Inflater;
 
 /**
@@ -37,6 +38,7 @@ public final class Bzip2Inflater extends
 {
 
     private long      handle;
+    private long      buffer;
     private int       bufferSize;
 
     private static native int     inflate0(long stream)
@@ -93,6 +95,8 @@ public final class Bzip2Inflater extends
             throw new OutOfMemoryError();
         }
         this.bufferSize = bufferSize;
+        if (bufferSize > 0)
+            buffer = handle + Bzip2Impl.SIZEOF_BZ_STREAM;
     }
 
     @Override
@@ -160,6 +164,21 @@ public final class Bzip2Inflater extends
     }
 
     @Override
+    public void setInput(Stream s)
+        throws IOException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (buffer == 0L)
+            throw new NullPointerException();
+        if (Bzip2Impl.needsInput(handle)) {
+            int len = s.read(buffer, bufferSize);
+            if (len > 0)
+                Bzip2Impl.setInput3(handle, len);
+        }
+    }
+
+    @Override
     public synchronized long getTotalIn()
     {
         if (handle == 0L)
@@ -176,15 +195,22 @@ public final class Bzip2Inflater extends
     }
 
     @Override
-    public int inflate()
+    public int inflate(Stream s)
         throws InvalidDataException,
-               OutOfMemoryError
+               OutOfMemoryError,
+               IOException
     {
-        if (bufferSize == 0)
+        if (buffer == 0L)
             throw new NullPointerException();
         if (handle == 0L)
             throw new IllegalStateException();
-        return inflate0(handle);
+        int n = 0;
+        int i;
+        do {
+            if ((i = inflate0(handle)) > 0)
+                n += s.write(buffer, i);
+        } while (i == bufferSize);
+        return n;
     }
 
     @Override

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c Thu Aug 18 06:47:08 2011
@@ -338,14 +338,18 @@ ACR_BZIP2_EXPORT(jint, Bzip2Impl, setInp
     return rc;
 }
 
-ACR_BZIP2_EXPORT(void, Bzip2Impl, setInput3)(JNI_STDARGS, jlong stream)
+ACR_BZIP2_EXPORT(void, Bzip2Impl, setInput3)(JNI_STDARGS, jlong stream,
+                                             jint len)
 {
     acr_bzstream *s = J2P(stream, acr_bzstream *);
 
     ACR_MFREE(s->next_array);
     /* Use internal buffer */
     s->bz.next_in  = ACR_BZBUFF(s);
-    s->bz.avail_in = s->blen;    
+    if (len < 0)
+        s->bz.avail_in = s->blen;
+    else
+        s->bz.avail_in = len;
 }
 
 ACR_BZIP2_EXPORT(jlong, Bzip2Impl, getTotalIn)(JNI_STDARGS, jlong stream)

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java?rev=1159060&r1=1159059&r2=1159060&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java
Thu Aug 18 06:47:08 2011
@@ -76,4 +76,28 @@ public class TestBzip2 extends Assert
         bz.close();
     }
 
+    @Test(groups = { "core" })
+    public void bzInflater()
+        throws Exception
+    {
+        byte[] cd = loremIpsum.getBytes();
+        byte[] dd = new byte[500];
+        byte[] dc = new byte[50];
+
+        int sc = Bzip2.buffToBuffCompress(cd, 0, dd, 0, cd.length, 9, 30);
+
+        Bzip2Inflater bz = new Bzip2Inflater();
+        bz.setInput(dd);
+
+        int i = 0;
+        int n;
+        do {
+            if ((n = bz.inflate(dc)) > 0)
+                i += n;
+        } while (n == dc.length);
+        assertTrue(bz.finished());
+        assertEquals(i, loremIpsum.length());
+        bz.close();
+    }
+
 }



Mime
View raw message