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] 95 /* 96 hb = buf; 97 offset = off; 98 */ 99 this.address = ARRAY_BASE_OFFSET + off * ARRAY_INDEX_SCALE; 100 #else[rw] 101 super(buf, mark, pos, lim, cap, off); 102 this.isReadOnly = true; 103 #end[rw] 104 } 105 106 public $Type$Buffer slice() { 107 return new Heap$Type$Buffer$RW$(hb, 108 -1, 109 0, 110 this.remaining(), 111 this.remaining(), 112 this.position() + offset); 113 } 114 115 #if[byte] 116 $Type$Buffer slice(int pos, int lim) { 117 assert (pos >= 0); 118 assert (pos <= lim); 119 int rem = lim - pos; 120 return new Heap$Type$Buffer$RW$(hb, 121 -1, 122 0, 123 rem, 124 rem, 125 pos + offset); 126 } 127 #end[byte] 128 129 public $Type$Buffer duplicate() { 130 return new Heap$Type$Buffer$RW$(hb, 131 this.markValue(), 132 this.position(), 133 this.limit(), 134 this.capacity(), 135 offset); 136 } 137 138 public $Type$Buffer asReadOnlyBuffer() { 139 #if[rw] 140 return new Heap$Type$BufferR(hb, 141 this.markValue(), 142 this.position(), 143 this.limit(), 144 this.capacity(), 145 offset); 146 #else[rw] 147 return duplicate(); | 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 java.util.Objects; 31 32 /** 33 #if[rw] 34 * A read/write Heap$Type$Buffer. 35 #else[rw] 36 * A read-only Heap$Type$Buffer. This class extends the corresponding 37 * read/write class, overriding the mutation methods to throw a {@link 38 * ReadOnlyBufferException} and overriding the view-buffer methods to return an 39 * instance of this class rather than of the superclass. 40 #end[rw] 41 */ 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] 95 /* 96 hb = buf; 97 offset = off; 98 */ 99 this.address = ARRAY_BASE_OFFSET + off * ARRAY_INDEX_SCALE; 100 #else[rw] 101 super(buf, mark, pos, lim, cap, off); 102 this.isReadOnly = true; 103 #end[rw] 104 } 105 106 public $Type$Buffer slice() { 107 return new Heap$Type$Buffer$RW$(hb, 108 -1, 109 0, 110 this.remaining(), 111 this.remaining(), 112 this.position() + offset); 113 } 114 115 @Override 116 public $Type$Buffer slice(int index, int length) { 117 Objects.checkFromIndexSize(index, length, limit()); 118 return new Heap$Type$Buffer$RW$(hb, 119 -1, 120 0, 121 length, 122 length, 123 index + offset); 124 } 125 126 public $Type$Buffer duplicate() { 127 return new Heap$Type$Buffer$RW$(hb, 128 this.markValue(), 129 this.position(), 130 this.limit(), 131 this.capacity(), 132 offset); 133 } 134 135 public $Type$Buffer asReadOnlyBuffer() { 136 #if[rw] 137 return new Heap$Type$BufferR(hb, 138 this.markValue(), 139 this.position(), 140 this.limit(), 141 this.capacity(), 142 offset); 143 #else[rw] 144 return duplicate(); |