1 /*
   2  * Copyright (c) 2000, 2008, 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             if (src == this)
 202                 throw new IllegalArgumentException();
 203             Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
 204             int n = sb.remaining();
 205             if (n > remaining())
 206                 throw new BufferOverflowException();
 207             System.arraycopy(sb.hb, sb.ix(sb.position()),
 208                              hb, ix(position()), n);
 209             sb.position(sb.position() + n);
 210             position(position() + n);
 211         } else if (src.isDirect()) {
 212             int n = src.remaining();
 213             if (n > remaining())
 214                 throw new BufferOverflowException();
 215             src.get(hb, ix(position()), n);
 216             position(position() + n);
 217         } else {
 218             super.put(src);
 219         }
 220         return this;
 221 #else[rw]
 222         throw new ReadOnlyBufferException();
 223 #end[rw]
 224     }
 225 
 226     public $Type$Buffer compact() {
 227 #if[rw]
 228         System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
 229         position(remaining());
 230         limit(capacity());
 231         discardMark();
 232         return this;
 233 #else[rw]
 234         throw new ReadOnlyBufferException();
 235 #end[rw]
 236     }
 237 
 238 
 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,
 555                                                                    size,
 556                                                                    off)));
 557     }
 558 
 559 
 560 #end[byte]
 561 
 562 
 563 #if[char]
 564 
 565     String toString(int start, int end) {               // package-private
 566         try {
 567             return new String(hb, start + offset, end - start);
 568         } catch (StringIndexOutOfBoundsException x) {
 569             throw new IndexOutOfBoundsException();
 570         }
 571     }
 572 
 573 
 574     // --- Methods to support CharSequence ---
 575 
 576     public CharBuffer subSequence(int start, int end) {
 577         if ((start < 0)
 578             || (end > length())
 579             || (start > end))
 580             throw new IndexOutOfBoundsException();
 581         int pos = position();
 582         return new HeapCharBuffer$RW$(hb,
 583                                       -1,
 584                                       pos + start,
 585                                       pos + end,
 586                                       capacity(),
 587                                       offset);
 588     }
 589 
 590 #end[char]
 591 
 592 
 593 #if[!byte]
 594 
 595     public ByteOrder order() {
 596         return ByteOrder.nativeOrder();
 597     }
 598 
 599 #end[!byte]
 600 
 601 }