< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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 #warn This file is preprocessed before being compiled
  27 
  28 package java.nio;
  29 
  30 /**
  31 #if[rw]
  32  * A read/write Heap$Type$Buffer.
  33 #else[rw]
  34  * A read-only Heap$Type$Buffer.  This class extends the corresponding
  35  * read/write class, overriding the mutation methods to throw a {@link
  36  * ReadOnlyBufferException} and overriding the view-buffer methods to return an
  37  * instance of this class rather than of the superclass.
  38 #end[rw]
  39  */
  40 


  41 class Heap$Type$Buffer$RW$
  42     extends {#if[ro]?Heap}$Type$Buffer
  43 {
  44     // Cached array base offset
  45     private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
  46 
  47     // Cached array base offset
  48     private static final long ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale($type$[].class);
  49 
  50     // For speed these fields are actually declared in X-Buffer;
  51     // these declarations are here as documentation
  52     /*
  53 #if[rw]
  54     protected final $type$[] hb;
  55     protected final int offset;
  56 #end[rw]
  57     */
  58 
  59     Heap$Type$Buffer$RW$(int cap, int lim) {            // package-private
  60 #if[rw]


 164 
 165     public $type$ get(int i) {
 166         return hb[ix(checkIndex(i))];
 167     }
 168 
 169 #if[streamableType]
 170     $type$ getUnchecked(int i) {
 171         return hb[ix(i)];
 172     }
 173 #end[streamableType]
 174 
 175     public $Type$Buffer get($type$[] dst, int offset, int length) {
 176         checkBounds(offset, length, dst.length);
 177         if (length > remaining())
 178             throw new BufferUnderflowException();
 179         System.arraycopy(hb, ix(position()), dst, offset, length);
 180         position(position() + length);
 181         return this;
 182     }
 183 








 184     public boolean isDirect() {
 185         return false;
 186     }
 187 
 188 #end[rw]
 189 
 190     public boolean isReadOnly() {
 191         return {#if[rw]?false:true};
 192     }
 193 
 194     public $Type$Buffer put($type$ x) {
 195 #if[rw]
 196         hb[ix(nextPutIndex())] = x;
 197         return this;
 198 #else[rw]
 199         throw new ReadOnlyBufferException();
 200 #end[rw]
 201     }
 202 
 203     public $Type$Buffer put(int i, $type$ x) {


 233                 throw new BufferOverflowException();
 234             System.arraycopy(sb.hb, sb.ix(sb.position()),
 235                              hb, ix(position()), n);
 236             sb.position(sb.position() + n);
 237             position(position() + n);
 238         } else if (src.isDirect()) {
 239             int n = src.remaining();
 240             if (n > remaining())
 241                 throw new BufferOverflowException();
 242             src.get(hb, ix(position()), n);
 243             position(position() + n);
 244         } else {
 245             super.put(src);
 246         }
 247         return this;
 248 #else[rw]
 249         throw new ReadOnlyBufferException();
 250 #end[rw]
 251     }
 252 







































 253     public $Type$Buffer compact() {
 254 #if[rw]
 255         System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
 256         position(remaining());
 257         limit(capacity());
 258         discardMark();
 259         return this;
 260 #else[rw]
 261         throw new ReadOnlyBufferException();
 262 #end[rw]
 263     }
 264 
 265 
 266 
 267 #if[byte]
 268 
 269     byte _get(int i) {                          // package-private
 270         return hb[i];
 271     }
 272 


   1 /*
   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.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  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 #warn This file is preprocessed before being compiled
  27 
  28 package java.nio;
  29 
  30 /**
  31 #if[rw]
  32  * A read/write Heap$Type$Buffer.
  33 #else[rw]
  34  * A read-only Heap$Type$Buffer.  This class extends the corresponding
  35  * read/write class, overriding the mutation methods to throw a {@link
  36  * ReadOnlyBufferException} and overriding the view-buffer methods to return an
  37  * instance of this class rather than of the superclass.
  38 #end[rw]
  39  */
  40 
  41 import java.util.Objects;
  42 
  43 class Heap$Type$Buffer$RW$
  44     extends {#if[ro]?Heap}$Type$Buffer
  45 {
  46     // Cached array base offset
  47     private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
  48 
  49     // Cached array base offset
  50     private static final long ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale($type$[].class);
  51 
  52     // For speed these fields are actually declared in X-Buffer;
  53     // these declarations are here as documentation
  54     /*
  55 #if[rw]
  56     protected final $type$[] hb;
  57     protected final int offset;
  58 #end[rw]
  59     */
  60 
  61     Heap$Type$Buffer$RW$(int cap, int lim) {            // package-private
  62 #if[rw]


 166 
 167     public $type$ get(int i) {
 168         return hb[ix(checkIndex(i))];
 169     }
 170 
 171 #if[streamableType]
 172     $type$ getUnchecked(int i) {
 173         return hb[ix(i)];
 174     }
 175 #end[streamableType]
 176 
 177     public $Type$Buffer get($type$[] dst, int offset, int length) {
 178         checkBounds(offset, length, dst.length);
 179         if (length > remaining())
 180             throw new BufferUnderflowException();
 181         System.arraycopy(hb, ix(position()), dst, offset, length);
 182         position(position() + length);
 183         return this;
 184     }
 185 
 186     public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
 187         //System.out.println("Heap absolute bulk get");
 188         Objects.checkFromIndexSize(index, length, limit());
 189         Objects.checkFromIndexSize(offset, length, dst.length);
 190         System.arraycopy(hb, ix(index), dst, offset, length);
 191         return this;
 192     }
 193 
 194     public boolean isDirect() {
 195         return false;
 196     }
 197 
 198 #end[rw]
 199 
 200     public boolean isReadOnly() {
 201         return {#if[rw]?false:true};
 202     }
 203 
 204     public $Type$Buffer put($type$ x) {
 205 #if[rw]
 206         hb[ix(nextPutIndex())] = x;
 207         return this;
 208 #else[rw]
 209         throw new ReadOnlyBufferException();
 210 #end[rw]
 211     }
 212 
 213     public $Type$Buffer put(int i, $type$ x) {


 243                 throw new BufferOverflowException();
 244             System.arraycopy(sb.hb, sb.ix(sb.position()),
 245                              hb, ix(position()), n);
 246             sb.position(sb.position() + n);
 247             position(position() + n);
 248         } else if (src.isDirect()) {
 249             int n = src.remaining();
 250             if (n > remaining())
 251                 throw new BufferOverflowException();
 252             src.get(hb, ix(position()), n);
 253             position(position() + n);
 254         } else {
 255             super.put(src);
 256         }
 257         return this;
 258 #else[rw]
 259         throw new ReadOnlyBufferException();
 260 #end[rw]
 261     }
 262 
 263     public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
 264         //System.out.println("Heap absolute bulk put array");
 265 #if[rw]
 266         Objects.checkFromIndexSize(index, length, limit());
 267         Objects.checkFromIndexSize(offset, length, src.length);
 268         System.arraycopy(src, offset, hb, ix(index), length);
 269         return this;
 270 #else[rw]
 271         throw new ReadOnlyBufferException();
 272 #end[rw]
 273     }
 274 
 275     public $Type$Buffer put(int index, $Type$Buffer src, int offset,
 276         int length) {
 277         //System.out.println("Heap absolute bulk put buffer");
 278 #if[rw]
 279         if (src instanceof Heap$Type$Buffer) {
 280             if (index < 0)
 281                 throw new IndexOutOfBoundsException("Index negative: " + index);
 282             Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
 283             Objects.checkFromIndexSize(index, length, limit());
 284             Objects.checkFromIndexSize(offset, length, sb.limit());
 285             System.arraycopy(sb.hb, sb.ix(offset),
 286                              hb, ix(index), length);
 287         } else if (src.isDirect()) {
 288             if (index < 0)
 289                 throw new IndexOutOfBoundsException("Index negative: " + index);
 290             Objects.checkFromIndexSize(index, length, limit());
 291             Objects.checkFromIndexSize(offset, length, src.limit());
 292             src.get(offset, hb, ix(index), length);
 293         } else {
 294             super.put(index, src, offset, length);
 295         }
 296         return this;
 297 #else[rw]
 298         throw new ReadOnlyBufferException();
 299 #end[rw]
 300     }
 301 
 302     public $Type$Buffer compact() {
 303 #if[rw]
 304         System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
 305         position(remaining());
 306         limit(capacity());
 307         discardMark();
 308         return this;
 309 #else[rw]
 310         throw new ReadOnlyBufferException();
 311 #end[rw]
 312     }
 313 
 314 
 315 
 316 #if[byte]
 317 
 318     byte _get(int i) {                          // package-private
 319         return hb[i];
 320     }
 321 


< prev index next >