8150785: (bf) Hoist slice and duplicate methods up to java.nio.Buffer

Reviewed-by: alanb, rriggs, chegar
This commit is contained in:
Pavel Rappo 2016-05-16 15:10:04 +01:00
parent 905d21fe76
commit baf59c0bd8
10 changed files with 65 additions and 33 deletions

View File

@ -111,7 +111,7 @@ import java.util.Spliterator;
* 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 @@ import java.util.Spliterator;
* 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 class Buffer {
*/
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. --

View File

@ -70,8 +70,7 @@ import java.util.stream.$Streamtype$Stream;
*
#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 @@ public abstract class $Type$Buffer
* @see #alignedSlice(int)
#end[byte]
*/
@Override
public abstract $Type$Buffer slice();
/**
@ -557,6 +557,7 @@ public abstract class $Type$Buffer
*
* @return The new $type$ buffer
*/
@Override
public abstract $Type$Buffer duplicate();
/**

View File

@ -52,8 +52,5 @@ public class Order$Type$ extends Order {
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);
}
}

View File

@ -51,25 +51,31 @@ public class Order {
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);

View File

@ -52,8 +52,5 @@ public class OrderChar extends Order {
buf = CharBuffer.allocate(LENGTH);
ck(buf.order(), nord);
ckCharBuffer(buf, nord);
ckCharBuffer(ByteBuffer.allocate(LENGTH).asCharBuffer(), be);
ckCharBuffer(ByteBuffer.allocateDirect(LENGTH).asCharBuffer(), be);
}
}

View File

@ -52,8 +52,5 @@ public class OrderDouble extends Order {
buf = DoubleBuffer.allocate(LENGTH);
ck(buf.order(), nord);
ckDoubleBuffer(buf, nord);
ckDoubleBuffer(ByteBuffer.allocate(LENGTH).asDoubleBuffer(), be);
ckDoubleBuffer(ByteBuffer.allocateDirect(LENGTH).asDoubleBuffer(), be);
}
}

View File

@ -52,8 +52,5 @@ public class OrderFloat extends Order {
buf = FloatBuffer.allocate(LENGTH);
ck(buf.order(), nord);
ckFloatBuffer(buf, nord);
ckFloatBuffer(ByteBuffer.allocate(LENGTH).asFloatBuffer(), be);
ckFloatBuffer(ByteBuffer.allocateDirect(LENGTH).asFloatBuffer(), be);
}
}

View File

@ -52,8 +52,5 @@ public class OrderInt extends Order {
buf = IntBuffer.allocate(LENGTH);
ck(buf.order(), nord);
ckIntBuffer(buf, nord);
ckIntBuffer(ByteBuffer.allocate(LENGTH).asIntBuffer(), be);
ckIntBuffer(ByteBuffer.allocateDirect(LENGTH).asIntBuffer(), be);
}
}

View File

@ -52,8 +52,5 @@ public class OrderLong extends Order {
buf = LongBuffer.allocate(LENGTH);
ck(buf.order(), nord);
ckLongBuffer(buf, nord);
ckLongBuffer(ByteBuffer.allocate(LENGTH).asLongBuffer(), be);
ckLongBuffer(ByteBuffer.allocateDirect(LENGTH).asLongBuffer(), be);
}
}

View File

@ -52,8 +52,5 @@ public class OrderShort extends Order {
buf = ShortBuffer.allocate(LENGTH);
ck(buf.order(), nord);
ckShortBuffer(buf, nord);
ckShortBuffer(ByteBuffer.allocate(LENGTH).asShortBuffer(), be);
ckShortBuffer(ByteBuffer.allocateDirect(LENGTH).asShortBuffer(), be);
}
}