< prev index next >

src/java.base/share/classes/java/nio/Buffer.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio;
  27 
  28 import jdk.internal.HotSpotIntrinsicCandidate;
  29 import jdk.internal.access.JavaNioAccess;
  30 import jdk.internal.access.SharedSecrets;

  31 import jdk.internal.misc.Unsafe;

  32 
  33 import java.util.Spliterator;
  34 
  35 /**
  36  * A container for data of a specific primitive type.
  37  *
  38  * <p> A buffer is a linear, finite sequence of elements of a specific
  39  * primitive type.  Aside from its content, the essential properties of a
  40  * buffer are its capacity, limit, and position: </p>
  41  *
  42  * <blockquote>
  43  *
  44  *   <p> A buffer's <i>capacity</i> is the number of elements it contains.  The
  45  *   capacity of a buffer is never negative and never changes.  </p>
  46  *
  47  *   <p> A buffer's <i>limit</i> is the index of the first element that should
  48  *   not be read or written.  A buffer's limit is never negative and is never
  49  *   greater than its capacity.  </p>
  50  *
  51  *   <p> A buffer's <i>position</i> is the index of the next element to be


 196         Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED;
 197 
 198     // Invariants: mark <= position <= limit <= capacity
 199     private int mark = -1;
 200     private int position = 0;
 201     private int limit;
 202     private int capacity;
 203 
 204     // Used by heap byte buffers or direct buffers with Unsafe access
 205     // For heap byte buffers this field will be the address relative to the
 206     // array base address and offset into that array. The address might
 207     // not align on a word boundary for slices, nor align at a long word
 208     // (8 byte) boundary for byte[] allocations on 32-bit systems.
 209     // For direct buffers it is the start address of the memory region. The
 210     // address might not align on a word boundary for slices, nor when created
 211     // using JNI, see NewDirectByteBuffer(void*, long).
 212     // Should ideally be declared final
 213     // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
 214     long address;
 215 












 216     // Creates a new buffer with the given mark, position, limit, and capacity,
 217     // after checking invariants.
 218     //
 219     Buffer(int mark, int pos, int lim, int cap) {       // package-private
 220         if (cap < 0)
 221             throw createCapacityException(cap);
 222         this.capacity = cap;

 223         limit(lim);
 224         position(pos);
 225         if (mark >= 0) {
 226             if (mark > pos)
 227                 throw new IllegalArgumentException("mark > position: ("
 228                                                    + mark + " > " + pos + ")");
 229             this.mark = mark;
 230         }
 231     }
 232 
 233     /**
 234      * Returns an {@code IllegalArgumentException} indicating that the source
 235      * and target are the same {@code Buffer}.  Intended for use in
 236      * {@code put(src)} when the parameter is the {@code Buffer} on which the
 237      * method is being invoked.
 238      *
 239      * @return  IllegalArgumentException
 240      *          With a message indicating equal source and target buffers
 241      */
 242     static IllegalArgumentException createSameBufferException() {


 714     final int checkIndex(int i) {                       // package-private
 715         if ((i < 0) || (i >= limit))
 716             throw new IndexOutOfBoundsException();
 717         return i;
 718     }
 719 
 720     final int checkIndex(int i, int nb) {               // package-private
 721         if ((i < 0) || (nb > limit - i))
 722             throw new IndexOutOfBoundsException();
 723         return i;
 724     }
 725 
 726     final int markValue() {                             // package-private
 727         return mark;
 728     }
 729 
 730     final void discardMark() {                          // package-private
 731         mark = -1;
 732     }
 733 







 734     static {
 735         // setup access to this package in SharedSecrets
 736         SharedSecrets.setJavaNioAccess(
 737             new JavaNioAccess() {
 738                 @Override
 739                 public JavaNioAccess.BufferPool getDirectBufferPool() {
 740                     return Bits.BUFFER_POOL;

























 741                 }
 742             });
 743     }
 744 
 745 }


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio;
  27 
  28 import jdk.internal.HotSpotIntrinsicCandidate;
  29 import jdk.internal.access.JavaNioAccess;
  30 import jdk.internal.access.SharedSecrets;
  31 import jdk.internal.access.foreign.MemorySegmentProxy;
  32 import jdk.internal.misc.Unsafe;
  33 import jdk.internal.vm.annotation.ForceInline;
  34 
  35 import java.util.Spliterator;
  36 
  37 /**
  38  * A container for data of a specific primitive type.
  39  *
  40  * <p> A buffer is a linear, finite sequence of elements of a specific
  41  * primitive type.  Aside from its content, the essential properties of a
  42  * buffer are its capacity, limit, and position: </p>
  43  *
  44  * <blockquote>
  45  *
  46  *   <p> A buffer's <i>capacity</i> is the number of elements it contains.  The
  47  *   capacity of a buffer is never negative and never changes.  </p>
  48  *
  49  *   <p> A buffer's <i>limit</i> is the index of the first element that should
  50  *   not be read or written.  A buffer's limit is never negative and is never
  51  *   greater than its capacity.  </p>
  52  *
  53  *   <p> A buffer's <i>position</i> is the index of the next element to be


 198         Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED;
 199 
 200     // Invariants: mark <= position <= limit <= capacity
 201     private int mark = -1;
 202     private int position = 0;
 203     private int limit;
 204     private int capacity;
 205 
 206     // Used by heap byte buffers or direct buffers with Unsafe access
 207     // For heap byte buffers this field will be the address relative to the
 208     // array base address and offset into that array. The address might
 209     // not align on a word boundary for slices, nor align at a long word
 210     // (8 byte) boundary for byte[] allocations on 32-bit systems.
 211     // For direct buffers it is the start address of the memory region. The
 212     // address might not align on a word boundary for slices, nor when created
 213     // using JNI, see NewDirectByteBuffer(void*, long).
 214     // Should ideally be declared final
 215     // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
 216     long address;
 217 
 218     // Used by buffers generated by the memory access API (JEP-370)
 219     final MemorySegmentProxy segment;
 220 
 221 
 222     // Creates a new buffer with given address and capacity.
 223     //
 224     Buffer(long addr, int cap, MemorySegmentProxy segment) {
 225         this.address = addr;
 226         this.capacity = cap;
 227         this.segment = segment;
 228     }
 229 
 230     // Creates a new buffer with the given mark, position, limit, and capacity,
 231     // after checking invariants.
 232     //
 233     Buffer(int mark, int pos, int lim, int cap, MemorySegmentProxy segment) {       // package-private
 234         if (cap < 0)
 235             throw createCapacityException(cap);
 236         this.capacity = cap;
 237         this.segment = segment;
 238         limit(lim);
 239         position(pos);
 240         if (mark >= 0) {
 241             if (mark > pos)
 242                 throw new IllegalArgumentException("mark > position: ("
 243                                                    + mark + " > " + pos + ")");
 244             this.mark = mark;
 245         }
 246     }
 247 
 248     /**
 249      * Returns an {@code IllegalArgumentException} indicating that the source
 250      * and target are the same {@code Buffer}.  Intended for use in
 251      * {@code put(src)} when the parameter is the {@code Buffer} on which the
 252      * method is being invoked.
 253      *
 254      * @return  IllegalArgumentException
 255      *          With a message indicating equal source and target buffers
 256      */
 257     static IllegalArgumentException createSameBufferException() {


 729     final int checkIndex(int i) {                       // package-private
 730         if ((i < 0) || (i >= limit))
 731             throw new IndexOutOfBoundsException();
 732         return i;
 733     }
 734 
 735     final int checkIndex(int i, int nb) {               // package-private
 736         if ((i < 0) || (nb > limit - i))
 737             throw new IndexOutOfBoundsException();
 738         return i;
 739     }
 740 
 741     final int markValue() {                             // package-private
 742         return mark;
 743     }
 744 
 745     final void discardMark() {                          // package-private
 746         mark = -1;
 747     }
 748 
 749     @ForceInline
 750     final void checkSegment() {
 751         if (segment != null) {
 752             segment.checkValidState();
 753         }
 754     }
 755 
 756     static {
 757         // setup access to this package in SharedSecrets
 758         SharedSecrets.setJavaNioAccess(
 759             new JavaNioAccess() {
 760                 @Override
 761                 public JavaNioAccess.BufferPool getDirectBufferPool() {
 762                     return Bits.BUFFER_POOL;
 763                 }
 764 
 765                 @Override
 766                 public ByteBuffer newDirectByteBuffer(long addr, int cap, Object obj, MemorySegmentProxy segment) {
 767                     return new DirectByteBuffer(addr, cap, obj, segment);
 768                 }
 769 
 770                 @Override
 771                 public ByteBuffer newHeapByteBuffer(byte[] hb, int offset, int capacity, MemorySegmentProxy segment) {
 772                     return new HeapByteBuffer(hb, offset, capacity, segment);
 773                 }
 774 
 775                 @Override
 776                 public Object getBufferBase(ByteBuffer bb) {
 777                     return bb.base();
 778                 }
 779 
 780                 @Override
 781                 public long getBufferAddress(ByteBuffer bb) {
 782                     return bb.address;
 783                 }
 784 
 785                 @Override
 786                 public void checkSegment(Buffer buffer) {
 787                     buffer.checkSegment();
 788                 }
 789             });
 790     }
 791 
 792 }
< prev index next >