commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1158327 [1/2] - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/ java/org/apache/commons/runtime/net/ java/org/apache/commons/runtime/util/ java/org/apache/commons/runtime/util/bzip2/ native/include/acr/ nati...
Date Tue, 16 Aug 2011 15:40:08 GMT
Author: mturk
Date: Tue Aug 16 15:40:07 2011
New Revision: 1158327

URL: http://svn.apache.org/viewvc?rev=1158327&view=rev
Log:
Finish up (mostly) the bzip2 api

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/OperationNotSupportedException.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java   (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.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/Streamable.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/bzip2/Bzip2.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr/memory.h
    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/io/Device.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java?rev=1158327&r1=1158326&r2=1158327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Device.java Tue Aug 16 15:40:07 2011
@@ -42,5 +42,5 @@ public interface Device extends Closeabl
      * @return {@code true} if the device is closed; {@code false} otherwise.
      */
     public boolean closed();
-    
+
 }

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/OperationNotSupportedException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/OperationNotSupportedException.java?rev=1158327&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/OperationNotSupportedException.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/OperationNotSupportedException.java Tue Aug 16 15:40:07 2011
@@ -0,0 +1,40 @@
+/* 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.io;
+import java.io.IOException;
+
+/**
+ * OperationNotSupportedException is thrown when an attempt is made to
+ * invoke an non supported operation.
+ * This exception is usually thrown when the stream does not
+ * support read or write opperations.
+ *
+ * @since Runtime 1.0
+ */
+public class OperationNotSupportedException extends IOException
+{
+
+    public OperationNotSupportedException()
+    {
+        super();
+    }
+
+    public OperationNotSupportedException(String msg)
+    {
+        super(msg);
+    }
+}

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

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=1158327&r1=1158326&r2=1158327&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 Tue Aug 16 15:40:07 2011
@@ -140,6 +140,8 @@ public abstract class Stream
      * @throws OperationWouldBlockException
      *          If the stream is in nonblocking mode and the operation
      *          would block.
+     * @throws OperationNotSupportedException
+     *          If the stream is not seekable.
      * @throws TimeoutException
      *          If operation times out.
      * @throws IOException
@@ -171,6 +173,8 @@ public abstract class Stream
     public final int read(byte[] buffer)
         throws IOException
     {
+        if (!canRead())
+            throw new OperationNotSupportedException();
         return read(buffer, 0, buffer.length);
     }
 
@@ -178,6 +182,8 @@ public abstract class Stream
     public final int read(Pointer pointer)
         throws IOException
     {
+        if (!canRead())
+            throw new OperationNotSupportedException();
         return read(pointer, 0L, (int)pointer.sizeof());
     }
 
@@ -185,6 +191,8 @@ public abstract class Stream
     public final int read(ByteBuffer buffer)
         throws IOException
     {
+        if (!canRead())
+            throw new OperationNotSupportedException();
         return read(buffer, buffer.position(), buffer.remaining());
     }
 
@@ -192,6 +200,8 @@ public abstract class Stream
     public final int write(byte[] buffer)
         throws IOException
     {
+        if (!canWrite())
+            throw new OperationNotSupportedException();
         return write(buffer, 0, buffer.length);
     }
 
@@ -199,6 +209,8 @@ public abstract class Stream
     public final int write(Pointer pointer)
         throws IOException
     {
+        if (!canWrite())
+            throw new OperationNotSupportedException();
         return write(pointer, 0L, (int)pointer.sizeof());
     }
 
@@ -206,6 +218,8 @@ public abstract class Stream
     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/Streamable.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Streamable.java?rev=1158327&r1=1158326&r2=1158327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Streamable.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/Streamable.java Tue Aug 16 15:40:07 2011
@@ -27,6 +27,30 @@ public interface Streamable extends Devi
 {
 
     /**
+     * Test if this stream supports read operations.
+     *
+     * @return {@code true} if the read operations are
+     *          supported, {@code false} otherwise.
+     */
+    public boolean canRead();
+
+    /**
+     * Test if this stream supports write operations.
+     *
+     * @return {@code true} if the write operations are
+     *          supported, {@code false} otherwise.
+     */
+    public boolean canWrite();
+
+    /**
+     * Test if this stream supports seek operations.
+     *
+     * @return {@code true} if the seek operations are
+     *          supported, {@code false} otherwise.
+     */
+    public boolean canSeek();
+    
+    /**
      * Test the end-of-stream indicator.
      *
      * @return {@code true} if end-of-stream was reached.

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=1158327&r1=1158326&r2=1158327&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 Tue Aug 16 15:40:07 2011
@@ -27,6 +27,7 @@ import java.nio.ByteBuffer;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.Descriptor;
 import org.apache.commons.runtime.io.OperationInProgressException;
+import org.apache.commons.runtime.io.OperationNotSupportedException;
 import org.apache.commons.runtime.io.OperationWouldBlockException;
 import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.Errno;
@@ -112,13 +113,28 @@ class IpcStream extends Stream
     }
 
     @Override
+    public boolean canRead()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean canWrite()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean canSeek()
+    {
+        return false;
+    }
+
+    @Override
     public long skip(long count)
         throws IOException
     {
-        if (closed())
-            throw new ClosedDescriptorException();
-
-        return 0L;
+        throw new OperationNotSupportedException();
     }
 
     @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=1158327&r1=1158326&r2=1158327&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 Tue Aug 16 15:40:07 2011
@@ -27,6 +27,7 @@ import java.nio.ByteBuffer;
 import org.apache.commons.runtime.io.ClosedDescriptorException;
 import org.apache.commons.runtime.io.Descriptor;
 import org.apache.commons.runtime.io.OperationInProgressException;
+import org.apache.commons.runtime.io.OperationNotSupportedException;
 import org.apache.commons.runtime.io.OperationWouldBlockException;
 import org.apache.commons.runtime.io.Stream;
 import org.apache.commons.runtime.Errno;
@@ -112,13 +113,28 @@ class SocketStream extends Stream
     }
 
     @Override
+    public boolean canRead()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean canWrite()
+    {
+        return true;
+    }
+
+    @Override
+    public boolean canSeek()
+    {
+        return false;
+    }
+
+    @Override
     public long skip(long count)
         throws IOException
     {
-        if (closed())
-            throw new ClosedDescriptorException();
-
-        return 0L;
+        throw new OperationNotSupportedException();
     }
 
     @Override

Added: 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=1158327&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Deflater.java Tue Aug 16 15:40:07 2011
@@ -0,0 +1,331 @@
+/* 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 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;
+
+/**
+ * General purpose compression base class.
+ * <p>
+ * This class is similar to the {@code java.util.zip.Deflater} class
+ * with the additional data types.
+ * </p>
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public abstract class Deflater implements Closeable
+{
+
+    /**
+     * Creates a new compressor.
+     */
+    protected Deflater()
+    {
+    }
+
+    /**
+     * Releases all memory associated with this compressor.
+     * <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.
+     */
+    public abstract void close()
+        throws IOException;
+
+    /**
+     * Called by the garbage collector when the object is destroyed.
+     * The class will free internal resources allocated by the
+     * Operating system only if there are no additional references
+     * to this object.
+     * @see Object#finalize()
+     * @throws Throwable the {@code Exception} raised by this method.
+     */
+    @Override
+    protected final void finalize()
+        throws Throwable
+    {
+        try {
+            close();
+        } catch (Exception e) {
+            // Ignore
+        }
+    }
+
+    /**
+     * Returns true if all input data was processed.
+     * @return {@code true} if no data remains in the input buffer.
+     */
+    public abstract boolean needsInput();
+
+    /**
+     * Indicates to the {@code Deflater} that all uncompressed input
+     * has been provided to it.
+     * <p>
+     * User must call deflate util {@code -1} is returned or until
+     * {@code finished()} returns {@code true} indicating that end
+     * of stream has been reached.
+     * <p>
+     * @throws IllegalStateException if the call to this method is done
+     *          after the defalter was finished
+     */
+    public abstract void finish()
+        throws IllegalStateException;
+
+    /**
+     * Returns whether or not all provided data has been successfully
+     * compressed.
+     *
+     * @return {@code true} if all data has been compressed,
+     *         {@code false} otherwise.
+     */
+    public abstract boolean finished();
+   
+    /**
+     * Put this deflater into the flushing state.
+     * <p>
+     * User must call deflate until {@code needsInput()}
+     * returns false.
+     * </p>
+     * @throws IllegalStateException if the call to this method is done
+     *          after the deflater was finished
+     */
+    public abstract void flush()
+        throws IllegalStateException;
+        
+    /**
+     * Sets the input buffer the {@code Deflater} will use to extract
+     * uncompressed bytes for later compression.
+     * Input will be taken from the buffer region starting at {@code off} and
+     * ending at {@code nbytes - 1}.
+     * <p>
+     * This method should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     * </p>
+     *
+     * @param b the input data bytes.
+     * @param off the start offset of the input data
+     * @param len the length of the input data
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public abstract void setInput(byte[] b, int off, int len)
+        throws InvalidArgumentException;
+
+    /**
+     * Sets the input buffer the {@code Deflater} will use to extract
+     * uncompressed bytes for later compression.
+     *
+     * @param b the input data bytes.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public void setInput(byte[] b)
+        throws InvalidArgumentException
+    {
+        if (b == null)
+            throw new NullPointerException();
+        if (b.length < 1)
+            throw new InvalidArgumentException();
+        setInput(b, 0, b.length);
+    }
+        
+    /**
+     * Set input data for compression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b the input data byte buffer.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public abstract void setInput(ByteBuffer b)
+        throws InvalidArgumentException;
+
+    /**
+     * Set input data for compression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b pointer to the input data bytes.
+     * @param off the start offset of the input data
+     * @param len the length of the input data
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public abstract void setInput(Pointer b, long off, long len)
+        throws InvalidArgumentException;
+
+    /**
+     * Set input data for compression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b pointer to the input data bytes.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public void setInput(Pointer b)
+        throws InvalidArgumentException
+    {
+        if (b == null)
+            throw new NullPointerException();
+        setInput(b, 0L, b.sizeof());
+    }
+
+    /**
+     * Returns the total number of bytes of input consumed by
+     * the {@code Deflater}.
+     *
+     * @return number of bytes of input read.
+     */
+    public abstract long getTotalIn();
+
+    /**
+     * Returns the total number of bytes of input consumed by
+     * the {@code Deflater}.
+     *
+     * @return number of bytes of input read.
+     */
+    public abstract long getTotalOut();
+
+    /**
+     * Compressees the data (previously passed to {@code setInput})
+     * into the internal buffer.
+     * 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>
+     * @return the actual number of bytes of compressed data
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract int deflate()
+        throws InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Compressees the data (previously passed to {@code setInput})
+     * into the supplied buffer.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @return the actual number of bytes of compressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the input data was invalid or corrupt.
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract int deflate(byte[] b, int off, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Compressees the data (previously passed to {@code setInput})
+     * into the supplied buffer.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @return the actual number of bytes of compressed data or {@code -1}
+     *          if end of uncompressed data has been reached.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public int deflate(byte[] b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (b == null)
+            throw new NullPointerException();
+        if (b.length < 1)
+            throw new InvalidArgumentException();
+        return deflate(b, 0, b.length);
+    }
+
+    /**
+     * Compressees the data (previously passed to {@code setInput})
+     * into the supplied byte buffer.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @return the actual number of bytes of compressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the input data was invalid or corrupt.
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract int deflate(ByteBuffer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Compressees the data (previously passed to {@code setInput})
+     * into the supplied buffer.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @return the actual number of bytes of compressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the input data was invalid or corrupt.
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract long deflate(Pointer b, long off, long len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Compressees the data (previously passed to {@code setInput})
+     * into the supplied buffer.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @return the actual number of bytes of compressed data or {@code -1}
+     *          if end of uncompressed data has been reached.
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public long deflate(Pointer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (b == null)
+            throw new NullPointerException();
+        if (b.sizeof() < 1)
+            throw new InvalidArgumentException();
+        return deflate(b, 0, b.sizeof());
+    }
+               
+}
+

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

Added: 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=1158327&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Inflater.java Tue Aug 16 15:40:07 2011
@@ -0,0 +1,325 @@
+/* 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 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;
+
+/**
+ * General purpose decompression base class.
+ * <p>
+ * This class is similar to the {@code java.util.zip.Inflater} class
+ * with the additional data types.
+ * </p>
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public abstract class Inflater implements Closeable
+{
+
+    /**
+     * Creates a new decompressor.
+     */
+    protected Inflater()
+    {
+    }
+
+    /**
+     * Releases all memory associated with this decompressor.
+     * <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.
+     */
+    public abstract void close()
+        throws IOException;
+
+    /**
+     * Called by the garbage collector when the object is destroyed.
+     * The class will free internal resources allocated by the
+     * Operating system only if there are no additional references
+     * to this object.
+     * @see Object#finalize()
+     * @throws Throwable the {@code Exception} raised by this method.
+     */
+    @Override
+    protected final void finalize()
+        throws Throwable
+    {
+        try {
+            close();
+        } catch (Exception e) {
+            // Ignore
+        }
+    }
+
+    /**
+     * Returns true if all input data was processed.
+     *
+     * @return {@code true} if no data remains in the input buffer.
+     */
+    public abstract boolean needsInput();
+
+    /**
+     * Check if the end of compressed data has been reached.
+     *
+     * @return {@code true} if the end of compressed data stream has
+     *          been reached.
+     */
+    public abstract boolean finished();
+
+    /**
+     * Set input data for decompression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b the input data bytes.
+     * @param off the start offset of the input data
+     * @param len the length of the input data
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public abstract void setInput(byte[] b, int off, int len)
+        throws InvalidArgumentException;
+
+    /**
+     * Set input data for decompression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b the input data bytes.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public void setInput(byte[] b)
+        throws InvalidArgumentException
+    {
+        if (b == null)
+            throw new NullPointerException();
+        if (b.length < 1)
+            throw new InvalidArgumentException();
+        setInput(b, 0, b.length);
+    }
+        
+    /**
+     * Set input data for decompression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b the input data byte buffer.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public abstract void setInput(ByteBuffer b)
+        throws InvalidArgumentException;
+
+    /**
+     * Set input data for decompression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b pointer to the input data bytes.
+     * @param off the start offset of the input data
+     * @param len the length of the input data
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public abstract void setInput(Pointer b, long off, long len)
+        throws InvalidArgumentException;
+
+    /**
+     * Set input data for decompression.
+     * This should be called whenever the {@code needsInput()}
+     * returns {@code true} indicating that more input data is required.
+     *
+     * @param b pointer to the input data bytes.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     */
+    public void setInput(Pointer b)
+        throws InvalidArgumentException
+    {
+        if (b == null)
+            throw new NullPointerException();
+        setInput(b, 0L, b.sizeof());
+    }
+
+    /**
+     * Returns total number of bytes of input read by the {@code Inflater}.
+     *
+     * @return the total number of bytes read.
+     */
+    public abstract long getTotalIn();
+
+    /**
+     * Returns total number of bytes of input read by the {@code Inflater}.
+     *
+     * @return the total number of bytes read.
+     */
+    public abstract long getTotalOut();
+
+    /**
+     * Uncompresses bytes from current input and stores them in
+     * the internal buffer.
+     * 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 uncompressed data.
+     * </p>
+     *
+     * @return the actual number of bytes of uncompressed data
+     *
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract int inflate()
+        throws InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Uncompresses up to {@code len} bytes from current input and stores
+     * them in {@code buf} starting at {@code off}.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @param b the buffer to write decompressed bytes to.
+     * @param off the offset in buffer where to start writing decompressed data.
+     * @param len the number of inflated bytes to write to {@code b}.
+     *
+     * @return the actual number of bytes of uncompressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the input data was invalid or corrupt.
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract int inflate(byte[] b, int off, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Uncompresses bytes from current input and stores them in
+     * the {@code b}.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @param b the buffer to write decompressed bytes to.
+     *
+     * @return the actual number of bytes of uncompressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public int inflate(byte[] b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (b == null)
+            throw new NullPointerException();
+        if (b.length < 1)
+            throw new InvalidArgumentException();
+        return inflate(b, 0, b.length);
+    }
+
+    /**
+     * Uncompresses bytes from current input and stores them in
+     * the {@code b}.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @param b the buffer to write decompressed bytes to.
+     *
+     * @return the actual number of bytes of uncompressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract int inflate(ByteBuffer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Uncompresses up to {@code len} bytes from current input and stores
+     * them in {@code buf} starting at {@code off}.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @param b the buffer to write decompressed bytes to.
+     * @param off the offset in buffer where to start writing decompressed data.
+     * @param len the number of inflated bytes to write to {@code b}.
+     *
+     * @return the actual number of bytes of uncompressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the input data was invalid or corrupt.
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public abstract long inflate(Pointer b, long off, long len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError;
+
+    /**
+     * Uncompresses bytes from current input and stores them in
+     * the {@code b}.
+     * A return value of {@code 0} indicates that {@code needsInput()} should
+     * be called in order to determine if more input data is required.
+     *
+     * @param b the buffer to write decompressed bytes to.
+     *
+     * @return the actual number of bytes of uncompressed data or {@code -1}
+     *          if end of compressed data has been reached.
+     *
+     * @throws InvalidArgumentException if the provided parameters are invalid
+     * @throws InvalidDataException if the provided data was invalid
+     * @throws OutOfMemoryError if the memory allocation failed
+     */
+    public long inflate(Pointer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (b == null)
+            throw new NullPointerException();
+        if (b.sizeof() < 1)
+            throw new InvalidArgumentException();
+        return inflate(b, 0, b.sizeof());
+    }
+               
+}
+

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

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2.java?rev=1158327&r1=1158326&r2=1158327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2.java Tue Aug 16 15:40:07 2011
@@ -16,12 +16,12 @@
 
 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.util.StringManager;
@@ -32,7 +32,7 @@ import org.apache.commons.runtime.util.S
  * @author Mladen Turk
  * @since Runtime 1.0
  */
-public final class Bzip2 implements Closeable
+public final class Bzip2
 {
     /**
      * Bzip2 library version string.
@@ -75,81 +75,62 @@ public final class Bzip2 implements Clos
     public static final int    BZ_STREAM_END    = 4;
 
 
-
-    /**
-     * Pointer to the native bz_stream structure.
-     */
-    private final byte[] stream;
-
-    private static native String sver0();
-    private static native int    size0();
-    private static native void   init0(byte[] stream, int bufferSize);
-    private static native int    end0(byte[] stream);
-    private static native int    bzinit0(byte[] stream, int blockSize100k, int workFactor);
-    private static native int    bzinit1(byte[] stream, boolean small);
-
-    private static native int    nextinp0(byte[] stream, byte[] buf, int off, int len);
-    private static native int    nextinp1(byte[] stream, ByteBuffer buf, int off, int len);
-    private static native int    nextinp2(byte[] stream, long buf, long off, long len);
-    private static native int    nextout0(byte[] stream, byte[] buf, int off, int len);
-    private static native int    nextout1(byte[] stream, ByteBuffer buf, int off, int len);
-    private static native int    nextout2(byte[] stream, long buf, long off, long len);
-
-    private static native void   nextinp3(byte[] stream);
-    private static native void   nextout3(byte[] stream);
-
-    private static final int     DEFAULT_BUFFER_SIZE = 4096;
-    private static final int     DEFAULT_WORK_FACTOR = 30;
-
-    private static final int     SIZEOF_BZ_STREAM;
-
+    private static native String  version0();
+        
     /* Error codes */
-    private static final int     BZ_SEQUENCE_ERROR   = -1;
-    private static final int     BZ_PARAM_ERROR      = -2;
-    private static final int     BZ_MEM_ERROR        = -3;
-    private static final int     BZ_DATA_ERROR       = -4;
-    private static final int     BZ_DATA_ERROR_MAGIC = -5;
-    private static final int     BZ_IO_ERROR         = -6;
-    private static final int     BZ_UNEXPECTED_EOF   = -7;
-    private static final int     BZ_OUTBUFF_FULL     = -8;
-    private static final int     BZ_CONFIG_ERROR     = -9;
+    private static final int   BZ_SEQUENCE_ERROR   = -1;
+    private static final int   BZ_PARAM_ERROR      = -2;
+    private static final int   BZ_MEM_ERROR        = -3;
+    private static final int   BZ_DATA_ERROR       = -4;
+    private static final int   BZ_DATA_ERROR_MAGIC = -5;
+    private static final int   BZ_IO_ERROR         = -6;
+    private static final int   BZ_UNEXPECTED_EOF   = -7;
+    private static final int   BZ_OUTBUFF_FULL     = -8;
+    private static final int   BZ_CONFIG_ERROR     = -9;
+
+    /* Indicates the stream direction */
+    private boolean isCompressing = false;
 
     static {
-        VERSION = sver0();
-        SIZEOF_BZ_STREAM = size0();        
+        VERSION = version0();
     }
 
-    private static native int  compress0(byte[] src, int spos,
-                                         byte[] dst, int dpos, int dlen,
-                                         int len, int blockSize100k,
-                                         int workFactor)
+    private Bzip2()
+    {
+        // No instance
+    }
+
+    private static native int  b2bdeflate0(byte[] src, int spos,
+                                           byte[] dst, int dpos, int dlen,
+                                           int len, int blockSize100k,
+                                           int workFactor)
         throws InvalidArgumentException,
                OverflowException;
-    private static native int  compress1(ByteBuffer src, int spos,
-                                         ByteBuffer dst, int dpos, int dlen,
-                                         int len, int blockSize100k,
-                                         int workFactor)
+    private static native int  b2bdeflate1(ByteBuffer src, int spos,
+                                           ByteBuffer dst, int dpos, int dlen,
+                                           int len, int blockSize100k,
+                                           int workFactor)
         throws InvalidArgumentException,
                OverflowException;
-    private static native long compress2(long src, long dst, long dlen,
-                                         long len, int blockSize100k,
-                                         int workFactor)
+    private static native long b2bdeflate2(long src, long dst, long dlen,
+                                           long len, int blockSize100k,
+                                           int workFactor)
         throws InvalidArgumentException,
                InvalidRangeException,
                OverflowException;
-    private static native int  decompress0(byte[] src, int spos,
+    private static native int  b2binflate0(byte[] src, int spos,
                                            byte[] dst, int dpos, int dlen,
                                            int len, boolean small)
         throws InvalidArgumentException,
                InvalidDataException,
                OverflowException;
-    private static native int  decompress1(ByteBuffer src, int spos,
+    private static native int  b2binflate1(ByteBuffer src, int spos,
                                            ByteBuffer dst, int dpos, int dlen,
                                            int len, boolean small)
         throws InvalidArgumentException,
                InvalidDataException,
                OverflowException;
-    private static native long decompress2(long src,long dst, long dlen,
+    private static native long b2binflate2(long src,long dst, long dlen,
                                            long len, boolean small)
         throws InvalidArgumentException,
                InvalidDataException,
@@ -176,6 +157,7 @@ public final class Bzip2 implements Clos
      * @param workFactor controls how the compression phase behaves when
      *          presented with worst case. Allowable values range from
      *          {@code 0} to {@code 250} inclusive.
+     *
      * @return the size of the compressed data.
      *
      * @throws InvalidArgumentException if any of the argumets is invalid.
@@ -183,18 +165,23 @@ public final class Bzip2 implements Clos
      *          the size of the destination buffer.
      */
     public static int buffToBuffCompress(byte[] src,
-                                  int srcPos,
-                                  byte[] dest,
-                                  int destPos,
-                                  int length,
-                                  int blockSize100k,
-                                  int workFactor)
+                                         int srcPos,
+                                         byte[] dest,
+                                         int destPos,
+                                         int length,
+                                         int blockSize100k,
+                                         int workFactor)
         throws InvalidArgumentException,
                OverflowException
     {
-        /* TODO: Check for param validity */
-        return compress0(src, srcPos, dest, destPos, dest.length - destPos,
-                         length, blockSize100k, workFactor);
+        if (blockSize100k < 1 || blockSize100k > 9 || workFactor < 0 || workFactor > 250)
+            throw new InvalidArgumentException();
+        int destLen = dest.length - destPos;
+        if (srcPos < 0  || destPos < 0 || destLen < 1 || length < 1 ||
+            srcPos + length > src.length)
+            throw new ArrayIndexOutOfBoundsException();
+        return b2bdeflate0(src, srcPos, dest, destPos, destLen,
+                           length, blockSize100k, workFactor);
     }
 
     /**
@@ -220,16 +207,17 @@ public final class Bzip2 implements Clos
      *          the size of the destination buffer.
      */
     public static void buffToBuffCompress(ByteBuffer src,
-                                   ByteBuffer dest,
-                                   int blockSize100k,
-                                   int workFactor)
+                                          ByteBuffer dest,
+                                          int blockSize100k,
+                                          int workFactor)
         throws InvalidArgumentException,
                OverflowException
     {
-        /* TODO: Check for param validity */
-        int s = compress1(src, src.position(), dest, dest.position(),
-                          dest.limit(), src.limit(),
-                          blockSize100k, workFactor);
+        if (blockSize100k < 1 || blockSize100k > 9 || workFactor < 0 || workFactor > 250)
+            throw new InvalidArgumentException();
+        int s = b2bdeflate1(src, src.position(), dest, dest.position(),
+                            dest.limit(), src.limit(),
+                            blockSize100k, workFactor);
         dest.position(s);
     }
 
@@ -245,8 +233,8 @@ public final class Bzip2 implements Clos
      *
      * @param src the source data buffer.
      * @param srcPos starting position in the source buffer.
-     * @param dest destination buffer that will hold compressed data.
-     * @param destPos stating position in the destination buffer.
+     * @param dst destination buffer that will hold compressed data.
+     * @param dstPos stating position in the destination buffer.
      * @param length number of bytes to compress.
      * @param blockSize100k the block size to be used for compression. It
      *          should be a value between {@code 1} and {@code 9} inclusive.
@@ -270,11 +258,15 @@ public final class Bzip2 implements Clos
                InvalidRangeException,
                OverflowException
     {
+        if (blockSize100k < 1 || blockSize100k > 9 || workFactor < 0 || workFactor > 250)
+            throw new InvalidArgumentException();
         long srca = src.address() + srcPos;
         long dsta = dst.address() + dstPos;
-        /* TODO: Check for param validity */
-        return compress2(srca, dsta, dst.sizeof() - dstPos, length,
-                         blockSize100k, workFactor);
+        long dlen = dst.sizeof()  - dstPos;
+        if (dlen < 1L || srcPos < 0L || dstPos < 0L)
+            throw new IndexOutOfBoundsException();
+        return b2bdeflate2(srca, dsta, dlen, length,
+                           blockSize100k, workFactor);
     }
 
     /**
@@ -313,9 +305,11 @@ public final class Bzip2 implements Clos
                InvalidDataException,
                OverflowException
     {
-        /* TODO: Check for param validity */
-        return decompress0(src, srcPos, dest, destPos, dest.length - destPos,
-                           length, small);
+        int destLen = dest.length - destPos;
+        if (srcPos < 0  || destPos < 0 || destLen < 1 || length < 1 ||
+            srcPos + length > src.length)
+            throw new ArrayIndexOutOfBoundsException();
+        return b2binflate0(src, srcPos, dest, destPos, destLen, length, small);
     }
 
     /**
@@ -348,7 +342,7 @@ public final class Bzip2 implements Clos
                OverflowException
     {
         /* TODO: Check for param validity */
-        int p = decompress1(src, src.position(), dest, dest.position(),
+        int p = b2binflate1(src, src.position(), dest, dest.position(),
                             dest.limit(), src.limit(), small);
         dest.position(p);
     }
@@ -391,212 +385,11 @@ public final class Bzip2 implements Clos
     {
         long srca = src.address() + srcPos;
         long dsta = dst.address() + dstPos;
+        long dlen = dst.sizeof()  - dstPos;
+        if (dlen < 1L || srcPos < 0L || dstPos < 0L)
+            throw new IndexOutOfBoundsException();
         /* TODO: Check for param validity */
-        return decompress2(srca, dsta, dst.sizeof() - dstPos, length, small);
-    }
-
-    /**
-     * Initialize this Bzip2 instance for compression.
-     *
-     * @param blockSize100k the block size to be used for compression. It
-     *          should be a value between {@code 1} and {@code 9} inclusive.
-     * @param workFactor controls how the compression phase behaves when
-     *          presented with worst case. Allowable values range from
-     *          {@code 0} to {@code 250} inclusive.
-     * @return the size of the uncompressed data.
-     *
-     * @throws InvalidArgumentException if any of the argumets is invalid.
-     * @throws OutOfMemoryError if there was not enough memory.
-     */
-    public void compressInit(int blockSize100k, int workFactor)
-        throws InvalidArgumentException,
-               OutOfMemoryError
-    {
-        if (blockSize100k < 1)
-            blockSize100k = 1;
-        if (blockSize100k > 9)
-            blockSize100k = 9;
-        int rc = bzinit0(stream, blockSize100k, workFactor);
-        if (rc != BZ_OK) {
-            if (rc == BZ_MEM_ERROR)
-                throw new OutOfMemoryError();
-            else
-                throw new InvalidArgumentException();
-        }
-
-    }
-
-    /**
-     * Initialize this Bzip2 instance for decompression.
-     *
-     * @param small if {@code true} the library will use an alternative
-     *          decompression algorithm which uses less memory but at the cost
-     *          of decompressing more slowly.
-     * @return the size of the uncompressed data.
-     *
-     * @throws InvalidArgumentException if any of the argumets is invalid.
-     * @throws OutOfMemoryError if there was not enough memory.
-     */
-    public void decompressInit(boolean small)
-        throws InvalidArgumentException,
-               OutOfMemoryError
-    {
-        int rc = bzinit1(stream, small);
-        if (rc != BZ_OK) {
-            if (rc == BZ_MEM_ERROR)
-                throw new OutOfMemoryError();
-            else
-                throw new InvalidArgumentException();
-        }
-
-    }
-
-    /**
-     * Create Bzip2 instance
-     */
-    public Bzip2()
-    {
-        stream = new byte[SIZEOF_BZ_STREAM + DEFAULT_BUFFER_SIZE];
-        init0(stream, DEFAULT_BUFFER_SIZE);
-    }
-
-    /**
-     * Create Bzip2 instance
-     *
-     * @param bufferSize internal buffer size.
-     */
-    public Bzip2(int bufferSize)
-    {
-        stream = new byte[SIZEOF_BZ_STREAM + bufferSize];
-        init0(stream, bufferSize);
-    }
-
-    /**
-     * Create compressor Bzip2 instance
-     * @param blockSize100k the block size to be used for compression. It
-     *          should be a value between {@code 1} and {@code 9} inclusive.
-     * @param workFactor controls how the compression phase behaves when
-     *          presented with worst case. Allowable values range from
-     *          {@code 0} to {@code 250} inclusive.
-     * @param bufferSize internal buffer size.
-     */
-    public Bzip2(int blockSize100k, int workFactor, int bufferSize)
-    {
-        stream = new byte[SIZEOF_BZ_STREAM + bufferSize];
-        init0(stream, bufferSize);
-        if (workFactor < 0 || workFactor > 250)
-            workFactor = DEFAULT_WORK_FACTOR;
-        compressInit(blockSize100k, workFactor);
-    }
-
-    /**
-     * Create compressor Bzip2 instance
-     * @param blockSize100k the block size to be used for compression. It
-     *          should be a value between {@code 1} and {@code 9} inclusive.
-     * @param workFactor controls how the compression phase behaves when
-     *          presented with worst case. Allowable values range from
-     *          {@code 0} to {@code 250} inclusive.
-     */
-    public Bzip2(int blockSize100k, int workFactor)
-    {
-        stream = new byte[SIZEOF_BZ_STREAM + DEFAULT_BUFFER_SIZE];
-        init0(stream, DEFAULT_BUFFER_SIZE);
-        compressInit(blockSize100k, workFactor);
-    }
-
-    /**
-     * Create decompressor Bzip2 instance
-     *
-     * @param small if {@code true} the library will use an alternative
-     *          decompression algorithm which uses less memory but at the cost
-     *          of decompressing more slowly.
-     * @param bufferSize internal buffer size.
-     */
-    public Bzip2(boolean small, int bufferSize)
-    {
-        stream = new byte[SIZEOF_BZ_STREAM + bufferSize];
-        init0(stream, bufferSize);
-        decompressInit(small);
-    }
-
-    /**
-     * Create decompressor Bzip2 instance
-     *
-     * @param small if {@code true} the library will use an alternative
-     *          decompression algorithm which uses less memory but at the cost
-     *          of decompressing more slowly.
-     */
-    public Bzip2(boolean small)
-    {
-        stream = new byte[SIZEOF_BZ_STREAM + DEFAULT_BUFFER_SIZE];
-        init0(stream, DEFAULT_BUFFER_SIZE);
-        decompressInit(small);
-    }
-
-    /**
-     * Releases all memory associated with this stream.
-     * <p>
-     * Note that {@code Object.finalize()} method will call
-     * this function. However explicit call to {@code close()}
-     * allows better memory management.
-     * </p>
-     * @see java.io.Closeable#close()
-     * @throws IOException if an I/O error occurs.
-     */
-    public void close()
-        throws IOException
-    {
-        int rc = end0(stream);
-        if (rc != BZ_OK) {
-            /* TODO: Use descriptions from localstrings
-            */
-            throw new IOException();
-        }
-    }
-
-    /**
-     * Called by the garbage collector when the object is destroyed.
-     * The class will free internal resources allocated by the
-     * Operating system only if there are no additional references
-     * to this object.
-     * @see Object#finalize()
-     * @throws Throwable the {@code Exception} raised by this method.
-     */
-    @Override
-    protected final void finalize()
-        throws Throwable
-    {
-        end0(stream);
-    }
-
-    public void setNextIn(byte[] buff, int off, int len)
-        throws InvalidArgumentException
-    {
-        if (buff == null || len < 1)
-            throw new InvalidArgumentException();
-        int rc = nextinp0(stream, buff, off, len);
-        if (rc != BZ_OK)
-            throw new InvalidArgumentException();
-    }
-
-    public void setNextIn(ByteBuffer buff)
-        throws InvalidArgumentException
-    {
-        if (buff == null || buff.capacity() < 1)
-            throw new InvalidArgumentException();
-        int rc = nextinp1(stream, buff, buff.position(), buff.limit());
-        if (rc != BZ_OK)
-            throw new InvalidArgumentException();
-    }
-
-    public void setNextIn(Pointer buff, long off, long len)
-        throws InvalidArgumentException
-    {
-        if (buff == null || len < 1L)
-            throw new InvalidArgumentException();
-        int rc = nextinp2(stream, buff.address(), off, len);
-        if (rc != BZ_OK)
-            throw new InvalidArgumentException();
+        return b2binflate2(srca, dsta, dlen, length, small);
     }
 
 }

Added: 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=1158327&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Deflater.java Tue Aug 16 15:40:07 2011
@@ -0,0 +1,256 @@
+/* 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.util.Deflater;
+
+/**
+ * Bzip2 compressor.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public class Bzip2Deflater extends Deflater
+{
+
+    private long      handle;
+
+    private static native int     deflate0(long stream)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     deflate1(long stream, byte[] buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     deflate2(long stream, ByteBuffer buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native long    deflate3(long stream, long buf, long len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+
+    private static native void    close0(long stream);
+    
+    /**
+     * Creates a new compressor.
+     */
+    public Bzip2Deflater()
+    {
+        this(9, 30);
+    }
+
+    /**
+     * Creates a new Bzip2 compressor.
+     *
+     * @param blockSize100k the block size to be used for compression. It
+     *          should be a value between {@code 1} and {@code 9} inclusive.
+     * @param workFactor controls how the compression phase behaves when
+     *          presented with worst case. Allowable values range from
+     *          {@code 0} to {@code 250} inclusive.
+     */
+    public Bzip2Deflater(int blockSize100k, int workFactor)
+    {
+        this(blockSize100k, workFactor, 0);
+    }
+
+    /**
+     * Creates a new Bzip2 compressor.
+     *
+     * @param blockSize100k the block size to be used for compression. It
+     *          should be a value between {@code 1} and {@code 9} inclusive.
+     * @param workFactor controls how the compression phase behaves when
+     *          presented with worst case. Allowable values range from
+     *          {@code 0} to {@code 250} inclusive.
+     * @param bufferSize internal buffer size.
+     */
+    public Bzip2Deflater(int blockSize100k, int workFactor, int bufferSize)
+    {
+        if (bufferSize < 0)
+            throw new InvalidArgumentException();
+        if (blockSize100k < 1 || blockSize100k > 9)
+            throw new InvalidArgumentException();
+        if (workFactor < 0 || workFactor > 250)
+            throw new InvalidArgumentException();
+        if (workFactor == 0)
+            workFactor = Libbzip2.DEFAULT_WORK_FACTOR;
+        handle = Libbzip2.newStream(bufferSize);
+        int rc = Libbzip2.compressInit(handle, blockSize100k, workFactor);
+        if (rc != 0)
+            throw new OutOfMemoryError();
+    }
+
+    @Override
+    public synchronized void close()
+        throws IOException
+    {
+        System.out.println("Closing deflater " + handle);
+        if (handle != 0L) {
+            close0(handle);
+            handle = 0L;
+        }
+    }
+
+    @Override
+    public synchronized boolean needsInput()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return Libbzip2.needsInput(handle);
+    }
+
+    @Override
+    public synchronized boolean finished()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return Libbzip2.finished(handle);
+    }
+
+    @Override
+    public synchronized void setInput(byte[] b, int off, int len)
+        throws InvalidArgumentException
+    {
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (off < 0 || off + len > b.length)
+            throw new ArrayIndexOutOfBoundsException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        Libbzip2.setInput0(handle, b, off, len);
+    }
+
+    @Override
+    public synchronized void setInput(ByteBuffer b)
+        throws InvalidArgumentException
+    {
+        int len = b.limit() - b.position();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        Libbzip2.setInput1(handle, b, b.position(), len);
+    }
+
+    @Override
+    public synchronized void setInput(Pointer b, long off, long len)
+        throws InvalidArgumentException
+    {
+        if (len < 1L)
+            throw new InvalidArgumentException();
+        if (off < 0L || off + len > b.sizeof())
+            throw new IndexOutOfBoundsException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        Libbzip2.setInput2(handle, b.address() + off, len);
+    }
+
+    @Override
+    public synchronized long getTotalIn()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return Libbzip2.getTotalIn(handle);
+    }
+
+    @Override
+    public synchronized long getTotalOut()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return Libbzip2.getTotalOut(handle);
+    }
+
+    @Override
+    public synchronized void finish()
+        throws IllegalStateException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (!Libbzip2.finish(handle))
+            throw new IllegalStateException();
+    }
+   
+    @Override
+    public synchronized void flush()
+        throws IllegalStateException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (!Libbzip2.flush(handle))
+            throw new IllegalStateException();
+    }
+        
+    @Override
+    public int deflate()
+        throws InvalidDataException,
+               OutOfMemoryError
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return deflate0(handle);
+    }
+
+    @Override
+    public int deflate(byte[] b, int off, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return deflate1(handle, b, off, len);
+    }
+
+    @Override
+    public int deflate(ByteBuffer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        int len = b.limit() - b.position();
+        return deflate2(handle, b, b.position(), len);
+    }
+
+    @Override
+    public long deflate(Pointer b, long off, long len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (len < 1L)
+            throw new InvalidArgumentException();
+        if (off < 0L || off + len > b.sizeof())
+            throw new IndexOutOfBoundsException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return deflate3(handle, b.address() + off, len);
+    }
+
+}
+

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

Added: 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=1158327&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/bzip2/Bzip2Inflater.java Tue Aug 16 15:40:07 2011
@@ -0,0 +1,204 @@
+/* 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.util.Inflater;
+
+/**
+ * Bzip2 decompressor.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public final class Bzip2Inflater extends Inflater
+{
+
+    private long      handle;
+
+    private static native int     inflate0(long stream)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     inflate1(long stream, byte[] buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     inflate2(long stream, ByteBuffer buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native long    inflate3(long stream, long buf, long len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+
+    private static native void    close0(long stream);
+               
+    /**
+     * Creates a new Bzip2 decompressor.
+     */
+    public Bzip2Inflater()
+    {
+        this(false);
+    }
+
+    /**
+     * Create a new Bzip2 decompressor
+     *
+     * @param small if {@code true} the library will use an alternative
+     *          decompression algorithm which uses less memory but at the cost
+     *          of decompressing more slowly.
+     */
+    public Bzip2Inflater(boolean small)
+    {
+        this(small, 0);
+    }
+
+    /**
+     * Create a new Bzip2 decompressor
+     *
+     * @param small if {@code true} the library will use an alternative
+     *          decompression algorithm which uses less memory but at the cost
+     *          of decompressing more slowly.
+     * @param bufferSize internal buffer size.
+     */
+    public Bzip2Inflater(boolean small, int bufferSize)
+    {
+        if (bufferSize < 0)
+            throw new InvalidArgumentException();
+        handle = Libbzip2.newStream(bufferSize);
+        int rc = Libbzip2.decompressInit(handle, small);
+    }
+
+    @Override
+    public synchronized void close()
+        throws IOException
+    {
+        if (handle != 0L) {
+            close0(handle);
+            handle = 0L;
+        }
+    }
+
+    @Override
+    public synchronized boolean needsInput()
+    {
+        return Libbzip2.needsInput(handle);
+    }
+
+    @Override
+    public synchronized boolean finished()
+    {
+        return Libbzip2.finished(handle);
+    }
+
+    @Override
+    public synchronized void setInput(byte[] b, int off, int len)
+        throws InvalidArgumentException
+    {
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (off < 0 || off + len > b.length)
+            throw new ArrayIndexOutOfBoundsException();
+        Libbzip2.setInput0(handle, b, off, len);
+    }
+
+    @Override
+    public synchronized void setInput(ByteBuffer b)
+        throws InvalidArgumentException
+    {
+        int len = b.limit() - b.position();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        Libbzip2.setInput1(handle, b, b.position(), len);
+    }
+
+    @Override
+    public synchronized void setInput(Pointer b, long off, long len)
+        throws InvalidArgumentException
+    {
+        if (len < 1L)
+            throw new InvalidArgumentException();
+        if (off < 0L || off + len > b.sizeof())
+            throw new IndexOutOfBoundsException();
+        Libbzip2.setInput2(handle, b.address() + off, len);
+    }
+
+    @Override
+    public synchronized long getTotalIn()
+    {
+        return Libbzip2.getTotalIn(handle);
+    }
+
+    @Override
+    public synchronized long getTotalOut()
+    {
+        return Libbzip2.getTotalOut(handle);
+    }
+
+    @Override
+    public int inflate()
+        throws InvalidDataException,
+               OutOfMemoryError
+    {
+        return inflate0(handle);
+    }
+
+    @Override
+    public int inflate(byte[] b, int off, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        return inflate1(handle, b, off, len);
+    }
+
+    @Override
+    public int inflate(ByteBuffer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        int len = b.limit() - b.position();
+        return inflate2(handle, b, b.position(), len);
+    }
+
+    @Override
+    public long inflate(Pointer b, long off, long len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (len < 1L)
+            throw new InvalidArgumentException();
+        if (off < 0L || off + len > b.sizeof())
+            throw new IndexOutOfBoundsException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return inflate3(handle, b.address() + off, len);
+    }
+
+}

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

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/memory.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/memory.h?rev=1158327&r1=1158326&r2=1158327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/memory.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/memory.h Tue Aug 16 15:40:07 2011
@@ -52,6 +52,7 @@
                 ((Type *)AcrRealloc(env, (Mem), sizeof(Type) * (size_t)(Size)))
 #endif
 
+#define ACR_MFREE(Mem) AcrFree(Mem); (Mem) = 0
 #define ACR_FFREE(Mem) AcrFree((void *)(Mem))
 #define ACR_MEMZERO(Type, Mem, Size)                            \
     do {                                                        \



Mime
View raw message