1 /* 2 * Copyright (c) 2000, 2015, 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 /** 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 */ 75 #else[rw] 76 super(buf, off, len); 77 this.isReadOnly = true; 78 #end[rw] 79 } 80 81 protected Heap$Type$Buffer$RW$($type$[] buf, 82 int mark, int pos, int lim, int cap, 83 int off) 84 { 85 #if[rw] 86 super(mark, pos, lim, cap, buf, off); 87 /* 88 hb = buf; 89 offset = off; 90 */ 91 #else[rw] 92 super(buf, mark, pos, lim, cap, off); 93 this.isReadOnly = true; 94 #end[rw] 95 } 96 97 public $Type$Buffer slice() { 98 return new Heap$Type$Buffer$RW$(hb, 99 -1, 100 0, 101 this.remaining(), 102 this.remaining(), 103 this.position() + offset); 104 } 105 106 public $Type$Buffer duplicate() { 107 return new Heap$Type$Buffer$RW$(hb, 108 this.markValue(), 109 this.position(), 110 this.limit(), 111 this.capacity(), 112 offset); 113 } 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); 154 return this; 155 } 156 157 public boolean isDirect() { 158 return false; 159 } 160 161 #end[rw] 162 163 public boolean isReadOnly() { 164 return {#if[rw]?false:true}; 165 } 166 167 public $Type$Buffer put($type$ x) { 168 #if[rw] 169 hb[ix(nextPutIndex())] = x; 170 return this; 171 #else[rw] 172 throw new ReadOnlyBufferException(); 173 #end[rw] 174 } 175 176 public $Type$Buffer put(int i, $type$ x) { 177 #if[rw] 178 hb[ix(checkIndex(i))] = x; 179 return this; 180 #else[rw] 181 throw new ReadOnlyBufferException(); 182 #end[rw] 183 } 184 185 public $Type$Buffer put($type$[] src, int offset, int length) { 186 #if[rw] 187 checkBounds(offset, length, src.length); 188 if (length > remaining()) 189 throw new BufferOverflowException(); 190 System.arraycopy(src, offset, hb, ix(position()), length); 191 position(position() + length); 192 return this; 193 #else[rw] 194 throw new ReadOnlyBufferException(); 195 #end[rw] 196 } 197 198 public $Type$Buffer put($Type$Buffer src) { 199 #if[rw] 200 if (src instanceof Heap$Type$Buffer) { 201 checkSourceBufferNotThisBuffer(src); 202 Heap$Type$Buffer sb = (Heap$Type$Buffer)src; 203 int n = sb.remaining(); 204 if (n > remaining()) 205 throw new BufferOverflowException(); 206 System.arraycopy(sb.hb, sb.ix(sb.position()), 207 hb, ix(position()), n); 208 sb.position(sb.position() + n); 209 position(position() + n); 210 } else if (src.isDirect()) { 211 int n = src.remaining(); 212 if (n > remaining()) 213 throw new BufferOverflowException(); 214 src.get(hb, ix(position()), n); 215 position(position() + n); 216 } else { 217 super.put(src); 218 } 219 return this; 220 #else[rw] 221 throw new ReadOnlyBufferException(); 222 #end[rw] 223 } 224 225 public $Type$Buffer compact() { 226 #if[rw] 227 System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); 228 position(remaining()); 229 limit(capacity()); 230 discardMark(); 231 return this; 232 #else[rw] 233 throw new ReadOnlyBufferException(); 234 #end[rw] 235 } 236 237 238 239 #if[byte] 240 241 byte _get(int i) { // package-private 242 return hb[i]; 243 } 244 245 void _put(int i, byte b) { // package-private 246 #if[rw] 247 hb[i] = b; 248 #else[rw] 249 throw new ReadOnlyBufferException(); 250 #end[rw] 251 } 252 253 // char 254 255 #if[rw] 256 257 public char getChar() { 258 return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); 259 } 260 261 public char getChar(int i) { 262 return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); 263 } 264 265 #end[rw] 266 267 public $Type$Buffer putChar(char x) { 268 #if[rw] 269 Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); 270 return this; 271 #else[rw] 272 throw new ReadOnlyBufferException(); 273 #end[rw] 274 } 275 276 public $Type$Buffer putChar(int i, char x) { 277 #if[rw] 278 Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); 279 return this; 280 #else[rw] 281 throw new ReadOnlyBufferException(); 282 #end[rw] 283 } 284 285 public CharBuffer asCharBuffer() { 286 int size = this.remaining() >> 1; 287 int off = offset + position(); 288 return (bigEndian 289 ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, 290 -1, 291 0, 292 size, 293 size, 294 off)) 295 : (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this, 296 -1, 297 0, 298 size, 299 size, 300 off))); 301 } 302 303 304 // short 305 306 #if[rw] 307 308 public short getShort() { 309 return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); 310 } 311 312 public short getShort(int i) { 313 return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); 314 } 315 316 #end[rw] 317 318 public $Type$Buffer putShort(short x) { 319 #if[rw] 320 Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); 321 return this; 322 #else[rw] 323 throw new ReadOnlyBufferException(); 324 #end[rw] 325 } 326 327 public $Type$Buffer putShort(int i, short x) { 328 #if[rw] 329 Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); 330 return this; 331 #else[rw] 332 throw new ReadOnlyBufferException(); 333 #end[rw] 334 } 335 336 public ShortBuffer asShortBuffer() { 337 int size = this.remaining() >> 1; 338 int off = offset + position(); 339 return (bigEndian 340 ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, 341 -1, 342 0, 343 size, 344 size, 345 off)) 346 : (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this, 347 -1, 348 0, 349 size, 350 size, 351 off))); 352 } 353 354 355 // int 356 357 #if[rw] 358 359 public int getInt() { 360 return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); 361 } 362 363 public int getInt(int i) { 364 return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); 365 } 366 367 #end[rw] 368 369 public $Type$Buffer putInt(int x) { 370 #if[rw] 371 Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); 372 return this; 373 #else[rw] 374 throw new ReadOnlyBufferException(); 375 #end[rw] 376 } 377 378 public $Type$Buffer putInt(int i, int x) { 379 #if[rw] 380 Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); 381 return this; 382 #else[rw] 383 throw new ReadOnlyBufferException(); 384 #end[rw] 385 } 386 387 public IntBuffer asIntBuffer() { 388 int size = this.remaining() >> 2; 389 int off = offset + position(); 390 return (bigEndian 391 ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, 392 -1, 393 0, 394 size, 395 size, 396 off)) 397 : (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this, 398 -1, 399 0, 400 size, 401 size, 402 off))); 403 } 404 405 406 // long 407 408 #if[rw] 409 410 public long getLong() { 411 return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); 412 } 413 414 public long getLong(int i) { 415 return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); 416 } 417 418 #end[rw] 419 420 public $Type$Buffer putLong(long x) { 421 #if[rw] 422 Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); 423 return this; 424 #else[rw] 425 throw new ReadOnlyBufferException(); 426 #end[rw] 427 } 428 429 public $Type$Buffer putLong(int i, long x) { 430 #if[rw] 431 Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); 432 return this; 433 #else[rw] 434 throw new ReadOnlyBufferException(); 435 #end[rw] 436 } 437 438 public LongBuffer asLongBuffer() { 439 int size = this.remaining() >> 3; 440 int off = offset + position(); 441 return (bigEndian 442 ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, 443 -1, 444 0, 445 size, 446 size, 447 off)) 448 : (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this, 449 -1, 450 0, 451 size, 452 size, 453 off))); 454 } 455 456 457 // float 458 459 #if[rw] 460 461 public float getFloat() { 462 return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); 463 } 464 465 public float getFloat(int i) { 466 return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); 467 } 468 469 #end[rw] 470 471 public $Type$Buffer putFloat(float x) { 472 #if[rw] 473 Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); 474 return this; 475 #else[rw] 476 throw new ReadOnlyBufferException(); 477 #end[rw] 478 } 479 480 public $Type$Buffer putFloat(int i, float x) { 481 #if[rw] 482 Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); 483 return this; 484 #else[rw] 485 throw new ReadOnlyBufferException(); 486 #end[rw] 487 } 488 489 public FloatBuffer asFloatBuffer() { 490 int size = this.remaining() >> 2; 491 int off = offset + position(); 492 return (bigEndian 493 ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, 494 -1, 495 0, 496 size, 497 size, 498 off)) 499 : (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this, 500 -1, 501 0, 502 size, 503 size, 504 off))); 505 } 506 507 508 // double 509 510 #if[rw] 511 512 public double getDouble() { 513 return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); 514 } 515 516 public double getDouble(int i) { 517 return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); 518 } 519 520 #end[rw] 521 522 public $Type$Buffer putDouble(double x) { 523 #if[rw] 524 Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); 525 return this; 526 #else[rw] 527 throw new ReadOnlyBufferException(); 528 #end[rw] 529 } 530 531 public $Type$Buffer putDouble(int i, double x) { 532 #if[rw] 533 Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); 534 return this; 535 #else[rw] 536 throw new ReadOnlyBufferException(); 537 #end[rw] 538 } 539 540 public DoubleBuffer asDoubleBuffer() { 541 int size = this.remaining() >> 3; 542 int off = offset + position(); 543 return (bigEndian 544 ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this, 545 -1, 546 0, 547 size, 548 size, 549 off)) 550 : (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this, 551 -1, 552 0, 553 size, 554 size, 555 off))); 556 } 557 558 559 #end[byte] 560 561 562 #if[char] 563 564 String toString(int start, int end) { // package-private 565 try { 566 return new String(hb, start + offset, end - start); 567 } catch (StringIndexOutOfBoundsException x) { 568 throw new IndexOutOfBoundsException(); 569 } 570 } 571 572 573 // --- Methods to support CharSequence --- 574 575 public CharBuffer subSequence(int start, int end) { 576 if ((start < 0) 577 || (end > length()) 578 || (start > end)) 579 throw new IndexOutOfBoundsException(); 580 int pos = position(); 581 return new HeapCharBuffer$RW$(hb, 582 -1, 583 pos + start, 584 pos + end, 585 capacity(), 586 offset); 587 } 588 589 #end[char] 590 591 592 #if[!byte] 593 594 public ByteOrder order() { 595 return ByteOrder.nativeOrder(); 596 } 597 598 #end[!byte] 599 600 }