Heap-X-Buffer.java.template revision 2362
0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#warn This file is preprocessed before being compiled
0N/A
0N/Apackage java.nio;
0N/A
0N/A
0N/A/**
0N/A#if[rw]
0N/A * A read/write Heap$Type$Buffer.
0N/A#else[rw]
0N/A * A read-only Heap$Type$Buffer. This class extends the corresponding
0N/A * read/write class, overriding the mutation methods to throw a {@link
0N/A * ReadOnlyBufferException} and overriding the view-buffer methods to return an
0N/A * instance of this class rather than of the superclass.
0N/A#end[rw]
0N/A */
0N/A
0N/Aclass Heap$Type$Buffer$RW$
0N/A extends {#if[ro]?Heap}$Type$Buffer
0N/A{
0N/A
0N/A // For speed these fields are actually declared in X-Buffer;
0N/A // these declarations are here as documentation
0N/A /*
0N/A#if[rw]
0N/A protected final $type$[] hb;
0N/A protected final int offset;
0N/A#end[rw]
0N/A */
0N/A
0N/A Heap$Type$Buffer$RW$(int cap, int lim) { // package-private
0N/A#if[rw]
0N/A super(-1, 0, lim, cap, new $type$[cap], 0);
0N/A /*
0N/A hb = new $type$[cap];
0N/A offset = 0;
0N/A */
0N/A#else[rw]
0N/A super(cap, lim);
0N/A this.isReadOnly = true;
0N/A#end[rw]
0N/A }
0N/A
0N/A Heap$Type$Buffer$RW$($type$[] buf, int off, int len) { // package-private
0N/A#if[rw]
0N/A super(-1, off, off + len, buf.length, buf, 0);
0N/A /*
0N/A hb = buf;
0N/A offset = 0;
0N/A */
0N/A#else[rw]
0N/A super(buf, off, len);
0N/A this.isReadOnly = true;
0N/A#end[rw]
0N/A }
0N/A
0N/A protected Heap$Type$Buffer$RW$($type$[] buf,
0N/A int mark, int pos, int lim, int cap,
0N/A int off)
0N/A {
0N/A#if[rw]
0N/A super(mark, pos, lim, cap, buf, off);
0N/A /*
0N/A hb = buf;
0N/A offset = off;
0N/A */
0N/A#else[rw]
0N/A super(buf, mark, pos, lim, cap, off);
0N/A this.isReadOnly = true;
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer slice() {
0N/A return new Heap$Type$Buffer$RW$(hb,
0N/A -1,
0N/A 0,
0N/A this.remaining(),
0N/A this.remaining(),
0N/A this.position() + offset);
0N/A }
0N/A
0N/A public $Type$Buffer duplicate() {
0N/A return new Heap$Type$Buffer$RW$(hb,
0N/A this.markValue(),
0N/A this.position(),
0N/A this.limit(),
0N/A this.capacity(),
0N/A offset);
0N/A }
0N/A
0N/A public $Type$Buffer asReadOnlyBuffer() {
0N/A#if[rw]
0N/A return new Heap$Type$BufferR(hb,
0N/A this.markValue(),
0N/A this.position(),
0N/A this.limit(),
0N/A this.capacity(),
0N/A offset);
0N/A#else[rw]
0N/A return duplicate();
0N/A#end[rw]
0N/A }
0N/A
0N/A#if[rw]
0N/A
0N/A protected int ix(int i) {
0N/A return i + offset;
0N/A }
0N/A
0N/A public $type$ get() {
0N/A return hb[ix(nextGetIndex())];
0N/A }
0N/A
0N/A public $type$ get(int i) {
0N/A return hb[ix(checkIndex(i))];
0N/A }
0N/A
0N/A public $Type$Buffer get($type$[] dst, int offset, int length) {
0N/A checkBounds(offset, length, dst.length);
0N/A if (length > remaining())
0N/A throw new BufferUnderflowException();
0N/A System.arraycopy(hb, ix(position()), dst, offset, length);
0N/A position(position() + length);
0N/A return this;
0N/A }
0N/A
0N/A public boolean isDirect() {
0N/A return false;
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public boolean isReadOnly() {
0N/A return {#if[rw]?false:true};
0N/A }
0N/A
0N/A public $Type$Buffer put($type$ x) {
0N/A#if[rw]
0N/A hb[ix(nextPutIndex())] = x;
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer put(int i, $type$ x) {
0N/A#if[rw]
0N/A hb[ix(checkIndex(i))] = x;
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer put($type$[] src, int offset, int length) {
0N/A#if[rw]
0N/A checkBounds(offset, length, src.length);
0N/A if (length > remaining())
0N/A throw new BufferOverflowException();
0N/A System.arraycopy(src, offset, hb, ix(position()), length);
0N/A position(position() + length);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer put($Type$Buffer src) {
0N/A#if[rw]
0N/A if (src instanceof Heap$Type$Buffer) {
0N/A if (src == this)
0N/A throw new IllegalArgumentException();
0N/A Heap$Type$Buffer sb = (Heap$Type$Buffer)src;
0N/A int n = sb.remaining();
0N/A if (n > remaining())
0N/A throw new BufferOverflowException();
0N/A System.arraycopy(sb.hb, sb.ix(sb.position()),
0N/A hb, ix(position()), n);
0N/A sb.position(sb.position() + n);
0N/A position(position() + n);
0N/A } else if (src.isDirect()) {
0N/A int n = src.remaining();
0N/A if (n > remaining())
0N/A throw new BufferOverflowException();
0N/A src.get(hb, ix(position()), n);
0N/A position(position() + n);
0N/A } else {
0N/A super.put(src);
0N/A }
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer compact() {
0N/A#if[rw]
0N/A System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
0N/A position(remaining());
0N/A limit(capacity());
721N/A discardMark();
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A
0N/A
0N/A#if[byte]
0N/A
0N/A byte _get(int i) { // package-private
0N/A return hb[i];
0N/A }
0N/A
0N/A void _put(int i, byte b) { // package-private
0N/A#if[rw]
0N/A hb[i] = b;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A // char
0N/A
0N/A#if[rw]
0N/A
0N/A public char getChar() {
0N/A return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian);
0N/A }
0N/A
0N/A public char getChar(int i) {
0N/A return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian);
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public $Type$Buffer putChar(char x) {
0N/A#if[rw]
0N/A Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer putChar(int i, char x) {
0N/A#if[rw]
0N/A Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public CharBuffer asCharBuffer() {
0N/A int size = this.remaining() >> 1;
0N/A int off = offset + position();
0N/A return (bigEndian
0N/A ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off))
0N/A : (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off)));
0N/A }
0N/A
0N/A
0N/A // short
0N/A
0N/A#if[rw]
0N/A
0N/A public short getShort() {
0N/A return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian);
0N/A }
0N/A
0N/A public short getShort(int i) {
0N/A return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian);
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public $Type$Buffer putShort(short x) {
0N/A#if[rw]
0N/A Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer putShort(int i, short x) {
0N/A#if[rw]
0N/A Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public ShortBuffer asShortBuffer() {
0N/A int size = this.remaining() >> 1;
0N/A int off = offset + position();
0N/A return (bigEndian
0N/A ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off))
0N/A : (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off)));
0N/A }
0N/A
0N/A
0N/A // int
0N/A
0N/A#if[rw]
0N/A
0N/A public int getInt() {
0N/A return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
0N/A }
0N/A
0N/A public int getInt(int i) {
0N/A return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian);
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public $Type$Buffer putInt(int x) {
0N/A#if[rw]
0N/A Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer putInt(int i, int x) {
0N/A#if[rw]
0N/A Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public IntBuffer asIntBuffer() {
0N/A int size = this.remaining() >> 2;
0N/A int off = offset + position();
0N/A return (bigEndian
0N/A ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off))
0N/A : (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off)));
0N/A }
0N/A
0N/A
0N/A // long
0N/A
0N/A#if[rw]
0N/A
0N/A public long getLong() {
0N/A return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian);
0N/A }
0N/A
0N/A public long getLong(int i) {
0N/A return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian);
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public $Type$Buffer putLong(long x) {
0N/A#if[rw]
0N/A Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer putLong(int i, long x) {
0N/A#if[rw]
0N/A Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public LongBuffer asLongBuffer() {
0N/A int size = this.remaining() >> 3;
0N/A int off = offset + position();
0N/A return (bigEndian
0N/A ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off))
0N/A : (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off)));
0N/A }
0N/A
0N/A
0N/A // float
0N/A
0N/A#if[rw]
0N/A
0N/A public float getFloat() {
0N/A return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian);
0N/A }
0N/A
0N/A public float getFloat(int i) {
0N/A return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian);
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public $Type$Buffer putFloat(float x) {
0N/A#if[rw]
0N/A Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer putFloat(int i, float x) {
0N/A#if[rw]
0N/A Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public FloatBuffer asFloatBuffer() {
0N/A int size = this.remaining() >> 2;
0N/A int off = offset + position();
0N/A return (bigEndian
0N/A ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off))
0N/A : (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off)));
0N/A }
0N/A
0N/A
0N/A // double
0N/A
0N/A#if[rw]
0N/A
0N/A public double getDouble() {
0N/A return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian);
0N/A }
0N/A
0N/A public double getDouble(int i) {
0N/A return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian);
0N/A }
0N/A
0N/A#end[rw]
0N/A
0N/A public $Type$Buffer putDouble(double x) {
0N/A#if[rw]
0N/A Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public $Type$Buffer putDouble(int i, double x) {
0N/A#if[rw]
0N/A Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian);
0N/A return this;
0N/A#else[rw]
0N/A throw new ReadOnlyBufferException();
0N/A#end[rw]
0N/A }
0N/A
0N/A public DoubleBuffer asDoubleBuffer() {
0N/A int size = this.remaining() >> 3;
0N/A int off = offset + position();
0N/A return (bigEndian
0N/A ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off))
0N/A : (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this,
0N/A -1,
0N/A 0,
0N/A size,
0N/A size,
0N/A off)));
0N/A }
0N/A
0N/A
0N/A#end[byte]
0N/A
0N/A
0N/A#if[char]
0N/A
0N/A String toString(int start, int end) { // package-private
0N/A try {
0N/A return new String(hb, start + offset, end - start);
0N/A } catch (StringIndexOutOfBoundsException x) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A }
0N/A
0N/A
0N/A // --- Methods to support CharSequence ---
0N/A
571N/A public CharBuffer subSequence(int start, int end) {
0N/A if ((start < 0)
0N/A || (end > length())
0N/A || (start > end))
0N/A throw new IndexOutOfBoundsException();
1108N/A int pos = position();
0N/A return new HeapCharBuffer$RW$(hb,
1108N/A -1,
1108N/A pos + start,
1108N/A pos + end,
1108N/A capacity(),
1108N/A offset);
0N/A }
0N/A
0N/A#end[char]
0N/A
0N/A
0N/A#if[!byte]
0N/A
0N/A public ByteOrder order() {
0N/A return ByteOrder.nativeOrder();
0N/A }
0N/A
0N/A#end[!byte]
0N/A
0N/A}