< prev index next >

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

Print this page
rev 15050 : 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
Reviewed-by: shade
   1 /*
   2  * Copyright (c) 2000, 2013, 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 class ByteBufferAs$Type$Buffer$RW$$BO$                  // package-private
  32     extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$}
  33 {
  34 
  35 #if[rw]
  36 



  37     protected final ByteBuffer bb;
  38     protected final int offset;
  39 
  40 #end[rw]
  41 
  42     ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb) {   // package-private
  43 #if[rw]
  44         super(-1, 0,
  45               bb.remaining() >> $LG_BYTES_PER_VALUE$,
  46               bb.remaining() >> $LG_BYTES_PER_VALUE$);
  47         this.bb = bb;
  48         // enforce limit == capacity
  49         int cap = this.capacity();
  50         this.limit(cap);
  51         int pos = this.position();
  52         assert (pos <= cap);
  53         offset = pos;
  54 #else[rw]
  55         super(bb);
  56 #end[rw]


  90 
  91     public $Type$Buffer asReadOnlyBuffer() {
  92 #if[rw]
  93         return new ByteBufferAs$Type$BufferR$BO$(bb,
  94                                                  this.markValue(),
  95                                                  this.position(),
  96                                                  this.limit(),
  97                                                  this.capacity(),
  98                                                  offset);
  99 #else[rw]
 100         return duplicate();
 101 #end[rw]
 102     }
 103 
 104 #if[rw]
 105 
 106     protected int ix(int i) {
 107         return (i << $LG_BYTES_PER_VALUE$) + offset;
 108     }
 109 




 110     public $type$ get() {
 111         return Bits.get$Type$$BO$(bb, ix(nextGetIndex()));


 112     }
 113 
 114     public $type$ get(int i) {
 115         return Bits.get$Type$$BO$(bb, ix(checkIndex(i)));


 116     }
 117 
 118 #if[streamableType]
 119    $type$ getUnchecked(int i) {
 120         return Bits.get$Type$$BO$(bb, ix(i));


 121     }
 122 #end[streamableType]
 123 
 124 #end[rw]
 125 
 126     public $Type$Buffer put($type$ x) {
 127 #if[rw]
 128         Bits.put$Type$$BO$(bb, ix(nextPutIndex()), x);


 129         return this;
 130 #else[rw]
 131         throw new ReadOnlyBufferException();
 132 #end[rw]
 133     }
 134 
 135     public $Type$Buffer put(int i, $type$ x) {
 136 #if[rw]
 137         Bits.put$Type$$BO$(bb, ix(checkIndex(i)), x);


 138         return this;
 139 #else[rw]
 140         throw new ReadOnlyBufferException();
 141 #end[rw]
 142     }
 143 
 144     public $Type$Buffer compact() {
 145 #if[rw]
 146         int pos = position();
 147         int lim = limit();
 148         assert (pos <= lim);
 149         int rem = (pos <= lim ? lim - pos : 0);
 150 
 151         ByteBuffer db = bb.duplicate();
 152         db.limit(ix(lim));
 153         db.position(ix(0));
 154         ByteBuffer sb = db.slice();
 155         sb.position(pos << $LG_BYTES_PER_VALUE$);
 156         sb.compact();
 157         position(rem);


   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 import jdk.internal.misc.Unsafe;
  31 
  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     // Cached unsafe-access object
  40     private static final Unsafe unsafe = Bits.unsafe();
  41 
  42     protected final ByteBuffer bb;
  43     protected final int offset;
  44 
  45 #end[rw]
  46 
  47     ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb) {   // package-private
  48 #if[rw]
  49         super(-1, 0,
  50               bb.remaining() >> $LG_BYTES_PER_VALUE$,
  51               bb.remaining() >> $LG_BYTES_PER_VALUE$);
  52         this.bb = bb;
  53         // enforce limit == capacity
  54         int cap = this.capacity();
  55         this.limit(cap);
  56         int pos = this.position();
  57         assert (pos <= cap);
  58         offset = pos;
  59 #else[rw]
  60         super(bb);
  61 #end[rw]


  95 
  96     public $Type$Buffer asReadOnlyBuffer() {
  97 #if[rw]
  98         return new ByteBufferAs$Type$BufferR$BO$(bb,
  99                                                  this.markValue(),
 100                                                  this.position(),
 101                                                  this.limit(),
 102                                                  this.capacity(),
 103                                                  offset);
 104 #else[rw]
 105         return duplicate();
 106 #end[rw]
 107     }
 108 
 109 #if[rw]
 110 
 111     protected int ix(int i) {
 112         return (i << $LG_BYTES_PER_VALUE$) + offset;
 113     }
 114 
 115     private long byteOffset(long i) {
 116         return (i << $LG_BYTES_PER_VALUE$) + bb.address + offset;
 117     }
 118 
 119     public $type$ get() {
 120         $memtype$ x = unsafe.get$Memtype$Unaligned(bb.hb, byteOffset(nextGetIndex()),
 121             {#if[boB]?true:false});
 122         return $fromBits$(x);
 123     }
 124 
 125     public $type$ get(int i) {
 126         $memtype$ x = unsafe.get$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)),
 127             {#if[boB]?true:false});
 128         return $fromBits$(x);
 129     }
 130 
 131 #if[streamableType]
 132    $type$ getUnchecked(int i) {
 133         $memtype$ x = unsafe.get$Memtype$Unaligned(bb.hb, byteOffset(i),
 134             {#if[boB]?true:false});
 135         return $fromBits$(x);
 136     }
 137 #end[streamableType]
 138 
 139 #end[rw]
 140 
 141     public $Type$Buffer put($type$ x) {
 142 #if[rw]
 143         $memtype$ y = $toBits$(x);
 144         unsafe.put$Memtype$Unaligned(bb.hb, byteOffset(nextPutIndex()), y,
 145             {#if[boB]?true:false});
 146         return this;
 147 #else[rw]
 148         throw new ReadOnlyBufferException();
 149 #end[rw]
 150     }
 151 
 152     public $Type$Buffer put(int i, $type$ x) {
 153 #if[rw]
 154         $memtype$ y = $toBits$(x);
 155         unsafe.put$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)), y,
 156             {#if[boB]?true:false});
 157         return this;
 158 #else[rw]
 159         throw new ReadOnlyBufferException();
 160 #end[rw]
 161     }
 162 
 163     public $Type$Buffer compact() {
 164 #if[rw]
 165         int pos = position();
 166         int lim = limit();
 167         assert (pos <= lim);
 168         int rem = (pos <= lim ? lim - pos : 0);
 169 
 170         ByteBuffer db = bb.duplicate();
 171         db.limit(ix(lim));
 172         db.position(ix(0));
 173         ByteBuffer sb = db.slice();
 174         sb.position(pos << $LG_BYTES_PER_VALUE$);
 175         sb.compact();
 176         position(rem);


< prev index next >