changeset 14441:db250cfb765a

8150785: (bf) Hoist slice and duplicate methods up to java.nio.Buffer Reviewed-by: alanb, rriggs, chegar
author prappo
date Mon, 16 May 2016 15:10:04 +0100
parents 4352ddb02f68
children 0c2e739fb309
files src/java.base/share/classes/java/nio/Buffer.java src/java.base/share/classes/java/nio/X-Buffer.java.template test/java/nio/Buffer/Order-X.java.template test/java/nio/Buffer/Order.java test/java/nio/Buffer/OrderChar.java test/java/nio/Buffer/OrderDouble.java test/java/nio/Buffer/OrderFloat.java test/java/nio/Buffer/OrderInt.java test/java/nio/Buffer/OrderLong.java test/java/nio/Buffer/OrderShort.java
diffstat 10 files changed, 65 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nio/Buffer.java	Mon May 16 14:47:27 2016 +0530
+++ b/src/java.base/share/classes/java/nio/Buffer.java	Mon May 16 15:10:04 2016 +0100
@@ -111,7 +111,7 @@
  * to zero.
  *
  *
- * <h2> Clearing, flipping, and rewinding </h2>
+ * <h2> Additional operations </h2>
  *
  * <p> In addition to methods for accessing the position, limit, and capacity
  * values and for marking and resetting, this class also defines the following
@@ -131,6 +131,12 @@
  *   it already contains: It leaves the limit unchanged and sets the position
  *   to zero.  </p></li>
  *
+ *   <li><p> {@link #slice} creates a subsequence of a buffer: It leaves the
+ *   limit and the position unchanged. </p></li>
+ *
+ *   <li><p> {@link #duplicate} creates a shallow copy of a buffer: It leaves
+ *   the limit and the position unchanged. </p></li>
+ *
  * </ul>
  *
  *
@@ -567,6 +573,46 @@
      */
     public abstract boolean isDirect();
 
+    /**
+     * Creates a new buffer whose content is a shared subsequence of
+     * this buffer's content.
+     *
+     * <p> The content of the new buffer will start at this buffer's current
+     * position.  Changes to this buffer's content will be visible in the new
+     * buffer, and vice versa; the two buffers' position, limit, and mark
+     * values will be independent.
+     *
+     * <p> The new buffer's position will be zero, its capacity and its limit
+     * will be the number of elements remaining in this buffer, its mark will be
+     * undefined. The new buffer will be direct if, and only if, this buffer is
+     * direct, and it will be read-only if, and only if, this buffer is
+     * read-only.  </p>
+     *
+     * @return  The new buffer
+     *
+     * @since 9
+     */
+    public abstract Buffer slice();
+
+    /**
+     * Creates a new buffer that shares this buffer's content.
+     *
+     * <p> The content of the new buffer will be that of this buffer.  Changes
+     * to this buffer's content will be visible in the new buffer, and vice
+     * versa; the two buffers' position, limit, and mark values will be
+     * independent.
+     *
+     * <p> The new buffer's capacity, limit, position and mark values will be
+     * identical to those of this buffer. The new buffer will be direct if, and
+     * only if, this buffer is direct, and it will be read-only if, and only if,
+     * this buffer is read-only.  </p>
+     *
+     * @return  The new buffer
+     *
+     * @since 9
+     */
+    public abstract Buffer duplicate();
+
 
     // -- Package-private methods for bounds checking, etc. --
 
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template	Mon May 16 14:47:27 2016 +0530
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template	Mon May 16 15:10:04 2016 +0100
@@ -70,8 +70,7 @@
  *
 #end[byte]
  *
- *   <li><p> Methods for {@link #compact compacting}, {@link
- *   #duplicate duplicating}, and {@link #slice slicing}
+ *   <li><p> A method for {@link #compact compacting}
  *   $a$ $type$ buffer.  </p></li>
  *
  * </ul>
@@ -535,6 +534,7 @@
      * @see #alignedSlice(int)
 #end[byte]
      */
+    @Override
     public abstract $Type$Buffer slice();
 
     /**
@@ -557,6 +557,7 @@
      *
      * @return  The new $type$ buffer
      */
+    @Override
     public abstract $Type$Buffer duplicate();
 
     /**
--- a/test/java/nio/Buffer/Order-X.java.template	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/Order-X.java.template	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = $Type$Buffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ck$Type$Buffer(buf, nord);
-
-        ck$Type$Buffer(ByteBuffer.allocate(LENGTH).as$Type$Buffer(), be);
-        ck$Type$Buffer(ByteBuffer.allocateDirect(LENGTH).as$Type$Buffer(), be);
     }
 }
--- a/test/java/nio/Buffer/Order.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/Order.java	Mon May 16 15:10:04 2016 +0100
@@ -51,25 +51,31 @@
         ck(bb.asDoubleBuffer().order(), bb.order());
     }
 
+    private static void ckCopyViews(ByteBuffer bb) {
+        ck(bb.asReadOnlyBuffer().order(), be);
+        ck(bb.duplicate().order(), be);
+        ck(bb.slice().order(), be);
+    }
+
     private static void ckByteBuffer(ByteBuffer bb) {
         ckViews(bb);
+        ckCopyViews(bb);
         bb.order(be);
         ckViews(bb);
+        ckCopyViews(bb);
         bb.order(le);
         ckViews(bb);
-
-        if (bb.hasArray()) {
-            byte[] array = bb.array();
-            ck(ByteBuffer.wrap(array, LENGTH/2, LENGTH/2).order(), be);
-            ck(ByteBuffer.wrap(array).order(), be);
-            ck(bb.asReadOnlyBuffer().order(), be);
-            ck(bb.duplicate().order(), be);
-            ck(bb.slice().order(), be);
-        }
+        ckCopyViews(bb);
     }
 
     public static void main(String args[]) throws Exception {
 
+        ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(), be);
+        ck(ByteBuffer.wrap(new byte[LENGTH]).order(), be);
+        ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(be).order(), be);
+        ck(ByteBuffer.wrap(new byte[LENGTH]).order(be).order(), be);
+        ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(le).order(), le);
+        ck(ByteBuffer.wrap(new byte[LENGTH]).order(le).order(), le);
         ck(ByteBuffer.allocate(LENGTH).order(), be);
         ck(ByteBuffer.allocateDirect(LENGTH).order(), be);
         ck(ByteBuffer.allocate(LENGTH).order(be).order(), be);
--- a/test/java/nio/Buffer/OrderChar.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/OrderChar.java	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = CharBuffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ckCharBuffer(buf, nord);
-
-        ckCharBuffer(ByteBuffer.allocate(LENGTH).asCharBuffer(), be);
-        ckCharBuffer(ByteBuffer.allocateDirect(LENGTH).asCharBuffer(), be);
     }
 }
--- a/test/java/nio/Buffer/OrderDouble.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/OrderDouble.java	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = DoubleBuffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ckDoubleBuffer(buf, nord);
-
-        ckDoubleBuffer(ByteBuffer.allocate(LENGTH).asDoubleBuffer(), be);
-        ckDoubleBuffer(ByteBuffer.allocateDirect(LENGTH).asDoubleBuffer(), be);
     }
 }
--- a/test/java/nio/Buffer/OrderFloat.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/OrderFloat.java	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = FloatBuffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ckFloatBuffer(buf, nord);
-
-        ckFloatBuffer(ByteBuffer.allocate(LENGTH).asFloatBuffer(), be);
-        ckFloatBuffer(ByteBuffer.allocateDirect(LENGTH).asFloatBuffer(), be);
     }
 }
--- a/test/java/nio/Buffer/OrderInt.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/OrderInt.java	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = IntBuffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ckIntBuffer(buf, nord);
-
-        ckIntBuffer(ByteBuffer.allocate(LENGTH).asIntBuffer(), be);
-        ckIntBuffer(ByteBuffer.allocateDirect(LENGTH).asIntBuffer(), be);
     }
 }
--- a/test/java/nio/Buffer/OrderLong.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/OrderLong.java	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = LongBuffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ckLongBuffer(buf, nord);
-
-        ckLongBuffer(ByteBuffer.allocate(LENGTH).asLongBuffer(), be);
-        ckLongBuffer(ByteBuffer.allocateDirect(LENGTH).asLongBuffer(), be);
     }
 }
--- a/test/java/nio/Buffer/OrderShort.java	Mon May 16 14:47:27 2016 +0530
+++ b/test/java/nio/Buffer/OrderShort.java	Mon May 16 15:10:04 2016 +0100
@@ -52,8 +52,5 @@
         buf = ShortBuffer.allocate(LENGTH);
         ck(buf.order(), nord);
         ckShortBuffer(buf, nord);
-
-        ckShortBuffer(ByteBuffer.allocate(LENGTH).asShortBuffer(), be);
-        ckShortBuffer(ByteBuffer.allocateDirect(LENGTH).asShortBuffer(), be);
     }
 }