5071718: (bf) Add ByteBuffer.slice(int offset, int length)

Reviewed-by: alanb, bchristi, darcy, rriggs
This commit is contained in:
Brian Burkhalter 2019-02-28 12:05:59 -08:00
parent 8f84ae5684
commit fad1f059b0
17 changed files with 367 additions and 77 deletions

View file

@ -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 @@ 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> 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>
@ -599,6 +600,39 @@ public abstract class Buffer {
*/
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.
*

View file

@ -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 @@ class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private
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(),

View file

@ -218,14 +218,17 @@ class Direct$Type$Buffer$RW$$BO$
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,

View file

@ -27,6 +27,8 @@
package java.nio;
import java.util.Objects;
/**
#if[rw]
* A read/write Heap$Type$Buffer.
@ -38,8 +40,6 @@ package java.nio;
#end[rw]
*/
import java.util.Objects;
class Heap$Type$Buffer$RW$
extends {#if[ro]?Heap}$Type$Buffer
{
@ -112,19 +112,17 @@ class Heap$Type$Buffer$RW$
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,

View file

@ -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 @@ class StringCharBuffer // package-private
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,

View file

@ -546,6 +546,46 @@ public abstract class $Type$Buffer
@Override
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.
*
@ -1950,11 +1990,9 @@ public abstract class $Type$Buffer
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,