--- old/src/java.base/share/classes/java/nio/Buffer.java 2019-02-22 15:08:15.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/Buffer.java 2019-02-22 15:08:15.000000000 -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 @@ -600,6 +600,39 @@ public abstract Buffer slice(); /** + * Creates a new buffer whose content is a shared subsequence of + * this buffer's content. + * + *

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. + * + *

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.

+ * + * @param index + * The position in this buffer at which the content of the new + * buffer will start; must be non-negative and less 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 not less 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. * *

The content of the new buffer will be that of this buffer. Changes --- old/src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template 2019-02-22 15:08:16.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/ByteBufferAs-X-Buffer.java.template 2019-02-22 15:08:15.000000000 -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,17 @@ return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, addr); } + @Override + public $Type$Buffer slice(int index, int length) { + Objects.checkFromIndexSize(index, length, limit()); + 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(), --- old/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template 2019-02-22 15:08:16.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template 2019-02-22 15:08:16.000000000 -0800 @@ -218,14 +218,16 @@ 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.checkFromIndexSize(index, length, limit()); + 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, --- old/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2019-02-22 15:08:17.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2019-02-22 15:08:16.000000000 -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,16 @@ 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.checkFromIndexSize(index, length, limit()); 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, --- old/src/java.base/share/classes/java/nio/StringCharBuffer.java 2019-02-22 15:08:17.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/StringCharBuffer.java 2019-02-22 15:08:17.000000000 -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,17 @@ offset + this.position()); } + @Override + public CharBuffer slice(int index, int length) { + Objects.checkFromIndexSize(index, length, limit()); + return new StringCharBuffer(str, + -1, + 0, + length, + length, + offset + index); + } + private StringCharBuffer(CharSequence s, int mark, int pos, --- old/src/java.base/share/classes/java/nio/X-Buffer.java.template 2019-02-22 15:08:18.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/X-Buffer.java.template 2019-02-22 15:08:17.000000000 -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. + * + *

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. + * + *

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.

+ * + * @param index + * The position in this buffer at which the content of the new + * buffer will start; must be non-negative and less 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 not less 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. * *

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, --- old/test/jdk/java/nio/Buffer/Basic-X.java.template 2019-02-22 15:08:18.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/Basic-X.java.template 2019-02-22 15:08:18.000000000 -0800 @@ -832,6 +832,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 --- old/test/jdk/java/nio/Buffer/Basic.java 2019-02-22 15:08:19.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/Basic.java 2019-02-22 15:08:19.000000000 -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 --- old/test/jdk/java/nio/Buffer/BasicByte.java 2019-02-22 15:08:19.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicByte.java 2019-02-22 15:08:19.000000000 -0800 @@ -832,6 +832,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 --- old/test/jdk/java/nio/Buffer/BasicChar.java 2019-02-22 15:08:20.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicChar.java 2019-02-22 15:08:20.000000000 -0800 @@ -832,6 +832,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); + --- old/test/jdk/java/nio/Buffer/BasicDouble.java 2019-02-22 15:08:20.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicDouble.java 2019-02-22 15:08:20.000000000 -0800 @@ -832,6 +832,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); + --- old/test/jdk/java/nio/Buffer/BasicFloat.java 2019-02-22 15:08:21.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicFloat.java 2019-02-22 15:08:21.000000000 -0800 @@ -832,6 +832,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); + --- old/test/jdk/java/nio/Buffer/BasicInt.java 2019-02-22 15:08:22.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicInt.java 2019-02-22 15:08:21.000000000 -0800 @@ -832,6 +832,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); + --- old/test/jdk/java/nio/Buffer/BasicLong.java 2019-02-22 15:08:22.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicLong.java 2019-02-22 15:08:22.000000000 -0800 @@ -832,6 +832,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); + --- old/test/jdk/java/nio/Buffer/BasicShort.java 2019-02-22 15:08:23.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/BasicShort.java 2019-02-22 15:08:22.000000000 -0800 @@ -832,6 +832,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); + --- old/test/jdk/java/nio/Buffer/ByteBufferViews.java 2019-02-22 15:08:23.000000000 -0800 +++ new/test/jdk/java/nio/Buffer/ByteBufferViews.java 2019-02-22 15:08:23.000000000 -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