You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/05/28 10:56:17 UTC

svn commit: r779488 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java

Author: mturk
Date: Thu May 28 08:56:16 2009
New Revision: 779488

URL: http://svn.apache.org/viewvc?rev=779488&view=rev
Log:
Make unsafe methods as protected.

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java?rev=779488&r1=779487&r2=779488&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java Thu May 28 08:56:16 2009
@@ -74,43 +74,54 @@
         IndexOutOfBoundsException;
 
     /**
-     * Allocate a new ByteBuffer from memory.
-     * @param size The amount of memory to allocate
-     * @return The ByteBuffer with allocated memory
+     * Allocate a new {@code ByteBuffer} from {@link Pointer} memory area.
+     * <p>
+     * <b>Warning:</b><br/>
+     * Allocated array elements share the same memory segment
+     * so the {@code free()} must <b>not</b> be called on the retured
+     * {@code ByteBuffer}, neither it can be used after the {@code ptr}
+     * has been destroyed.
+     * </p>
+     * @param ptr Memory area to use.
+     * @param off Start offset of the {@code ptr} memory area.
+     * @param len The length of the {@code ptr} memory areat to use.
+     * @return The {@code ByteBuffer} backed by the {@code ptr} memory area.
      *
-     * @throws OutOfMemoryError if memory cannot be allocated from the system.
-     * @throws IllegalArgumentException if {@code size} is invalid.
+     * @throws NullPointerException if {@code ptr} is null.
+     * @throws IllegalArgumentException if {@code off} or {@code len}
+     *         is invalid.
+     * @throws IndexOutOfBoundsException if copying would cause access of
+     *         data outside array bounds.
      */
-    public static ByteBuffer allocate(long size)
-        throws OutOfMemoryError, IllegalArgumentException
+    public static ByteBuffer allocate(Pointer ptr, long off, long len)
+        throws NullPointerException, IllegalArgumentException,
+        IndexOutOfBoundsException
     {
-        return alloc0(size);
+        return alloc4(ptr, off, len);
     }
 
     /**
-     * Allocate a new ByteBuffer from memory and set all of the memory
-     * bytes to zero.
-     * @param size Length in bytes of each element.
-     * @return The ByteBuffer with allocated memory
+     * Allocate a new {@code ByteBuffer} from {@link Pointer} memory area.
+     * <p>
+     * <b>Warning:</b><br/>
+     * Allocated array elements share the same memory segment
+     * so the {@code free()} must <b>not</b> be called on the retured
+     * {@code ByteBuffer}, neither it can be used after the {@code ptr}
+     * has been destroyed.
+     * </p>
+     * @param ptr Memory area to use.
+     * @return The {@code ByteBuffer} backed by the {@code ptr} memory area.
      *
-     * @throws OutOfMemoryError if memory cannot be allocated from the system.
-     * @throws IllegalArgumentException if {@code size} is invalid.
+     * @throws NullPointerException if {@code ptr} is null.
      */
-    public static ByteBuffer allocateAndClear(long size)
-        throws OutOfMemoryError, IllegalArgumentException
+    public static ByteBuffer allocate(Pointer ptr)
+        throws NullPointerException
     {
-        return alloc1(size);
+        return alloc5(ptr);
     }
 
     /**
-     * Allocate a new ByteBuffer array from memory.
-     * <p>
-     * <b>Warning:</b><br/>
-     * Allocated array elements share the same memory segment
-     * so the {@code free()} must be called only for a
-     * <b>first</b> returned array element since it contains the
-     * start of the allocated memory pointer.
-     * </p>
+     * Allocate a new ByteBuffer array from memory contained by {@code ptr}.
      * <p>
      * Memory is internally aligned to 8 byte boundary
      * meaning that there might be unused chunks of memory if
@@ -122,7 +133,13 @@
      * memory will be {@code 16} bytes because each requested
      * size is aligned to 8 byte boundary.
      * </p>
+     * <p> Pointer {@code ptr} doesn't have to be pre-allocated. It can be an
+     * empty referece type pointer created by {@code Memory.malloc()} in
+     * which case it will contain the needed memory segment as requested
+     * by the {@code sizes} array.
+     * </p>
      *
+     * @param ptr {@link Pointer} that holds the native memory area.
      * @param sizes Array of lengths for each ByteBuffer.
      * @param off Start offset of the {@code sizes} array.
      * @param len The length of the {@code sizes} array to use.
@@ -134,11 +151,11 @@
      * @throws IndexOutOfBoundsException if copying would cause access of
      *         data outside array bounds.
      */
-    public static ByteBuffer[] allocate(long sizes[], int off, int len)
+    public static ByteBuffer[] allocate(Pointer ptr, long sizes[], int off, int len)
         throws NullPointerException, IllegalArgumentException,
         IndexOutOfBoundsException
     {
-        return alloc2(sizes, off, len);
+        return alloc6(ptr, sizes, off, len);
     }
 
     /**
@@ -149,21 +166,16 @@
      * the requested sizes are not aligned to a specified boundary.
      * </p>
      * <p>
-     * For example if two ByteBuffers are allocated with sizes
-     * {@code 2} and {@code 3} the overall allocated
-     * memory will be {@code 16} bytes because each requested
-     * size is aligned to 8 byte boundary.
-     * </p>
-     * <p> Pointer {@code ptr} doesn't have to be pre-allocated. It can be an
-     * empty referece type pointer created by {@code Memory.malloc()} in
-     * which case it will contain the needed memory segment as requested
-     * by the {@code sizes} array.
+     * This method does the same as calling the:
+     * <pre>
+     *
+     * DirectByteBuffer.allocate(ptr, sizes, 0, sizes.length);
+     *
+     * </pre>
      * </p>
      *
      * @param ptr {@link Pointer} that holds the native memory area.
      * @param sizes Array of lengths for each ByteBuffer.
-     * @param off Start offset of the {@code sizes} array.
-     * @param len The length of the {@code sizes} array to use.
      * @return The ByteBuffer array with allocated memory
      *
      * @throws OutOfMemoryError if memory cannot be allocated from the system.
@@ -172,31 +184,130 @@
      * @throws IndexOutOfBoundsException if copying would cause access of
      *         data outside array bounds.
      */
-    public static ByteBuffer[] allocate(Pointer ptr, long sizes[], int off, int len)
+    public static ByteBuffer[] allocate(Pointer ptr, long sizes[])
         throws NullPointerException, IllegalArgumentException,
         IndexOutOfBoundsException
     {
-        return alloc6(ptr, sizes, off, len);
+        return alloc6(ptr, sizes, 0, sizes.length);
     }
 
     /**
-     * Allocate a new ByteBuffer array from memory contained by {@code ptr}.
+     * Returns the allocated memory size of the ByteBuffer.
+     * @param buf Previously allocated ByteBuffer.
+     */
+    public static native long size(ByteBuffer buf);
+
+    /**
+     * Set ByteBuffer to specified character.
+     * @param buf The ByteBuffer to use
+     * @param c The character to set
+     * @param count Number of characters
+     *
+     * @throws NullPointerException if {@code buf} is null.
+     * @throws IllegalArgumentException if {@code count} is less then {@code 1}.
+     * @throws IndexOutOfBoundsException if copying would cause access of
+     *         data outside ByteBuffer bounds.
+     */
+    public static native void set(ByteBuffer buf, int c, int count)
+        throws NullPointerException, IllegalArgumentException,
+        IndexOutOfBoundsException;
+
+    /**
+     * Copy an ByteBuffer content from specified source ByteBuffer.
+     * @param src The source ByteBuffer.
+     * @param dst The destination ByteBuffer.
+     * @param length The number of bytes to be copied.
+     *
+     * @throws NullPointerException if {@code src} or {@code dst} is null
+     * @throws IllegalArgumentException if {@code length} invalid
+     * @throws IndexOutOfBoundsException if copying would cause access of
+     *         data outside ByteBuffer bounds.
+     */
+    public static void copy(ByteBuffer src, ByteBuffer dst, int length)
+        throws NullPointerException, IllegalArgumentException,
+        IndexOutOfBoundsException
+    {
+        copy0(src, 0, dst, 0, length);
+    }
+
+    /**
+     * Copy an ByteBuffer content from specified source ByteBuffer.
+     * @param src The source ByteBuffer.
+     * @param srcPos Starting position in the source ByteBuffer.
+     * @param dst The destination ByteBuffer.
+     * @param dstPos Starting position in the source ByteBuffer.
+     * @param length The number of bytes to be copied.
+     *
+     * @throws NullPointerException if {@code src} or {@code dst} is null
+     * @throws IllegalArgumentException if {@code length} invalid
+     * @throws IndexOutOfBoundsException if copying would cause access of
+     *         data outside ByteBuffer bounds.
+     */
+    public static void copy(ByteBuffer src, long srcPos, ByteBuffer dst,
+                            long dstPos, int length)
+        throws NullPointerException, IllegalArgumentException,
+        IndexOutOfBoundsException
+    {
+        copy0(src, srcPos, dst, dstPos, length);
+    }
+
+    // Package private methods
+    // TODO: See if those methods should be present at the first
+    //       place because they can very easily crash the VM.
+
+    /**
+     * Allocate a new ByteBuffer from memory.
+     * @param size The amount of memory to allocate
+     * @return The ByteBuffer with allocated memory
+     *
+     * @throws OutOfMemoryError if memory cannot be allocated from the system.
+     * @throws IllegalArgumentException if {@code size} is invalid.
+     */
+    protected static ByteBuffer allocate(long size)
+        throws OutOfMemoryError, IllegalArgumentException
+    {
+        return alloc0(size);
+    }
+
+    /**
+     * Allocate a new ByteBuffer from memory and set all of the memory
+     * bytes to zero.
+     * @param size Length in bytes of each element.
+     * @return The ByteBuffer with allocated memory
+     *
+     * @throws OutOfMemoryError if memory cannot be allocated from the system.
+     * @throws IllegalArgumentException if {@code size} is invalid.
+     */
+    protected static ByteBuffer allocateAndClear(long size)
+        throws OutOfMemoryError, IllegalArgumentException
+    {
+        return alloc1(size);
+    }
+
+    /**
+     * Allocate a new ByteBuffer array from memory.
+     * <p>
+     * <b>Warning:</b><br/>
+     * Allocated array elements share the same memory segment
+     * so the {@code free()} must be called only for a
+     * <b>first</b> returned array element since it contains the
+     * start of the allocated memory pointer.
+     * </p>
      * <p>
      * Memory is internally aligned to 8 byte boundary
      * meaning that there might be unused chunks of memory if
      * the requested sizes are not aligned to a specified boundary.
      * </p>
      * <p>
-     * This method does the same as calling the:
-     * <pre>
-     *
-     * DirectByteBuffer.allocate(ptr, sizes, 0, sizes.length);
-     *
-     * </pre>
+     * For example if two ByteBuffers are allocated with sizes
+     * {@code 2} and {@code 3} the overall allocated
+     * memory will be {@code 16} bytes because each requested
+     * size is aligned to 8 byte boundary.
      * </p>
      *
-     * @param ptr {@link Pointer} that holds the native memory area.
      * @param sizes Array of lengths for each ByteBuffer.
+     * @param off Start offset of the {@code sizes} array.
+     * @param len The length of the {@code sizes} array to use.
      * @return The ByteBuffer array with allocated memory
      *
      * @throws OutOfMemoryError if memory cannot be allocated from the system.
@@ -205,11 +316,11 @@
      * @throws IndexOutOfBoundsException if copying would cause access of
      *         data outside array bounds.
      */
-    public static ByteBuffer[] allocate(Pointer ptr, long sizes[])
+    protected static ByteBuffer[] allocate(long sizes[], int off, int len)
         throws NullPointerException, IllegalArgumentException,
         IndexOutOfBoundsException
     {
-        return alloc6(ptr, sizes, 0, sizes.length);
+        return alloc2(sizes, off, len);
     }
 
     /**
@@ -236,7 +347,7 @@
      * @throws IllegalArgumentException if {@code off} or {@code len}
      *         is invalid.
      */
-    public static ByteBuffer[] allocate(long sizes[])
+    protected static ByteBuffer[] allocate(long sizes[])
         throws NullPointerException, IllegalArgumentException
     {
         return alloc2(sizes, 0, sizes.length);
@@ -252,22 +363,25 @@
      * <b>first</b> returned array element since it contains the
      * start of the allocated memory pointer.
      * </p>
+     * <p>
+     * This method does the same as calling the:
+     * <pre>
+     *
+     * DirectByteBuffer.allocateAndClear(sizes, 0, sizes.length);
+     *
+     * </pre>
+     * </p>
      * @param sizes Array of lengths for each ByteBuffer.
-     * @param off Start offset of the {@code sizes} array.
-     * @param len The length of the {@code sizes} array to use.
      * @return The ByteBuffer array with allocated memory
      *
      * @throws OutOfMemoryError if memory cannot be allocated from the system.
      * @throws IllegalArgumentException if {@code off} or {@code len}
      *         is invalid.
-     * @throws IndexOutOfBoundsException if copying would cause access of
-     *         data outside array bounds.
      */
-    public static ByteBuffer[] allocateAndClear(long sizes[], int off, int len)
-        throws NullPointerException, IllegalArgumentException,
-        IndexOutOfBoundsException
+    protected static ByteBuffer[] allocateAndClear(long sizes[])
+        throws NullPointerException, IllegalArgumentException
     {
-        return alloc3(sizes, off, len);
+        return alloc3(sizes, 0, sizes.length);
     }
 
     /**
@@ -280,25 +394,22 @@
      * <b>first</b> returned array element since it contains the
      * start of the allocated memory pointer.
      * </p>
-     * <p>
-     * This method does the same as calling the:
-     * <pre>
-     *
-     * DirectByteBuffer.allocateAndClear(sizes, 0, sizes.length);
-     *
-     * </pre>
-     * </p>
      * @param sizes Array of lengths for each ByteBuffer.
+     * @param off Start offset of the {@code sizes} array.
+     * @param len The length of the {@code sizes} array to use.
      * @return The ByteBuffer array with allocated memory
      *
      * @throws OutOfMemoryError if memory cannot be allocated from the system.
      * @throws IllegalArgumentException if {@code off} or {@code len}
      *         is invalid.
+     * @throws IndexOutOfBoundsException if copying would cause access of
+     *         data outside array bounds.
      */
-    public static ByteBuffer[] allocateAndClear(long sizes[])
-        throws NullPointerException, IllegalArgumentException
+    protected static ByteBuffer[] allocateAndClear(long sizes[], int off, int len)
+        throws NullPointerException, IllegalArgumentException,
+        IndexOutOfBoundsException
     {
-        return alloc3(sizes, 0, sizes.length);
+        return alloc3(sizes, off, len);
     }
 
     /**
@@ -322,60 +433,13 @@
      * @throws IllegalArgumentException if {@code offset} or
      *         {@code size} are invalid.
      */
-    public static ByteBuffer allocate(long mem, long offset, long size)
+    protected static ByteBuffer allocate(long mem, long offset, long size)
         throws NullPointerException, IllegalArgumentException
     {
         return attach(mem, offset, size);
     }
 
     /**
-     * Allocate a new {@code ByteBuffer} from {@link Pointer} memory area.
-     * <p>
-     * <b>Warning:</b><br/>
-     * Allocated array elements share the same memory segment
-     * so the {@code free()} must <b>not</b> be called on the retured
-     * {@code ByteBuffer}, neither it can be used after the {@code ptr}
-     * has been destroyed.
-     * </p>
-     * @param ptr Memory area to use.
-     * @return The {@code ByteBuffer} backed by the {@code ptr} memory area.
-     *
-     * @throws NullPointerException if {@code ptr} is null.
-     */
-    public static ByteBuffer allocate(Pointer ptr)
-        throws NullPointerException
-    {
-        return alloc5(ptr);
-    }
-
-    /**
-     * Allocate a new {@code ByteBuffer} from {@link Pointer} memory area.
-     * <p>
-     * <b>Warning:</b><br/>
-     * Allocated array elements share the same memory segment
-     * so the {@code free()} must <b>not</b> be called on the retured
-     * {@code ByteBuffer}, neither it can be used after the {@code ptr}
-     * has been destroyed.
-     * </p>
-     * @param ptr Memory area to use.
-     * @param off Start offset of the {@code ptr} memory area.
-     * @param len The length of the {@code ptr} memory areat to use.
-     * @return The {@code ByteBuffer} backed by the {@code ptr} memory area.
-     *
-     * @throws NullPointerException if {@code ptr} is null.
-     * @throws IllegalArgumentException if {@code off} or {@code len}
-     *         is invalid.
-     * @throws IndexOutOfBoundsException if copying would cause access of
-     *         data outside array bounds.
-     */
-    public static ByteBuffer allocate(Pointer ptr, long off, long len)
-        throws NullPointerException, IllegalArgumentException,
-        IndexOutOfBoundsException
-    {
-        return alloc4(ptr, off, len);
-    }
-
-    /**
      * Allocate a new ByteBuffer from already allocated ByteBuffer.
      * @param buf The ByteBuffer who's memory to use
      * @param offset Offset from the memory to use
@@ -386,7 +450,7 @@
      * @throws IllegalArgumentException if {@code offset} or
      *         {@code size} are invalid.
      */
-    public static ByteBuffer allocate(ByteBuffer buf, long offset, long size)
+    protected static ByteBuffer allocate(ByteBuffer buf, long offset, long size)
         throws NullPointerException, IllegalArgumentException
     {
         return attach(address(buf), offset, size);
@@ -411,73 +475,13 @@
      *
      * @throws NullPointerException if {@code buf} is null
      */
-    public static native void free(ByteBuffer buf)
+    protected static native void free(ByteBuffer buf)
         throws NullPointerException;
 
     /**
      * Returns the memory address of the ByteBuffer.
      * @param buf Previously allocated ByteBuffer.
      */
-    public static native long address(ByteBuffer buf);
-
-    /**
-     * Returns the allocated memory size of the ByteBuffer.
-     * @param buf Previously allocated ByteBuffer.
-     */
-    public static native long size(ByteBuffer buf);
-
-    /**
-     * Set ByteBuffer to specified character.
-     * @param buf The ByteBuffer to use
-     * @param c The character to set
-     * @param count Number of characters
-     *
-     * @throws NullPointerException if {@code buf} is null.
-     * @throws IllegalArgumentException if {@code count} is less then {@code 1}.
-     * @throws IndexOutOfBoundsException if copying would cause access of
-     *         data outside ByteBuffer bounds.
-     */
-    public static native void set(ByteBuffer buf, int c, int count)
-        throws NullPointerException, IllegalArgumentException,
-        IndexOutOfBoundsException;
-
-    /**
-     * Copy an ByteBuffer content from specified source ByteBuffer.
-     * @param src The source ByteBuffer.
-     * @param dst The destination ByteBuffer.
-     * @param length The number of bytes to be copied.
-     *
-     * @throws NullPointerException if {@code src} or {@code dst} is null
-     * @throws IllegalArgumentException if {@code length} invalid
-     * @throws IndexOutOfBoundsException if copying would cause access of
-     *         data outside ByteBuffer bounds.
-     */
-    public static void copy(ByteBuffer src, ByteBuffer dst, int length)
-        throws NullPointerException, IllegalArgumentException,
-        IndexOutOfBoundsException
-    {
-        copy0(src, 0, dst, 0, length);
-    }
-
-    /**
-     * Copy an ByteBuffer content from specified source ByteBuffer.
-     * @param src The source ByteBuffer.
-     * @param srcPos Starting position in the source ByteBuffer.
-     * @param dst The destination ByteBuffer.
-     * @param dstPos Starting position in the source ByteBuffer.
-     * @param length The number of bytes to be copied.
-     *
-     * @throws NullPointerException if {@code src} or {@code dst} is null
-     * @throws IllegalArgumentException if {@code length} invalid
-     * @throws IndexOutOfBoundsException if copying would cause access of
-     *         data outside ByteBuffer bounds.
-     */
-    public static void copy(ByteBuffer src, long srcPos, ByteBuffer dst,
-                            long dstPos, int length)
-        throws NullPointerException, IllegalArgumentException,
-        IndexOutOfBoundsException
-    {
-        copy0(src, srcPos, dst, dstPos, length);
-    }
+    protected static native long address(ByteBuffer buf);
 
 }