001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package java.nio;
026:
027: /**
028: * A <code>Buffer</code> storing <code>short</code> data.
029: */
030: class ShortBufferImpl extends ShortBuffer {
031:
032: ShortBufferImpl(ByteBufferImpl parent, int capacity, short[] array,
033: int arrayOffset, boolean isDirect) {
034: this .parent = parent;
035: this .isDirect = isDirect;
036: this .array = array;
037: this .arrayOffset = arrayOffset;
038: this .capacity = this .limit = capacity;
039: this .position = 0;
040: }
041:
042: public short get() {
043: if (position >= limit) {
044: throw new BufferUnderflowException();
045: }
046: return get(position++);
047: }
048:
049: public short get(int index) {
050: if (index < 0 || index >= limit) {
051: throw new IndexOutOfBoundsException();
052: }
053:
054: int bytePtr = arrayOffset + (index << 1);
055: if (isDirect) {
056: return ByteBufferImpl._getShort(bytePtr);
057: } else if (array != null) {
058: return array[arrayOffset + index];
059: } else {
060: return parent.getShort(bytePtr);
061: }
062: }
063:
064: public ShortBuffer put(short s) {
065: if (position >= limit) {
066: throw new BufferOverflowException();
067: }
068: return put(position++, s);
069: }
070:
071: public ShortBuffer put(int index, short s) {
072: if (index < 0 || index >= limit) {
073: throw new IndexOutOfBoundsException();
074: }
075:
076: int bytePtr = arrayOffset + (index << 1);
077: if (isDirect) {
078: ByteBufferImpl._putShort(bytePtr, s);
079: } else if (array != null) {
080: array[arrayOffset + index] = s;
081: } else {
082: parent.putShort(bytePtr, s);
083: }
084: return this ;
085: }
086:
087: public ShortBuffer slice() {
088: int pos = position;
089: if (isDirect) {
090: pos <<= 1;
091: }
092: return new ShortBufferImpl(parent, limit - position, array,
093: arrayOffset + pos, isDirect);
094: }
095:
096: public boolean isDirect() {
097: return isDirect;
098: }
099:
100: public int nativeAddress() {
101: return arrayOffset;
102: }
103:
104: // public String toString() {
105: // return "ShortBufferImpl[" +
106: // "parent=" + parent +
107: // ",array=" + array +
108: // ",arrayOffset=" + arrayOffset +
109: // ",capacity=" + capacity +
110: // ",limit=" + limit +
111: // ",position=" + position +
112: // ",isDirect=" + isDirect +
113: // ",disposed=" + disposed + "]";
114: // }
115:
116: public void dispose() {
117: // Need revisit
118: this .disposed = true;
119: }
120: }
|