Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8318316

(bf) Add CharBuffer absolute bulk put method for CharSequence

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • tbd
    • core-libs
    • None
    • behavioral
    • minimal
    • This is a new method and `CharBuffer` is sealed so the risk should be nil.
    • Java API
    • SE

      Summary

      Add to java.nio.CharBuffer an absolute bulk put method

      CharBuffer put(int index, CharSequence csq, int start, int end) {}

      which allows writing a CharSequence or a subsequence thereof to a specified absolute position in a CharBuffer.

      Problem

      Absolute bulk put methods are already present in CharBuffer for char[] and CharBuffer sources, but there is no corresponding absolute bulk put method for CharSeqeuence sources.

      Solution

      Add the method

      CharBuffer put(int index, CharSequence csq, int start, int end) {}

      to java.nio.CharBuffer.

      Specification

      --- a/src/java.base/share/classes/java/nio/X-Buffer.java.template
      +++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template
      @@ -59,8 +59,13 @@ import jdk.internal.util.ArraysSupport;
        *
        *   <li><p> Absolute and relative {@link #put($type$[]) <i>bulk put</i>}
      
      • methods that transfer contiguous sequences of $type$s from $a$
      • * $type$ array{#if[char]
      ?, a string,} or some other $type$ - * buffer into this buffer;</p></li> + * $type$ array{#if[char]?, a string, a <code>CharSequence</code>,} + * or some other $type$ buffer into this buffer{#if[char]?,}{#if[!char]?;} +#if[char] + * with relative bulk put capability for {@linkplain CharSequence} sources + * provided in the form of the + * {@linkplain #append(CharSequence,int,int) <i>append</i>} methods; +#end[char] * #if[byte] * @@ -1464,6 +1469,97 @@ public abstract sealed class $Type$Buffer return put(src, 0, src.length()); } + /** + * Absolute bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>. + * + * <p> This method transfers {@code $type$}s from the given + * {@link CharSequence} into this buffer. If there are more {@code $type$}s + * to be copied from the {@code CharSequence} than remain in this buffer, + * that is, if + * <code>end&nbsp;-&nbsp;start&nbsp;>&nbsp; + * limit()&nbsp;-&nbsp;index</code>, + * then no {@code $type$}s are transferred and a + * {@link BufferOverflowException} is thrown. + * + * <p> Otherwise, this method copies + * <code>n&nbsp;=&nbsp;end&nbsp;-&nbsp;start</code> {@code $type$}s + * from the given {@code CharSequence} into this buffer, starting at the + * given {@code start} index and at the given {@code index} in this buffer. + * The position of this buffer is unchanged. + * + * <p> In other words, an invocation of this method of the form + * <code>dst.put(index,&nbsp;csq,&nbsp;start,&nbsp;end)</code> + * has exactly the same effect as the loop + * + * <pre>{@code + * for (int i = start, j = index; i < end; i++, j++) + * dst.put(j, csq.charAt(i)); + * }</pre> + * + * except that it first checks that there is sufficient space in this + * buffer and it is potentially much more efficient. + * + * @apiNote + * Unlike the other four-parameter absolute bulk put methods in this class, + * this method: + * <ul> + * <li>defines the range of characters to be read from the source in terms + * of the half-open interval <code>[start,&nbsp;end)</code> instead of + * an {@code offset} and a {@code length};</li> + * <li>throws a {@code BufferOverflowException} instead of an + * {@code IndexOutOfBoundsException} for the condition + * <code>index&nbsp;+&nbsp;end&nbsp;-&nbsp;start&nbsp;>&nbsp;limit()</code>. + * </ul> + * + * @param index + * The index in this buffer at which the first char will be written; + * must be non-negative and less than {@code limit()} + * + * @param csq + * The {@code CharSequence} from which {@code $type$}s + * are to be read + * + * @param start + * The offset within the {@code CharSequence} of the first $type$ + * to be read; must be non-negative and no larger than + * {@code csq.length()} + * + * @param end + * The offset within the {@code CharSequence} of the last $type$ + * to be read, plus one; must be non-negative and no larger than + * {@code csq.length()} + * + * @return This buffer + * + * @throws BufferOverflowException + * If there is insufficient space in this buffer + * + * @throws IndexOutOfBoundsException + * If the preconditions on the {@code index}, {@code start}, + * and {@code end} parameters do not hold + * + * @throws ReadOnlyBufferException + * If this buffer is read-only + * + * @see #append(CharSequence, int, int) + * @see #put(int, $type$[], int, int) + * @see #put(int, $Type$Buffer, int, int) + * @since 22 + */ + public $Type$Buffer put(int index, CharSequence csq, int start, int end) {}

            bpb Brian Burkhalter
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: