< prev index next >

src/java.base/share/classes/java/nio/X-Buffer.java.template

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -34,10 +34,11 @@
 import java.util.Spliterator;
 import java.util.stream.StreamSupport;
 import java.util.stream.$Streamtype$Stream;
 #end[streamableType]
 
+import java.util.Objects;
 import jdk.internal.util.ArraysSupport;
 
 /**
  * $A$ $type$ buffer.
  *

@@ -48,15 +49,15 @@
  *
  *   <li><p> Absolute and relative {@link #get() <i>get</i>} and
  *   {@link #put($type$) <i>put</i>} methods that read and write
  *   single $type$s; </p></li>
  *
- *   <li><p> Relative {@link #get($type$[]) <i>bulk get</i>}
+ *   <li><p> Absolute and relative {@link #get($type$[]) <i>bulk get</i>}
  *   methods that transfer contiguous sequences of $type$s from this buffer
  *   into an array; {#if[!byte]?and}</p></li>
  *
- *   <li><p> Relative {@link #put($type$[]) <i>bulk put</i>}
+ *   <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;{#if[!byte]? and} </p></li>
  *
 #if[byte]

@@ -432,11 +433,10 @@
      *
      * @param target the buffer to read characters into
      * @return The number of characters added to the buffer, or
      *         -1 if this source of characters is at its end
      * @throws IOException if an I/O error occurs
-     * @throws NullPointerException if target is null
      * @throws ReadOnlyBufferException if target is a read only buffer
      * @since 1.5
      */
     public int read(CharBuffer target) throws IOException {
         // Determine the number of bytes n that can be transferred

@@ -760,10 +760,95 @@
      */
     public $Type$Buffer get($type$[] dst) {
         return get(dst, 0, dst.length);
     }
 
+    /**
+     * Absolute bulk <i>get</i> method.
+     *
+     * <p> This method transfers {@code length} $type$s from this
+     * buffer into the given array, starting at the given index in this
+     * buffer and at the given offset in the array.  The position of this
+     * buffer is unchanged.
+     *
+     * <p> An invocation of this method of the form
+     * <code>src.get(index,&nbsp;dst,&nbsp;offset,&nbsp;length)</code>
+     * has exactly the same effect as the following loop except that it first
+     * checks the consistency of the supplied parameters and it is potentially
+     * much more efficient:
+     *
+     * <pre>{@code
+     *     for (int i = offset, j = index; i < offset + length; i++, j++)
+     *         dst[i] = src.get(j);
+     * }</pre>
+     *
+     * @param  index
+     *         The index in this buffer from which the first $type$ will be
+     *         read; must be non-negative and less than {@code limit()}
+     *
+     * @param  dst
+     *         The destination array
+     *
+     * @param  offset
+     *         The offset within the array of the first $type$ to be
+     *         written; must be non-negative and less than
+     *         {@code dst.length}
+     *
+     * @param  length
+     *         The number of $type$s to be written to the given array;
+     *         must be non-negative and no larger than the smaller of
+     *         {@code limit() - index} and {@code dst.length - offset}
+     *
+     * @return  This buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If the preconditions on the {@code index}, {@code offset}, and
+     *          {@code length} parameters do not hold
+     *
+     * @since 13
+     */
+    public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
+        //System.out.println("Absolute bulk get");
+        Objects.checkFromIndexSize(index, length, limit());
+        Objects.checkFromIndexSize(offset, length, dst.length);
+        int end = offset + length;
+        for (int i = offset, j = index; i < end; i++, j++)
+            dst[i] = get(j);
+        return this;
+    }
+
+    /**
+     * Absolute bulk <i>get</i> method.
+     *
+     * <p> This method transfers $type$s from this buffer into the given
+     * destination array.  The position of this buffer is unchanged.  An
+     * invocation of this method of the form
+     * <code>src.get(index,&nbsp;dst)</code> behaves in exactly the same
+     * way as the invocation:
+     *
+     * <pre>
+     *     src.get(index, dst, 0, dst.length) </pre>
+     *
+     * @param  index
+     *         The index in this buffer from which the first $type$ will be
+     *         read; must be non-negative and less than {@code limit()}
+     *
+     * @param  dst
+     *         The destination array
+     *
+     * @return  This buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If {@code index} is negative, not smaller than {@code limit()},
+     *          or {@code limit() - index < dst.length}
+     *
+     * @since 13
+     */
+    public $Type$Buffer get(int index, $type$[] dst) {
+        return get(index, dst, 0, dst.length);
+    }
+
 
     // -- Bulk put operations --
 
     /**
      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.

@@ -838,27 +923,27 @@
      * <code>dst.put(src,&nbsp;off,&nbsp;len)</code> has exactly the same effect as
      * the loop
      *
      * <pre>{@code
      *     for (int i = off; i < off + len; i++)
-     *         dst.put(a[i]);
+     *         dst.put(src[i]);
      * }</pre>
      *
      * except that it first checks that there is sufficient space in this
      * buffer and it is potentially much more efficient.
      *
      * @param  src
      *         The array from which $type$s are to be read
      *
      * @param  offset
      *         The offset within the array of the first $type$ to be read;
-     *         must be non-negative and no larger than {@code array.length}
+     *         must be non-negative and no larger than {@code src.length}
      *
      * @param  length
      *         The number of $type$s to be read from the given array;
      *         must be non-negative and no larger than
-     *         {@code array.length - offset}
+     *         {@code src.length - offset}
      *
      * @return  This buffer
      *
      * @throws  BufferOverflowException
      *          If there is insufficient space in this buffer

@@ -904,10 +989,160 @@
      */
     public final $Type$Buffer put($type$[] src) {
         return put(src, 0, src.length);
     }
 
+    /**
+     * Absolute bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
+     *
+     * <p> This method transfers {@code length} $type$s from the given
+     * array, starting at the given offset in the array and at the given index
+     * in this buffer.  The position of this buffer is unchanged.
+     *
+     * <p> An invocation of this method of the form
+     * <code>dst.put(index,&nbsp;src,&nbsp;offset,&nbsp;length)</code>
+     * has exactly the same effect as the following loop except that it first
+     * checks the consistency of the supplied parameters and it is potentially
+     * much more efficient:
+     *
+     * <pre>{@code
+     *     for (int i = offset, j = index; i < offset + length; i++, j++)
+     *         dst.put(j, src[i]);
+     * }</pre>
+     *
+     * @param  index
+     *         The index in this buffer at which the first $type$ will be
+     *         written; must be non-negative and less than {@code limit()}
+     *
+     * @param  src
+     *         The array from which $type$s are to be read
+     *
+     * @param  offset
+     *         The offset within the array of the first $type$ to be read;
+     *         must be non-negative and less than {@code src.length}
+     *
+     * @param  length
+     *         The number of $type$s to be read from the given array;
+     *         must be non-negative and no larger than the smaller of
+     *         {@code limit() - index} and {@code src.length - offset}
+     *
+     * @return  This buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If the preconditions on the {@code index}, {@code offset}, and
+     *          {@code length}, parameters do not hold
+     *
+     * @throws  ReadOnlyBufferException
+     *          If this buffer is read-only
+     *
+     * @since 13
+     */
+    public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
+        //System.out.println("Absolute bulk put array");
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
+        Objects.checkFromIndexSize(index, length, limit());
+        Objects.checkFromIndexSize(offset, length, src.length);
+        int end = offset + length;
+        for (int i = offset, j = index; i < end; i++, j++)
+            this.put(j, src[i]);
+        return this;
+    }
+
+    /**
+     * Absolute bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
+     *
+     * <p> This method copies $type$s into this buffer from the given source
+     * array.  The position of this buffer is unchanged.  An invocation of this
+     * method of the form <code>dst.put(index,&nbsp;src)</code>
+     * behaves in exactly the same was as the invocation:
+     *
+     * <pre>
+     *     dst.put(index, src, 0, src.length); </pre>
+     *
+     * @param  index
+     *         The index in this buffer at which the first $type$ will be
+     *         written; must be non-negative and less than {@code limit()}
+     *
+     * @param  src
+     *         The array from which $type$s are to be read
+     *
+     * @return  This buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If the index is negative, not smaller than {@code limit()}, or
+     *          {@code limit() - index < src.length}
+     *
+     * @throws  ReadOnlyBufferException
+     *          If this buffer is read-only
+     *
+     * @since 13
+     */
+    public $Type$Buffer put(int index, $type$[] src) {
+        return put(index, src, 0, src.length);
+    }
+
+    /**
+     * Absolute bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
+     *
+     * <p> This method transfers {@code length} $type$s into this buffer
+     * from the source buffer, starting at the given {@code srcIndex} in the
+     * source buffer and the given {@code index} in this buffer.  The positions
+     * of both buffers are unchanged.  The source buffer may be this buffer and
+     * the specified regions may overlap.
+     *
+     * <p> An invocation of this method of the form
+     * <code>dst.put(index,&nbsp;src,&nbsp;srcIndex,&nbsp;length)</code>
+     * has exactly the same effect as the following loop except that it first
+     * checks the consistency of the supplied parameters and it is potentially
+     * much more efficient:
+     *
+     * <pre>{@code
+     *     for (int i = srcIndex, j = index; i < srcIndex + length; i++, j++)
+     *         dst.put(j, src.get(i));
+     * }</pre>
+     *
+     * @param  index
+     *         The index in this buffer at which the first $type$ will be
+     *         written; must be non-negative and less than {@code limit()}
+     *
+     * @param  src
+     *         The buffer from which $type$s are to be read
+     *
+     * @param  srcIndex
+     *         The index within the source buffer of the first $type$ to be
+     *         read; must be non-negative and less than {@code src.limit()}
+     *
+     * @param  length
+     *         The number of $type$s to be read from the given buffer;
+     *         must be non-negative and no larger than the smaller of
+     *         {@code limit() - index} and {@code src.limit() - offset}
+     *
+     * @return  This buffer
+     *
+     * @throws  IndexOutOfBoundsException
+     *          If the preconditions on the {@code index}, {@code srcIndex}, and
+     *          {@code length}, parameters do not hold
+     *
+     * @throws  ReadOnlyBufferException
+     *          If this buffer is read-only
+     *
+     * @since 13
+     */
+    public $Type$Buffer put(int index, $Type$Buffer src, int srcIndex,
+        int length) {
+        //System.out.println("Absolute bulk put buffer");
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
+        Objects.checkFromIndexSize(index, length, limit());
+        int n = src.limit();
+        Objects.checkFromIndexSize(srcIndex, length, n);
+        for (int i = srcIndex, j = index; i < n; i++, j++)
+            put(j, src.get(i));
+        return this;
+    }
+
 #if[char]
 
     /**
      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
      *
< prev index next >