src/java.base/share/classes/java/nio/X-Buffer.java.template
changeset 53855 7c362992527a
parent 53832 842b45d8e25e
child 53959 1542e63eb537
equal deleted inserted replaced
53854:792c8a5fbb29 53855:7c362992527a
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    34 import java.util.Spliterator;
    34 import java.util.Spliterator;
    35 import java.util.stream.StreamSupport;
    35 import java.util.stream.StreamSupport;
    36 import java.util.stream.$Streamtype$Stream;
    36 import java.util.stream.$Streamtype$Stream;
    37 #end[streamableType]
    37 #end[streamableType]
    38 
    38 
       
    39 import java.util.Objects;
    39 import jdk.internal.util.ArraysSupport;
    40 import jdk.internal.util.ArraysSupport;
    40 
    41 
    41 /**
    42 /**
    42  * $A$ $type$ buffer.
    43  * $A$ $type$ buffer.
    43  *
    44  *
    48  *
    49  *
    49  *   <li><p> Absolute and relative {@link #get() <i>get</i>} and
    50  *   <li><p> Absolute and relative {@link #get() <i>get</i>} and
    50  *   {@link #put($type$) <i>put</i>} methods that read and write
    51  *   {@link #put($type$) <i>put</i>} methods that read and write
    51  *   single $type$s; </p></li>
    52  *   single $type$s; </p></li>
    52  *
    53  *
    53  *   <li><p> Relative {@link #get($type$[]) <i>bulk get</i>}
    54  *   <li><p> Absolute and relative {@link #get($type$[]) <i>bulk get</i>}
    54  *   methods that transfer contiguous sequences of $type$s from this buffer
    55  *   methods that transfer contiguous sequences of $type$s from this buffer
    55  *   into an array; {#if[!byte]?and}</p></li>
    56  *   into an array; {#if[!byte]?and}</p></li>
    56  *
    57  *
    57  *   <li><p> Relative {@link #put($type$[]) <i>bulk put</i>}
    58  *   <li><p> Absolute and relative {@link #put($type$[]) <i>bulk put</i>}
    58  *   methods that transfer contiguous sequences of $type$s from $a$
    59  *   methods that transfer contiguous sequences of $type$s from $a$
    59  *   $type$ array{#if[char]?,&#32;a&#32;string,} or some other $type$
    60  *   $type$ array{#if[char]?,&#32;a&#32;string,} or some other $type$
    60  *   buffer into this buffer;{#if[!byte]?&#32;and} </p></li>
    61  *   buffer into this buffer;{#if[!byte]?&#32;and} </p></li>
    61  *
    62  *
    62 #if[byte]
    63 #if[byte]
   434      *
   435      *
   435      * @param target the buffer to read characters into
   436      * @param target the buffer to read characters into
   436      * @return The number of characters added to the buffer, or
   437      * @return The number of characters added to the buffer, or
   437      *         -1 if this source of characters is at its end
   438      *         -1 if this source of characters is at its end
   438      * @throws IOException if an I/O error occurs
   439      * @throws IOException if an I/O error occurs
   439      * @throws NullPointerException if target is null
       
   440      * @throws ReadOnlyBufferException if target is a read only buffer
   440      * @throws ReadOnlyBufferException if target is a read only buffer
   441      * @since 1.5
   441      * @since 1.5
   442      */
   442      */
   443     public int read(CharBuffer target) throws IOException {
   443     public int read(CharBuffer target) throws IOException {
   444         // Determine the number of bytes n that can be transferred
   444         // Determine the number of bytes n that can be transferred
   762      */
   762      */
   763     public $Type$Buffer get($type$[] dst) {
   763     public $Type$Buffer get($type$[] dst) {
   764         return get(dst, 0, dst.length);
   764         return get(dst, 0, dst.length);
   765     }
   765     }
   766 
   766 
       
   767     /**
       
   768      * Absolute bulk <i>get</i> method.
       
   769      *
       
   770      * <p> This method transfers {@code length} $type$s from this
       
   771      * buffer into the given array, starting at the given index in this
       
   772      * buffer and at the given offset in the array.  The position of this
       
   773      * buffer is unchanged.
       
   774      *
       
   775      * <p> An invocation of this method of the form
       
   776      * <code>src.get(index,&nbsp;dst,&nbsp;offset,&nbsp;length)</code>
       
   777      * has exactly the same effect as the following loop except that it first
       
   778      * checks the consistency of the supplied parameters and it is potentially
       
   779      * much more efficient:
       
   780      *
       
   781      * <pre>{@code
       
   782      *     for (int i = offset, j = index; i < offset + length; i++, j++)
       
   783      *         dst[i] = src.get(j);
       
   784      * }</pre>
       
   785      *
       
   786      * @param  index
       
   787      *         The index in this buffer from which the first $type$ will be
       
   788      *         read; must be non-negative and less than {@code limit()}
       
   789      *
       
   790      * @param  dst
       
   791      *         The destination array
       
   792      *
       
   793      * @param  offset
       
   794      *         The offset within the array of the first $type$ to be
       
   795      *         written; must be non-negative and less than
       
   796      *         {@code dst.length}
       
   797      *
       
   798      * @param  length
       
   799      *         The number of $type$s to be written to the given array;
       
   800      *         must be non-negative and no larger than the smaller of
       
   801      *         {@code limit() - index} and {@code dst.length - offset}
       
   802      *
       
   803      * @return  This buffer
       
   804      *
       
   805      * @throws  IndexOutOfBoundsException
       
   806      *          If the preconditions on the {@code index}, {@code offset}, and
       
   807      *          {@code length} parameters do not hold
       
   808      *
       
   809      * @since 13
       
   810      */
       
   811     public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
       
   812         Objects.checkFromIndexSize(index, length, limit());
       
   813         Objects.checkFromIndexSize(offset, length, dst.length);
       
   814         int end = offset + length;
       
   815         for (int i = offset, j = index; i < end; i++, j++)
       
   816             dst[i] = get(j);
       
   817         return this;
       
   818     }
       
   819 
       
   820     /**
       
   821      * Absolute bulk <i>get</i> method.
       
   822      *
       
   823      * <p> This method transfers $type$s from this buffer into the given
       
   824      * destination array.  The position of this buffer is unchanged.  An
       
   825      * invocation of this method of the form
       
   826      * <code>src.get(index,&nbsp;dst)</code> behaves in exactly the same
       
   827      * way as the invocation:
       
   828      *
       
   829      * <pre>
       
   830      *     src.get(index, dst, 0, dst.length) </pre>
       
   831      *
       
   832      * @param  index
       
   833      *         The index in this buffer from which the first $type$ will be
       
   834      *         read; must be non-negative and less than {@code limit()}
       
   835      *
       
   836      * @param  dst
       
   837      *         The destination array
       
   838      *
       
   839      * @return  This buffer
       
   840      *
       
   841      * @throws  IndexOutOfBoundsException
       
   842      *          If {@code index} is negative, not smaller than {@code limit()},
       
   843      *          or {@code limit() - index < dst.length}
       
   844      *
       
   845      * @since 13
       
   846      */
       
   847     public $Type$Buffer get(int index, $type$[] dst) {
       
   848         return get(index, dst, 0, dst.length);
       
   849     }
   767 
   850 
   768     // -- Bulk put operations --
   851     // -- Bulk put operations --
   769 
   852 
   770     /**
   853     /**
   771      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
   854      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
   840      * <code>dst.put(src,&nbsp;off,&nbsp;len)</code> has exactly the same effect as
   923      * <code>dst.put(src,&nbsp;off,&nbsp;len)</code> has exactly the same effect as
   841      * the loop
   924      * the loop
   842      *
   925      *
   843      * <pre>{@code
   926      * <pre>{@code
   844      *     for (int i = off; i < off + len; i++)
   927      *     for (int i = off; i < off + len; i++)
   845      *         dst.put(a[i]);
   928      *         dst.put(src[i]);
   846      * }</pre>
   929      * }</pre>
   847      *
   930      *
   848      * except that it first checks that there is sufficient space in this
   931      * except that it first checks that there is sufficient space in this
   849      * buffer and it is potentially much more efficient.
   932      * buffer and it is potentially much more efficient.
   850      *
   933      *
   851      * @param  src
   934      * @param  src
   852      *         The array from which $type$s are to be read
   935      *         The array from which $type$s are to be read
   853      *
   936      *
   854      * @param  offset
   937      * @param  offset
   855      *         The offset within the array of the first $type$ to be read;
   938      *         The offset within the array of the first $type$ to be read;
   856      *         must be non-negative and no larger than {@code array.length}
   939      *         must be non-negative and no larger than {@code src.length}
   857      *
   940      *
   858      * @param  length
   941      * @param  length
   859      *         The number of $type$s to be read from the given array;
   942      *         The number of $type$s to be read from the given array;
   860      *         must be non-negative and no larger than
   943      *         must be non-negative and no larger than
   861      *         {@code array.length - offset}
   944      *         {@code src.length - offset}
   862      *
   945      *
   863      * @return  This buffer
   946      * @return  This buffer
   864      *
   947      *
   865      * @throws  BufferOverflowException
   948      * @throws  BufferOverflowException
   866      *          If there is insufficient space in this buffer
   949      *          If there is insufficient space in this buffer
   904      * @throws  ReadOnlyBufferException
   987      * @throws  ReadOnlyBufferException
   905      *          If this buffer is read-only
   988      *          If this buffer is read-only
   906      */
   989      */
   907     public final $Type$Buffer put($type$[] src) {
   990     public final $Type$Buffer put($type$[] src) {
   908         return put(src, 0, src.length);
   991         return put(src, 0, src.length);
       
   992     }
       
   993 
       
   994     /**
       
   995      * Absolute bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
       
   996      *
       
   997      * <p> This method transfers {@code length} $type$s from the given
       
   998      * array, starting at the given offset in the array and at the given index
       
   999      * in this buffer.  The position of this buffer is unchanged.
       
  1000      *
       
  1001      * <p> An invocation of this method of the form
       
  1002      * <code>dst.put(index,&nbsp;src,&nbsp;offset,&nbsp;length)</code>
       
  1003      * has exactly the same effect as the following loop except that it first
       
  1004      * checks the consistency of the supplied parameters and it is potentially
       
  1005      * much more efficient:
       
  1006      *
       
  1007      * <pre>{@code
       
  1008      *     for (int i = offset, j = index; i < offset + length; i++, j++)
       
  1009      *         dst.put(j, src[i]);
       
  1010      * }</pre>
       
  1011      *
       
  1012      * @param  index
       
  1013      *         The index in this buffer at which the first $type$ will be
       
  1014      *         written; must be non-negative and less than {@code limit()}
       
  1015      *
       
  1016      * @param  src
       
  1017      *         The array from which $type$s are to be read
       
  1018      *
       
  1019      * @param  offset
       
  1020      *         The offset within the array of the first $type$ to be read;
       
  1021      *         must be non-negative and less than {@code src.length}
       
  1022      *
       
  1023      * @param  length
       
  1024      *         The number of $type$s to be read from the given array;
       
  1025      *         must be non-negative and no larger than the smaller of
       
  1026      *         {@code limit() - index} and {@code src.length - offset}
       
  1027      *
       
  1028      * @return  This buffer
       
  1029      *
       
  1030      * @throws  IndexOutOfBoundsException
       
  1031      *          If the preconditions on the {@code index}, {@code offset}, and
       
  1032      *          {@code length} parameters do not hold
       
  1033      *
       
  1034      * @throws  ReadOnlyBufferException
       
  1035      *          If this buffer is read-only
       
  1036      *
       
  1037      * @since 13
       
  1038      */
       
  1039     public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
       
  1040         if (isReadOnly())
       
  1041             throw new ReadOnlyBufferException();
       
  1042         Objects.checkFromIndexSize(index, length, limit());
       
  1043         Objects.checkFromIndexSize(offset, length, src.length);
       
  1044         int end = offset + length;
       
  1045         for (int i = offset, j = index; i < end; i++, j++)
       
  1046             this.put(j, src[i]);
       
  1047         return this;
       
  1048     }
       
  1049 
       
  1050     /**
       
  1051      * Absolute bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
       
  1052      *
       
  1053      * <p> This method copies $type$s into this buffer from the given source
       
  1054      * array.  The position of this buffer is unchanged.  An invocation of this
       
  1055      * method of the form <code>dst.put(index,&nbsp;src)</code>
       
  1056      * behaves in exactly the same way as the invocation:
       
  1057      *
       
  1058      * <pre>
       
  1059      *     dst.put(index, src, 0, src.length); </pre>
       
  1060      *
       
  1061      * @param  index
       
  1062      *         The index in this buffer at which the first $type$ will be
       
  1063      *         written; must be non-negative and less than {@code limit()}
       
  1064      *
       
  1065      * @param  src
       
  1066      *         The array from which $type$s are to be read
       
  1067      *
       
  1068      * @return  This buffer
       
  1069      *
       
  1070      * @throws  IndexOutOfBoundsException
       
  1071      *          If {@code index} is negative, not smaller than {@code limit()},
       
  1072      *          or {@code limit() - index < src.length}
       
  1073      *
       
  1074      * @throws  ReadOnlyBufferException
       
  1075      *          If this buffer is read-only
       
  1076      *
       
  1077      * @since 13
       
  1078      */
       
  1079     public $Type$Buffer put(int index, $type$[] src) {
       
  1080         return put(index, src, 0, src.length);
   909     }
  1081     }
   910 
  1082 
   911 #if[char]
  1083 #if[char]
   912 
  1084 
   913     /**
  1085     /**