< prev index next >

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

Print this page
rev 57242 : imported patch 8234049.patch


  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 import java.util.Objects;

  31 import jdk.internal.misc.Unsafe;
  32 
  33 class ByteBufferAs$Type$Buffer$RW$$BO$                  // package-private
  34     extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$}
  35 {
  36 
  37 #if[rw]
  38 
  39     protected final ByteBuffer bb;
  40 
  41 #end[rw]
  42 
  43     ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb) {   // package-private
  44 #if[rw]
  45         super(-1, 0,
  46               bb.remaining() >> $LG_BYTES_PER_VALUE$,
  47               bb.remaining() >> $LG_BYTES_PER_VALUE$);
  48         this.bb = bb;
  49         // enforce limit == capacity
  50         int cap = this.capacity();
  51         this.limit(cap);
  52         int pos = this.position();
  53         assert (pos <= cap);
  54         address = bb.address;
  55 #else[rw]
  56         super(bb);
  57 #end[rw]
  58     }
  59 
  60     ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb,
  61                                      int mark, int pos, int lim, int cap,
  62                                      long addr)
  63     {
  64 #if[rw]
  65         super(mark, pos, lim, cap);
  66         this.bb = bb;
  67         address = addr;
  68         assert address >= bb.address;
  69 #else[rw]
  70         super(bb, mark, pos, lim, cap, addr);
  71 #end[rw]
  72     }
  73 
  74     @Override
  75     Object base() {
  76         return bb.hb;
  77     }
  78 
  79     public $Type$Buffer slice() {
  80         int pos = this.position();
  81         int lim = this.limit();
  82         assert (pos <= lim);
  83         int rem = (pos <= lim ? lim - pos : 0);
  84         long addr = byteOffset(pos);
  85         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, addr);
  86     }
  87 
  88     @Override
  89     public $Type$Buffer slice(int index, int length) {
  90         Objects.checkFromIndexSize(index, length, limit());
  91         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
  92                                                     -1,
  93                                                     0,
  94                                                     length,
  95                                                     length,
  96                                                     byteOffset(index));
  97     }
  98 
  99     public $Type$Buffer duplicate() {
 100         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
 101                                                     this.markValue(),
 102                                                     this.position(),
 103                                                     this.limit(),
 104                                                     this.capacity(),
 105                                                     address);
 106     }
 107 
 108     public $Type$Buffer asReadOnlyBuffer() {
 109 #if[rw]
 110         return new ByteBufferAs$Type$BufferR$BO$(bb,
 111                                                  this.markValue(),
 112                                                  this.position(),
 113                                                  this.limit(),
 114                                                  this.capacity(),
 115                                                  address);
 116 #else[rw]
 117         return duplicate();
 118 #end[rw]
 119     }
 120 
 121 #if[rw]
 122 
 123     private int ix(int i) {
 124         int off = (int) (address - bb.address);
 125         return (i << $LG_BYTES_PER_VALUE$) + off;
 126     }
 127 
 128     protected long byteOffset(long i) {
 129         return (i << $LG_BYTES_PER_VALUE$) + address;
 130     }
 131 
 132     public $type$ get() {

 133         $memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(nextGetIndex()),
 134             {#if[boB]?true:false});
 135         return $fromBits$(x);
 136     }
 137 
 138     public $type$ get(int i) {

 139         $memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)),
 140             {#if[boB]?true:false});
 141         return $fromBits$(x);
 142     }
 143 
 144 #if[streamableType]
 145    $type$ getUnchecked(int i) {
 146         $memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(i),
 147             {#if[boB]?true:false});
 148         return $fromBits$(x);
 149     }
 150 #end[streamableType]
 151 
 152 #end[rw]
 153 
 154     public $Type$Buffer put($type$ x) {
 155 #if[rw]

 156         $memtype$ y = $toBits$(x);
 157         UNSAFE.put$Memtype$Unaligned(bb.hb, byteOffset(nextPutIndex()), y,
 158             {#if[boB]?true:false});
 159         return this;
 160 #else[rw]
 161         throw new ReadOnlyBufferException();
 162 #end[rw]
 163     }
 164 
 165     public $Type$Buffer put(int i, $type$ x) {
 166 #if[rw]

 167         $memtype$ y = $toBits$(x);
 168         UNSAFE.put$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)), y,
 169             {#if[boB]?true:false});
 170         return this;
 171 #else[rw]
 172         throw new ReadOnlyBufferException();
 173 #end[rw]
 174     }
 175 
 176     public $Type$Buffer compact() {
 177 #if[rw]
 178         int pos = position();
 179         int lim = limit();
 180         assert (pos <= lim);
 181         int rem = (pos <= lim ? lim - pos : 0);
 182 
 183         ByteBuffer db = bb.duplicate();
 184         db.limit(ix(lim));
 185         db.position(ix(0));
 186         ByteBuffer sb = db.slice();


 220             throw new IndexOutOfBoundsException();
 221         }
 222     }
 223 
 224 
 225     // --- Methods to support CharSequence ---
 226 
 227     public CharBuffer subSequence(int start, int end) {
 228         int pos = position();
 229         int lim = limit();
 230         assert (pos <= lim);
 231         pos = (pos <= lim ? pos : lim);
 232         int len = lim - pos;
 233 
 234         Objects.checkFromToIndex(start, end, len);
 235         return new ByteBufferAsCharBuffer$RW$$BO$(bb,
 236                                                   -1,
 237                                                   pos + start,
 238                                                   pos + end,
 239                                                   capacity(),
 240                                                   address);
 241     }
 242 
 243 #end[char]
 244 
 245 
 246     public ByteOrder order() {
 247 #if[boB]
 248         return ByteOrder.BIG_ENDIAN;
 249 #end[boB]
 250 #if[boL]
 251         return ByteOrder.LITTLE_ENDIAN;
 252 #end[boL]
 253     }
 254 
 255 #if[char]
 256     ByteOrder charRegionOrder() {
 257         return order();
 258     }
 259 #end[char]
 260 }


  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 import java.util.Objects;
  31 import jdk.internal.access.foreign.MemorySegmentProxy;
  32 import jdk.internal.misc.Unsafe;
  33 
  34 class ByteBufferAs$Type$Buffer$RW$$BO$                  // package-private
  35     extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$}
  36 {
  37 
  38 #if[rw]
  39 
  40     protected final ByteBuffer bb;
  41 
  42 #end[rw]
  43 
  44     ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb, MemorySegmentProxy segment) {   // package-private
  45 #if[rw]
  46         super(-1, 0,
  47               bb.remaining() >> $LG_BYTES_PER_VALUE$,
  48               bb.remaining() >> $LG_BYTES_PER_VALUE$, segment);
  49         this.bb = bb;
  50         // enforce limit == capacity
  51         int cap = this.capacity();
  52         this.limit(cap);
  53         int pos = this.position();
  54         assert (pos <= cap);
  55         address = bb.address;
  56 #else[rw]
  57         super(bb, segment);
  58 #end[rw]
  59     }
  60 
  61     ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb,
  62                                      int mark, int pos, int lim, int cap,
  63                                      long addr, MemorySegmentProxy segment)
  64     {
  65 #if[rw]
  66         super(mark, pos, lim, cap, segment);
  67         this.bb = bb;
  68         address = addr;
  69         assert address >= bb.address;
  70 #else[rw]
  71         super(bb, mark, pos, lim, cap, addr, segment);
  72 #end[rw]
  73     }
  74 
  75     @Override
  76     Object base() {
  77         return bb.hb;
  78     }
  79 
  80     public $Type$Buffer slice() {
  81         int pos = this.position();
  82         int lim = this.limit();
  83         assert (pos <= lim);
  84         int rem = (pos <= lim ? lim - pos : 0);
  85         long addr = byteOffset(pos);
  86         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, addr, segment);
  87     }
  88 
  89     @Override
  90     public $Type$Buffer slice(int index, int length) {
  91         Objects.checkFromIndexSize(index, length, limit());
  92         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
  93                                                     -1,
  94                                                     0,
  95                                                     length,
  96                                                     length,
  97                                                     byteOffset(index), segment);
  98     }
  99 
 100     public $Type$Buffer duplicate() {
 101         return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
 102                                                     this.markValue(),
 103                                                     this.position(),
 104                                                     this.limit(),
 105                                                     this.capacity(),
 106                                                     address, segment);
 107     }
 108 
 109     public $Type$Buffer asReadOnlyBuffer() {
 110 #if[rw]
 111         return new ByteBufferAs$Type$BufferR$BO$(bb,
 112                                                  this.markValue(),
 113                                                  this.position(),
 114                                                  this.limit(),
 115                                                  this.capacity(),
 116                                                  address, segment);
 117 #else[rw]
 118         return duplicate();
 119 #end[rw]
 120     }
 121 
 122 #if[rw]
 123 
 124     private int ix(int i) {
 125         int off = (int) (address - bb.address);
 126         return (i << $LG_BYTES_PER_VALUE$) + off;
 127     }
 128 
 129     protected long byteOffset(long i) {
 130         return (i << $LG_BYTES_PER_VALUE$) + address;
 131     }
 132 
 133     public $type$ get() {
 134         checkSegment();
 135         $memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(nextGetIndex()),
 136             {#if[boB]?true:false});
 137         return $fromBits$(x);
 138     }
 139 
 140     public $type$ get(int i) {
 141         checkSegment();
 142         $memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)),
 143             {#if[boB]?true:false});
 144         return $fromBits$(x);
 145     }
 146 
 147 #if[streamableType]
 148    $type$ getUnchecked(int i) {
 149         $memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(i),
 150             {#if[boB]?true:false});
 151         return $fromBits$(x);
 152     }
 153 #end[streamableType]
 154 
 155 #end[rw]
 156 
 157     public $Type$Buffer put($type$ x) {
 158 #if[rw]
 159         checkSegment();
 160         $memtype$ y = $toBits$(x);
 161         UNSAFE.put$Memtype$Unaligned(bb.hb, byteOffset(nextPutIndex()), y,
 162             {#if[boB]?true:false});
 163         return this;
 164 #else[rw]
 165         throw new ReadOnlyBufferException();
 166 #end[rw]
 167     }
 168 
 169     public $Type$Buffer put(int i, $type$ x) {
 170 #if[rw]
 171         checkSegment();
 172         $memtype$ y = $toBits$(x);
 173         UNSAFE.put$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)), y,
 174             {#if[boB]?true:false});
 175         return this;
 176 #else[rw]
 177         throw new ReadOnlyBufferException();
 178 #end[rw]
 179     }
 180 
 181     public $Type$Buffer compact() {
 182 #if[rw]
 183         int pos = position();
 184         int lim = limit();
 185         assert (pos <= lim);
 186         int rem = (pos <= lim ? lim - pos : 0);
 187 
 188         ByteBuffer db = bb.duplicate();
 189         db.limit(ix(lim));
 190         db.position(ix(0));
 191         ByteBuffer sb = db.slice();


 225             throw new IndexOutOfBoundsException();
 226         }
 227     }
 228 
 229 
 230     // --- Methods to support CharSequence ---
 231 
 232     public CharBuffer subSequence(int start, int end) {
 233         int pos = position();
 234         int lim = limit();
 235         assert (pos <= lim);
 236         pos = (pos <= lim ? pos : lim);
 237         int len = lim - pos;
 238 
 239         Objects.checkFromToIndex(start, end, len);
 240         return new ByteBufferAsCharBuffer$RW$$BO$(bb,
 241                                                   -1,
 242                                                   pos + start,
 243                                                   pos + end,
 244                                                   capacity(),
 245                                                   address, segment);
 246     }
 247 
 248 #end[char]
 249 
 250 
 251     public ByteOrder order() {
 252 #if[boB]
 253         return ByteOrder.BIG_ENDIAN;
 254 #end[boB]
 255 #if[boL]
 256         return ByteOrder.LITTLE_ENDIAN;
 257 #end[boL]
 258     }
 259 
 260 #if[char]
 261     ByteOrder charRegionOrder() {
 262         return order();
 263     }
 264 #end[char]
 265 }
< prev index next >