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 /** 32 #if[rw] 33 * A read/write Heap$Type$Buffer. 34 #else[rw] 35 * A read-only Heap$Type$Buffer. This class extends the corresponding 36 * read/write class, overriding the mutation methods to throw a {@link 37 * ReadOnlyBufferException} and overriding the view-buffer methods to return an 38 * instance of this class rather than of the superclass. 39 #end[rw] 40 */ 41 42 class Heap$Type$Buffer$RW$ 43 extends {#if[ro]?Heap}$Type$Buffer 44 { 45 46 // For speed these fields are actually declared in X-Buffer; 47 // these declarations are here as documentation 48 /* 49 #if[rw] 50 protected final $type$[] hb; 51 protected final int offset; 52 #end[rw] 53 */ 54 55 Heap$Type$Buffer$RW$(int cap, int lim) { // package-private 56 #if[rw] 57 super(-1, 0, lim, cap, new $type$[cap], 0); 58 /* 59 hb = new $type$[cap]; 60 offset = 0; 61 */ 62 #else[rw] 63 super(cap, lim); 64 this.isReadOnly = true; 65 #end[rw] 66 } 67 68 Heap$Type$Buffer$RW$($type$[] buf, int off, int len) { // package-private 69 #if[rw] 70 super(-1, off, off + len, buf.length, buf, 0); 71 /* 72 hb = buf; 73 offset = 0; 74 */ 114 115 public $Type$Buffer asReadOnlyBuffer() { 116 #if[rw] 117 return new Heap$Type$BufferR(hb, 118 this.markValue(), 119 this.position(), 120 this.limit(), 121 this.capacity(), 122 offset); 123 #else[rw] 124 return duplicate(); 125 #end[rw] 126 } 127 128 #if[rw] 129 130 protected int ix(int i) { 131 return i + offset; 132 } 133 134 public $type$ get() { 135 return hb[ix(nextGetIndex())]; 136 } 137 138 public $type$ get(int i) { 139 return hb[ix(checkIndex(i))]; 140 } 141 142 #if[streamableType] 143 $type$ getUnchecked(int i) { 144 return hb[ix(i)]; 145 } 146 #end[streamableType] 147 148 public $Type$Buffer get($type$[] dst, int offset, int length) { 149 checkBounds(offset, length, dst.length); 150 if (length > remaining()) 151 throw new BufferUnderflowException(); 152 System.arraycopy(hb, ix(position()), dst, offset, length); 153 position(position() + length); 239 240 #if[byte] 241 242 byte _get(int i) { // package-private 243 return hb[i]; 244 } 245 246 void _put(int i, byte b) { // package-private 247 #if[rw] 248 hb[i] = b; 249 #else[rw] 250 throw new ReadOnlyBufferException(); 251 #end[rw] 252 } 253 254 // char 255 256 #if[rw] 257 258 public char getChar() { 259 return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); 260 } 261 262 public char getChar(int i) { 263 return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); 264 } 265 266 #end[rw] 267 268 public $Type$Buffer putChar(char x) { 269 #if[rw] 270 Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); 271 return this; 272 #else[rw] 273 throw new ReadOnlyBufferException(); 274 #end[rw] 275 } 276 277 public $Type$Buffer putChar(int i, char x) { 278 #if[rw] 279 Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); 280 return this; 281 #else[rw] 282 throw new ReadOnlyBufferException(); 283 #end[rw] 284 } 285 286 public CharBuffer asCharBuffer() { 287 int size = this.remaining() >> 1; 288 int off = offset + position(); 289 return (bigEndian 290 ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, 291 -1, 292 0, 293 size, 294 size, 295 off)) 296 : (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this, 297 -1, 298 0, 299 size, 300 size, 301 off))); 302 } 303 304 305 // short 306 307 #if[rw] 308 309 public short getShort() { 310 return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); 311 } 312 313 public short getShort(int i) { 314 return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); 315 } 316 317 #end[rw] 318 319 public $Type$Buffer putShort(short x) { 320 #if[rw] 321 Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); 322 return this; 323 #else[rw] 324 throw new ReadOnlyBufferException(); 325 #end[rw] 326 } 327 328 public $Type$Buffer putShort(int i, short x) { 329 #if[rw] 330 Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); 331 return this; 332 #else[rw] 333 throw new ReadOnlyBufferException(); 334 #end[rw] 335 } 336 337 public ShortBuffer asShortBuffer() { 338 int size = this.remaining() >> 1; 339 int off = offset + position(); 340 return (bigEndian 341 ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, 342 -1, 343 0, 344 size, 345 size, 346 off)) 347 : (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this, 348 -1, 349 0, 350 size, 351 size, 352 off))); 353 } 354 355 356 // int 357 358 #if[rw] 359 360 public int getInt() { 361 return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); 362 } 363 364 public int getInt(int i) { 365 return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); 366 } 367 368 #end[rw] 369 370 public $Type$Buffer putInt(int x) { 371 #if[rw] 372 Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); 373 return this; 374 #else[rw] 375 throw new ReadOnlyBufferException(); 376 #end[rw] 377 } 378 379 public $Type$Buffer putInt(int i, int x) { 380 #if[rw] 381 Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); 382 return this; 383 #else[rw] 384 throw new ReadOnlyBufferException(); 385 #end[rw] 386 } 387 388 public IntBuffer asIntBuffer() { 389 int size = this.remaining() >> 2; 390 int off = offset + position(); 391 return (bigEndian 392 ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, 393 -1, 394 0, 395 size, 396 size, 397 off)) 398 : (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this, 399 -1, 400 0, 401 size, 402 size, 403 off))); 404 } 405 406 407 // long 408 409 #if[rw] 410 411 public long getLong() { 412 return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); 413 } 414 415 public long getLong(int i) { 416 return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); 417 } 418 419 #end[rw] 420 421 public $Type$Buffer putLong(long x) { 422 #if[rw] 423 Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); 424 return this; 425 #else[rw] 426 throw new ReadOnlyBufferException(); 427 #end[rw] 428 } 429 430 public $Type$Buffer putLong(int i, long x) { 431 #if[rw] 432 Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); 433 return this; 434 #else[rw] 435 throw new ReadOnlyBufferException(); 436 #end[rw] 437 } 438 439 public LongBuffer asLongBuffer() { 440 int size = this.remaining() >> 3; 441 int off = offset + position(); 442 return (bigEndian 443 ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, 444 -1, 445 0, 446 size, 447 size, 448 off)) 449 : (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this, 450 -1, 451 0, 452 size, 453 size, 454 off))); 455 } 456 457 458 // float 459 460 #if[rw] 461 462 public float getFloat() { 463 return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); 464 } 465 466 public float getFloat(int i) { 467 return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); 468 } 469 470 #end[rw] 471 472 public $Type$Buffer putFloat(float x) { 473 #if[rw] 474 Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); 475 return this; 476 #else[rw] 477 throw new ReadOnlyBufferException(); 478 #end[rw] 479 } 480 481 public $Type$Buffer putFloat(int i, float x) { 482 #if[rw] 483 Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); 484 return this; 485 #else[rw] 486 throw new ReadOnlyBufferException(); 487 #end[rw] 488 } 489 490 public FloatBuffer asFloatBuffer() { 491 int size = this.remaining() >> 2; 492 int off = offset + position(); 493 return (bigEndian 494 ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, 495 -1, 496 0, 497 size, 498 size, 499 off)) 500 : (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this, 501 -1, 502 0, 503 size, 504 size, 505 off))); 506 } 507 508 509 // double 510 511 #if[rw] 512 513 public double getDouble() { 514 return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); 515 } 516 517 public double getDouble(int i) { 518 return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); 519 } 520 521 #end[rw] 522 523 public $Type$Buffer putDouble(double x) { 524 #if[rw] 525 Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); 526 return this; 527 #else[rw] 528 throw new ReadOnlyBufferException(); 529 #end[rw] 530 } 531 532 public $Type$Buffer putDouble(int i, double x) { 533 #if[rw] 534 Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); 535 return this; 536 #else[rw] 537 throw new ReadOnlyBufferException(); 538 #end[rw] 539 } 540 541 public DoubleBuffer asDoubleBuffer() { 542 int size = this.remaining() >> 3; 543 int off = offset + position(); 544 return (bigEndian 545 ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this, 546 -1, 547 0, 548 size, 549 size, 550 off)) 551 : (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this, 552 -1, 553 0, 554 size, | 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 sun.misc.Unsafe; 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 47 // For speed these fields are actually declared in X-Buffer; 48 // these declarations are here as documentation 49 /* 50 #if[rw] 51 protected final $type$[] hb; 52 protected final int offset; 53 #end[rw] 54 */ 55 56 #if[byte] 57 58 // Cached unsafe-access object 59 private static final Unsafe unsafe = Bits.unsafe(); 60 61 // Cached array base offset 62 private static final long arrayBaseOffset = unsafe.arrayBaseOffset($type$[].class); 63 64 #end[byte] 65 66 Heap$Type$Buffer$RW$(int cap, int lim) { // package-private 67 #if[rw] 68 super(-1, 0, lim, cap, new $type$[cap], 0); 69 /* 70 hb = new $type$[cap]; 71 offset = 0; 72 */ 73 #else[rw] 74 super(cap, lim); 75 this.isReadOnly = true; 76 #end[rw] 77 } 78 79 Heap$Type$Buffer$RW$($type$[] buf, int off, int len) { // package-private 80 #if[rw] 81 super(-1, off, off + len, buf.length, buf, 0); 82 /* 83 hb = buf; 84 offset = 0; 85 */ 125 126 public $Type$Buffer asReadOnlyBuffer() { 127 #if[rw] 128 return new Heap$Type$BufferR(hb, 129 this.markValue(), 130 this.position(), 131 this.limit(), 132 this.capacity(), 133 offset); 134 #else[rw] 135 return duplicate(); 136 #end[rw] 137 } 138 139 #if[rw] 140 141 protected int ix(int i) { 142 return i + offset; 143 } 144 145 #if[byte] 146 private long byteOffset(long i) { 147 return arrayBaseOffset + i + offset; 148 } 149 #end[byte] 150 151 public $type$ get() { 152 return hb[ix(nextGetIndex())]; 153 } 154 155 public $type$ get(int i) { 156 return hb[ix(checkIndex(i))]; 157 } 158 159 #if[streamableType] 160 $type$ getUnchecked(int i) { 161 return hb[ix(i)]; 162 } 163 #end[streamableType] 164 165 public $Type$Buffer get($type$[] dst, int offset, int length) { 166 checkBounds(offset, length, dst.length); 167 if (length > remaining()) 168 throw new BufferUnderflowException(); 169 System.arraycopy(hb, ix(position()), dst, offset, length); 170 position(position() + length); 256 257 #if[byte] 258 259 byte _get(int i) { // package-private 260 return hb[i]; 261 } 262 263 void _put(int i, byte b) { // package-private 264 #if[rw] 265 hb[i] = b; 266 #else[rw] 267 throw new ReadOnlyBufferException(); 268 #end[rw] 269 } 270 271 // char 272 273 #if[rw] 274 275 public char getChar() { 276 return unsafe.getCharUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian); 277 } 278 279 public char getChar(int i) { 280 return unsafe.getCharUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); 281 } 282 283 #end[rw] 284 285 public $Type$Buffer putChar(char x) { 286 #if[rw] 287 unsafe.putCharUnaligned(hb, byteOffset(nextPutIndex(2)), x, bigEndian); 288 return this; 289 #else[rw] 290 throw new ReadOnlyBufferException(); 291 #end[rw] 292 } 293 294 public $Type$Buffer putChar(int i, char x) { 295 #if[rw] 296 unsafe.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian); 297 return this; 298 #else[rw] 299 throw new ReadOnlyBufferException(); 300 #end[rw] 301 } 302 303 public CharBuffer asCharBuffer() { 304 int size = this.remaining() >> 1; 305 int off = offset + position(); 306 return (bigEndian 307 ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, 308 -1, 309 0, 310 size, 311 size, 312 off)) 313 : (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this, 314 -1, 315 0, 316 size, 317 size, 318 off))); 319 } 320 321 322 // short 323 324 #if[rw] 325 326 public short getShort() { 327 return unsafe.getShortUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian); 328 } 329 330 public short getShort(int i) { 331 return unsafe.getShortUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); 332 } 333 334 #end[rw] 335 336 public $Type$Buffer putShort(short x) { 337 #if[rw] 338 unsafe.putShortUnaligned(hb, byteOffset(nextPutIndex(2)), x, bigEndian); 339 return this; 340 #else[rw] 341 throw new ReadOnlyBufferException(); 342 #end[rw] 343 } 344 345 public $Type$Buffer putShort(int i, short x) { 346 #if[rw] 347 unsafe.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian); 348 return this; 349 #else[rw] 350 throw new ReadOnlyBufferException(); 351 #end[rw] 352 } 353 354 public ShortBuffer asShortBuffer() { 355 int size = this.remaining() >> 1; 356 int off = offset + position(); 357 return (bigEndian 358 ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, 359 -1, 360 0, 361 size, 362 size, 363 off)) 364 : (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this, 365 -1, 366 0, 367 size, 368 size, 369 off))); 370 } 371 372 373 // int 374 375 #if[rw] 376 377 public int getInt() { 378 return unsafe.getIntUnaligned(hb, byteOffset(nextGetIndex(4)), bigEndian); 379 } 380 381 public int getInt(int i) { 382 return unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); 383 } 384 385 #end[rw] 386 387 public $Type$Buffer putInt(int x) { 388 #if[rw] 389 unsafe.putIntUnaligned(hb, byteOffset(nextPutIndex(4)), x, bigEndian); 390 return this; 391 #else[rw] 392 throw new ReadOnlyBufferException(); 393 #end[rw] 394 } 395 396 public $Type$Buffer putInt(int i, int x) { 397 #if[rw] 398 unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), x, bigEndian); 399 return this; 400 #else[rw] 401 throw new ReadOnlyBufferException(); 402 #end[rw] 403 } 404 405 public IntBuffer asIntBuffer() { 406 int size = this.remaining() >> 2; 407 int off = offset + position(); 408 return (bigEndian 409 ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, 410 -1, 411 0, 412 size, 413 size, 414 off)) 415 : (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this, 416 -1, 417 0, 418 size, 419 size, 420 off))); 421 } 422 423 424 // long 425 426 #if[rw] 427 428 public long getLong() { 429 return unsafe.getLongUnaligned(hb, byteOffset(nextGetIndex(8)), bigEndian); 430 } 431 432 public long getLong(int i) { 433 return unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); 434 } 435 436 #end[rw] 437 438 public $Type$Buffer putLong(long x) { 439 #if[rw] 440 unsafe.putLongUnaligned(hb, byteOffset(nextPutIndex(8)), x, bigEndian); 441 return this; 442 #else[rw] 443 throw new ReadOnlyBufferException(); 444 #end[rw] 445 } 446 447 public $Type$Buffer putLong(int i, long x) { 448 #if[rw] 449 unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), x, bigEndian); 450 return this; 451 #else[rw] 452 throw new ReadOnlyBufferException(); 453 #end[rw] 454 } 455 456 public LongBuffer asLongBuffer() { 457 int size = this.remaining() >> 3; 458 int off = offset + position(); 459 return (bigEndian 460 ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, 461 -1, 462 0, 463 size, 464 size, 465 off)) 466 : (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this, 467 -1, 468 0, 469 size, 470 size, 471 off))); 472 } 473 474 475 // float 476 477 #if[rw] 478 479 public float getFloat() { 480 return getFloat(nextGetIndex(4)); 481 } 482 483 public float getFloat(int i) { 484 int x = unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); 485 return Float.intBitsToFloat(x); 486 } 487 488 #end[rw] 489 490 public $Type$Buffer putFloat(float x) { 491 #if[rw] 492 putFloat(nextPutIndex(4), x); 493 return this; 494 #else[rw] 495 throw new ReadOnlyBufferException(); 496 #end[rw] 497 } 498 499 public $Type$Buffer putFloat(int i, float x) { 500 #if[rw] 501 int y = Float.floatToRawIntBits(x); 502 unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), y, bigEndian); 503 return this; 504 #else[rw] 505 throw new ReadOnlyBufferException(); 506 #end[rw] 507 } 508 509 public FloatBuffer asFloatBuffer() { 510 int size = this.remaining() >> 2; 511 int off = offset + position(); 512 return (bigEndian 513 ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, 514 -1, 515 0, 516 size, 517 size, 518 off)) 519 : (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this, 520 -1, 521 0, 522 size, 523 size, 524 off))); 525 } 526 527 528 // double 529 530 #if[rw] 531 532 public double getDouble() { 533 return getDouble(nextGetIndex(8)); 534 } 535 536 public double getDouble(int i) { 537 long x = unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); 538 return Double.longBitsToDouble(x); 539 } 540 541 #end[rw] 542 543 public $Type$Buffer putDouble(double x) { 544 #if[rw] 545 putDouble(nextPutIndex(8), x); 546 return this; 547 #else[rw] 548 throw new ReadOnlyBufferException(); 549 #end[rw] 550 } 551 552 public $Type$Buffer putDouble(int i, double x) { 553 #if[rw] 554 long y = Double.doubleToRawLongBits(x); 555 unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 4)), y, bigEndian); 556 return this; 557 #else[rw] 558 throw new ReadOnlyBufferException(); 559 #end[rw] 560 } 561 562 public DoubleBuffer asDoubleBuffer() { 563 int size = this.remaining() >> 3; 564 int off = offset + position(); 565 return (bigEndian 566 ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this, 567 -1, 568 0, 569 size, 570 size, 571 off)) 572 : (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this, 573 -1, 574 0, 575 size, |