changeset 53959:1542e63eb537

5071718: (bf) Add ByteBuffer.slice(int offset, int length) Reviewed-by: alanb, bchristi, darcy, rriggs
author bpb
date Thu, 28 Feb 2019 12:05:59 -0800
parents 196ab0abc685
children 6c3fd94de35a
files src/java.base/share/classes/java/nio/Buffer.java src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template src/java.base/share/classes/java/nio/StringCharBuffer.java src/java.base/share/classes/java/nio/X-Buffer.java.template test/jdk/java/nio/Buffer/Basic-X.java.template test/jdk/java/nio/Buffer/Basic.java test/jdk/java/nio/Buffer/BasicByte.java test/jdk/java/nio/Buffer/BasicChar.java test/jdk/java/nio/Buffer/BasicDouble.java test/jdk/java/nio/Buffer/BasicFloat.java test/jdk/java/nio/Buffer/BasicInt.java test/jdk/java/nio/Buffer/BasicLong.java test/jdk/java/nio/Buffer/BasicShort.java test/jdk/java/nio/Buffer/ByteBufferViews.java test/jdk/java/nio/Buffer/StringCharBufferSliceTest.java
diffstat 17 files changed, 367 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/nio/Buffer.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/src/java.base/share/classes/java/nio/Buffer.java	Thu Feb 28 12:05:59 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -134,8 +134,9 @@
  *   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> The {@link #slice} and {@link #slice(int,int) slice(index,length)}
+ *   methods create a subsequence of a buffer: They leave 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>
@@ -600,6 +601,39 @@
     public abstract Buffer slice();
 
     /**
+     * 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 position {@code index}
+     * in this buffer, and will contain {@code length} elements. 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 {@code length}, 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>
+     *
+     * @param   index
+     *          The position in this buffer at which the content of the new
+     *          buffer will start; must be non-negative and no larger than
+     *          {@link #limit() limit()}
+     *
+     * @param   length
+     *          The number of elements the new buffer will contain; must be
+     *          non-negative and no larger than {@code limit() - index}
+     *
+     * @return  The new buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If {@code index} is negative or greater than {@code limit()},
+     *          {@code length} is negative, or {@code length > limit() - index}
+     *
+     * @since 13
+     */
+    public abstract Buffer slice(int index, int length);
+
+    /**
      * Creates a new buffer that shares this buffer's content.
      *
      * <p> The content of the new buffer will be that of this buffer.  Changes
--- a/src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template	Thu Feb 28 10:04:27 2019 -0800
+++ b/src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template	Thu Feb 28 12:05:59 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,9 +27,9 @@
 
 package java.nio;
 
+import java.util.Objects;
 import jdk.internal.misc.Unsafe;
 
-
 class ByteBufferAs$Type$Buffer$RW$$BO$                  // package-private
     extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$}
 {
@@ -85,6 +85,18 @@
         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, addr);
     }
 
+    @Override
+    public $Type$Buffer slice(int index, int length) {
+        Objects.checkIndex(index, limit() + 1);
+        Objects.checkIndex(length, limit() - index + 1);
+        return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
+                                                    -1,
+                                                    0,
+                                                    length,
+                                                    length,
+                                                    byteOffset(index));
+    }
+
     public $Type$Buffer duplicate() {
         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
                                                     this.markValue(),
--- a/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template	Thu Feb 28 10:04:27 2019 -0800
+++ b/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template	Thu Feb 28 12:05:59 2019 -0800
@@ -218,14 +218,17 @@
         return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off);
     }
 
-#if[byte]
-    public $Type$Buffer slice(int pos, int lim) {
-        assert (pos >= 0);
-        assert (pos <= lim);
-        int rem = lim - pos;
-        return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, pos);
+    @Override
+    public $Type$Buffer slice(int index, int length) {
+        Objects.checkIndex(index, limit() + 1);
+        Objects.checkIndex(length, limit() - index + 1);
+        return new Direct$Type$Buffer$RW$$BO$(this,
+                                              -1,
+                                              0,
+                                              length,
+                                              length,
+                                              index);
     }
-#end[byte]
 
     public $Type$Buffer duplicate() {
         return new Direct$Type$Buffer$RW$$BO$(this,
--- a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template	Thu Feb 28 10:04:27 2019 -0800
+++ b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template	Thu Feb 28 12:05:59 2019 -0800
@@ -27,6 +27,8 @@
 
 package java.nio;
 
+import java.util.Objects;
+
 /**
 #if[rw]
  * A read/write Heap$Type$Buffer.
@@ -38,8 +40,6 @@
 #end[rw]
  */
 
-import java.util.Objects;
-
 class Heap$Type$Buffer$RW$
     extends {#if[ro]?Heap}$Type$Buffer
 {
@@ -112,19 +112,17 @@
                                         this.position() + offset);
     }
 
-#if[byte]
-    $Type$Buffer slice(int pos, int lim) {
-        assert (pos >= 0);
-        assert (pos <= lim);
-        int rem = lim - pos;
+    @Override
+    public $Type$Buffer slice(int index, int length) {
+        Objects.checkIndex(index, limit() + 1);
+        Objects.checkIndex(length, limit() - index + 1);
         return new Heap$Type$Buffer$RW$(hb,
                                         -1,
                                         0,
-                                        rem,
-                                        rem,
-                                        pos + offset);
+                                        length,
+                                        length,
+                                        index + offset);
     }
-#end[byte]
 
     public $Type$Buffer duplicate() {
         return new Heap$Type$Buffer$RW$(hb,
--- a/src/java.base/share/classes/java/nio/StringCharBuffer.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/src/java.base/share/classes/java/nio/StringCharBuffer.java	Thu Feb 28 12:05:59 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
 
 package java.nio;
 
+import java.util.Objects;
 
 // ## If the sequence is a string, use reflection to share its array
 
@@ -51,6 +52,18 @@
                                     offset + this.position());
     }
 
+    @Override
+    public CharBuffer slice(int index, int length) {
+        Objects.checkIndex(index, limit() + 1);
+        Objects.checkIndex(length, limit() - index + 1);
+        return new StringCharBuffer(str,
+                                    -1,
+                                    0,
+                                    length,
+                                    length,
+                                    offset + index);
+    }
+
     private StringCharBuffer(CharSequence s,
                              int mark,
                              int pos,
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template	Thu Feb 28 10:04:27 2019 -0800
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template	Thu Feb 28 12:05:59 2019 -0800
@@ -547,6 +547,46 @@
     public abstract $Type$Buffer slice();
 
     /**
+     * Creates a new $type$ buffer whose content is a shared subsequence of
+     * this buffer's content.
+     *
+     * <p> The content of the new buffer will start at position {@code index}
+     * in this buffer, and will contain {@code length} elements. 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 {@code length}, its mark will be undefined, and its byte order
+     * will be
+#if[byte]
+     * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
+#else[byte]
+     * identical to that of this buffer.
+#end[byte]
+     * 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>
+     *
+     * @param   index
+     *          The position in this buffer at which the content of the new
+     *          buffer will start; must be non-negative and no larger than
+     *          {@link #limit() limit()}
+     *
+     * @param   length
+     *          The number of elements the new buffer will contain; must be
+     *          non-negative and no larger than {@code limit() - index}
+     *
+     * @return  The new buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If {@code index} is negative or greater than {@code limit()},
+     *          {@code length} is negative, or {@code length > limit() - index}
+     *
+     * @since 13
+     */
+    @Override
+    public abstract $Type$Buffer slice(int index, int length);
+
+    /**
      * Creates a new $type$ buffer that shares this buffer's content.
      *
      * <p> The content of the new buffer will be that of this buffer.  Changes
@@ -1950,11 +1990,9 @@
             aligned_pos = aligned_lim = pos;
         }
 
-        return slice(aligned_pos, aligned_lim);
+        return slice(aligned_pos, aligned_lim - aligned_pos);
     }
 
-    abstract ByteBuffer slice(int pos, int lim);
-
     // #BIN
     //
     // Binary-data access methods  for short, char, int, long, float,
--- a/test/jdk/java/nio/Buffer/Basic-X.java.template	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/Basic-X.java.template	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, ($type$[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        $Type$Buffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        $Type$Buffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 #if[byte]
 
         // Views
--- a/test/jdk/java/nio/Buffer/Basic.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/Basic.java	Thu Feb 28 12:05:59 2019 -0800
@@ -25,8 +25,8 @@
  * @summary Unit test for buffers
  * @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725
  *      4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 5029431
- *      6231529 6221101 6234263 6535542 6591971 6593946 6795561 7190219 7199551
- *      8065556 8149469
+ *      5071718 6231529 6221101 6234263 6535542 6591971 6593946 6795561 7190219
+ *      7199551 8065556 8149469
  * @modules java.base/java.nio:open
  *          java.base/jdk.internal.misc
  * @author Mark Reinhold
--- a/test/jdk/java/nio/Buffer/BasicByte.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicByte.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (byte[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        ByteBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        ByteBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
         // Views
--- a/test/jdk/java/nio/Buffer/BasicChar.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicChar.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (char[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        CharBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        CharBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
 
--- a/test/jdk/java/nio/Buffer/BasicDouble.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicDouble.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (double[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        DoubleBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        DoubleBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
 
--- a/test/jdk/java/nio/Buffer/BasicFloat.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicFloat.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (float[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        FloatBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        FloatBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
 
--- a/test/jdk/java/nio/Buffer/BasicInt.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicInt.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (int[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        IntBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        IntBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
 
--- a/test/jdk/java/nio/Buffer/BasicLong.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicLong.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (long[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        LongBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        LongBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
 
--- a/test/jdk/java/nio/Buffer/BasicShort.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/BasicShort.java	Thu Feb 28 12:05:59 2019 -0800
@@ -648,7 +648,7 @@
             }
         }
 
-        // Exceptions in absolute bulk operations
+        // Exceptions in absolute bulk and slice operations
 
         catchNullArgument(b, () -> b.get(7, null, 0, 42));
         catchNullArgument(b, () -> b.put(7, (short[])null, 0, 42));
@@ -668,6 +668,11 @@
         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 
+        catchIndexOutOfBounds(b, () -> b.slice(-1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));
+        catchIndexOutOfBounds(b, () -> b.slice(0, -1));
+        catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));
+
         // Values
 
         b.clear();
@@ -832,6 +837,20 @@
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
         }
 
+        int bPos = b.position();
+        int bLim = b.limit();
+
+        b.position(7);
+        b.limit(42);
+        ShortBuffer rsb = b.slice();
+        b.position(0);
+        b.limit(b.capacity());
+        ShortBuffer asb = b.slice(7, 35);
+        checkSlice(rsb, asb);
+
+        b.position(bPos);
+        b.limit(bLim);
+
 
 
 
--- a/test/jdk/java/nio/Buffer/ByteBufferViews.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/ByteBufferViews.java	Thu Feb 28 12:05:59 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -64,6 +64,8 @@
                       size -> ByteBuffer.allocate(size).position(8).slice()),
             Map.entry("ByteBuffer.allocate(size).position(8).slice().duplicate()",
                       size -> ByteBuffer.allocate(size).position(8).slice().duplicate()),
+            Map.entry("ByteBuffer.allocate(size).slice(8,size-8)",
+                      size -> ByteBuffer.allocate(size).slice(8,size-8)),
             // Unaligned
             Map.entry("ByteBuffer.allocate(size).position(1)",
                       size -> ByteBuffer.allocate(size).position(1)),
@@ -71,6 +73,8 @@
                       size -> ByteBuffer.allocate(size).position(1).slice()),
             Map.entry("ByteBuffer.allocate(size).position(1).slice().duplicate()",
                       size -> ByteBuffer.allocate(size).position(1).slice().duplicate()),
+            Map.entry("ByteBuffer.allocate(size).slice(1,size-1)",
+                      size -> ByteBuffer.allocate(size).slice(1,size-1)),
 
             // Off-heap
             Map.entry("ByteBuffer.allocateDirect(size)",
@@ -82,13 +86,17 @@
                       size -> ByteBuffer.allocateDirect(size).position(8).slice()),
             Map.entry("ByteBuffer.allocateDirect(size).position(8).slice().duplicate()",
                       size -> ByteBuffer.allocateDirect(size).position(8).slice().duplicate()),
+            Map.entry("ByteBuffer.allocateDirect(size).slice(8,size-8)",
+                      size -> ByteBuffer.allocateDirect(size).slice(8,size-8)),
             // Unaligned
             Map.entry("ByteBuffer.allocateDirect(size).position(1)",
                       size -> ByteBuffer.allocateDirect(size).position(1)),
             Map.entry("ByteBuffer.allocateDirect(size).position(1).slice()",
                       size -> ByteBuffer.allocateDirect(size).position(1).slice()),
             Map.entry("ByteBuffer.allocateDirect(size).position(1).slice().duplicate()",
-                      size -> ByteBuffer.allocateDirect(size).position(1).slice().duplicate())
+                      size -> ByteBuffer.allocateDirect(size).position(1).slice().duplicate()),
+            Map.entry("ByteBuffer.allocateDirect(size).slice(1,size-1)",
+                      size -> ByteBuffer.allocateDirect(size).slice(1,size-1))
     );
 
     // List of buffer byte order functions
--- a/test/jdk/java/nio/Buffer/StringCharBufferSliceTest.java	Thu Feb 28 10:04:27 2019 -0800
+++ b/test/jdk/java/nio/Buffer/StringCharBufferSliceTest.java	Thu Feb 28 12:05:59 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,11 +22,14 @@
  */
 
 /* @test
- * @bug 4997655 7000913
+ * @bug 4997655 5071718 7000913
  * @summary (bf) CharBuffer.slice() on wrapped CharSequence results in wrong position
  */
 
-import java.nio.*;
+import java.nio.CharBuffer;
+import java.nio.InvalidMarkException;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
 
 public class StringCharBufferSliceTest {
     public static void main( String[] args) throws Exception {
@@ -40,85 +43,114 @@
 
         CharBuffer buff = CharBuffer.wrap(in);
         test(buff, buff.slice());
+        test(buff, buff.slice(0, buff.remaining()));
 
         System.out.println(
             ">>> StringCharBufferSliceTest-main: testing with new position.");
 
         buff.position(2);
         test(buff, buff.slice());
+        test(buff, buff.slice(2, buff.remaining()));
 
         System.out.println(
           ">>> StringCharBufferSliceTest-main: testing with non zero initial position.");
 
         buff = CharBuffer.wrap(in, 3, in.length());
         test(buff, buff.slice());
+        test(buff, buff.slice(0, buff.remaining()));
 
         System.out.println(
             ">>> StringCharBufferSliceTest-main: testing slice result with get()");
         buff.position(4);
         buff.limit(7);
-        CharBuffer slice = buff.slice();
-        for (int i = 0; i < 3; i++) {
-            if (slice.get() != buff.get()) {
-                throw new RuntimeException("Wrong characters in slice result.");
+        BiConsumer<CharBuffer,CharBuffer> bitest = (b, s) -> {
+            for (int i = 0; i < 3; i++) {
+                if (s.get() != b.get()) {
+                    throw new RuntimeException
+                        ("Wrong characters in slice result.");
+                }
             }
-        }
+        };
+        bitest.accept(buff, buff.slice());
+        buff.position(4);
+        bitest.accept(buff, buff.slice(4, 3));
 
         System.out.println(
             ">>> StringCharBufferSliceTest-main: testing slice result with get(int)");
         buff.position(4);
         buff.limit(7);
-        slice = buff.slice();
-        for (int i = 0; i < 3; i++) {
-            if (slice.get(i) != buff.get(4 + i)) {
-                throw new RuntimeException("Wrong characters in slice result.");
+        bitest = (b, s) -> {
+            for (int i = 0; i < 3; i++) {
+                if (s.get(i) != b.get(4 + i)) {
+                    throw new RuntimeException
+                        ("Wrong characters in slice result.");
+                }
             }
-        }
+        };
+        bitest.accept(buff, buff.slice());
+        buff.position(4);
+        bitest.accept(buff, buff.slice(4, 3));
 
         System.out.println(
           ">>> StringCharBufferSliceTest-main: testing slice with result of slice");
         buff.position(0);
         buff.limit(buff.capacity());
-        slice = buff.slice();
-        for (int i=0; i<4; i++) {
-            slice.position(i);
-            CharBuffer nextSlice = slice.slice();
-            if (nextSlice.position() != 0)
-                throw new RuntimeException("New buffer's position should be zero");
-            if (!nextSlice.equals(slice))
-                throw new RuntimeException("New buffer should be equal");
-            slice = nextSlice;
-        }
+        Consumer<CharBuffer> test = (s) -> {
+            for (int i=0; i<4; i++) {
+                s.position(i);
+                CharBuffer nextSlice = s.slice();
+                if (nextSlice.position() != 0)
+                    throw new RuntimeException
+                        ("New buffer's position should be zero");
+                if (!nextSlice.equals(s))
+                    throw new RuntimeException("New buffer should be equal");
+                s = nextSlice;
+            }
+        };
+        test.accept(buff.slice());
+        test.accept(buff.slice(0, buff.capacity()));
 
         System.out.println(
           ">>> StringCharBufferSliceTest-main: testing toString.");
         buff.position(4);
         buff.limit(7);
-        slice = buff.slice();
-        if (!slice.toString().equals("tes")) {
-            throw new RuntimeException("bad toString() after slice(): " + slice.toString());
-        }
+        test = (s) -> {
+            if (!s.toString().equals("tes")) {
+                throw new RuntimeException
+                    ("bad toString() after slice(): " + s.toString());
+            }
+        };
+        test.accept(buff.slice());
+        test.accept(buff.slice(4, 3));
 
         System.out.println(
           ">>> StringCharBufferSliceTest-main: testing subSequence.");
         buff.position(4);
         buff.limit(8);
-        slice = buff.slice();
-        CharSequence subSeq = slice.subSequence(1, 3);
-        if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
-            throw new RuntimeException("bad subSequence() after slice(): '" + subSeq + "'");
-        }
+        test = (s) -> {
+            CharSequence subSeq = s.subSequence(1, 3);
+            if (subSeq.charAt(0) != 'e' || subSeq.charAt(1) != 's') {
+                throw new RuntimeException
+                    ("bad subSequence() after slice(): '" + subSeq + "'");
+            }
+        };
+        test.accept(buff.slice());
+        test.accept(buff.slice(4, 4));
 
         System.out.println(
           ">>> StringCharBufferSliceTest-main: testing duplicate.");
         buff.position(4);
         buff.limit(8);
-        slice = buff.slice();
-        CharBuffer dupe = slice.duplicate();
-        if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
-            || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
-            throw new RuntimeException("bad duplicate() after slice(): '" + dupe + "'");
-        }
+        test = (s) -> {
+            CharBuffer dupe = s.duplicate();
+            if (dupe.charAt(0) != 't' || dupe.charAt(1) != 'e'
+                || dupe.charAt(2) != 's' || dupe.charAt(3) != 't') {
+                throw new RuntimeException
+                    ("bad duplicate() after slice(): '" + dupe + "'");
+            }
+        };
+        test.accept(buff.slice());
+        test.accept(buff.slice(4, 4));
 
         System.out.println(">>> StringCharBufferSliceTest-main: done!");
     }