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: import com.sun.jsr239.GLConfiguration;
028:
029: /**
030: * A <code>Buffer</code> storing <code>byte</code> data.
031: */
032: class ByteBufferImpl extends ByteBuffer {
033:
034: static native int _allocNative(int capacity);
035:
036: static native void _copyBytes(int srcAddress, int dstAddress,
037: int bytes);
038:
039: static native byte _getByte(int address);
040:
041: static native void _getBytes(int address, byte[] dst, int offset,
042: int length);
043:
044: static native void _putByte(int address, byte value);
045:
046: static native void _putBytes(int address, byte[] dst, int offset,
047: int length);
048:
049: static native short _getShort(int address);
050:
051: static native void _getShorts(int address, short[] dst, int offset,
052: int length);
053:
054: static native void _putShort(int address, short value);
055:
056: static native void _putShorts(int address, short[] dst, int offset,
057: int length);
058:
059: static native int _getInt(int address);
060:
061: static native void _getInts(int address, int[] dst, int offset,
062: int length);
063:
064: static native void _putInt(int address, int value);
065:
066: static native void _putInts(int address, int[] dst, int offset,
067: int length);
068:
069: static native float _getFloat(int address);
070:
071: static native void _getFloats(int address, float[] dst, int offset,
072: int length);
073:
074: static native void _putFloat(int address, float value);
075:
076: static native void _putFloats(int address, float[] dst, int offset,
077: int length);
078:
079: native private void finalize();
080:
081: ByteBufferImpl(int capacity, byte[] array, int arrayOffset,
082: ByteBuffer directParent) {
083: this .array = array;
084: this .arrayOffset = arrayOffset;
085:
086: this .capacity = capacity;
087: this .limit = capacity;
088: this .position = 0;
089:
090: this .isDirect = array == null;
091: this .directParent = directParent;
092: }
093:
094: public FloatBuffer asFloatBuffer() {
095: int pos = position + (isDirect ? arrayOffset : 0);
096: return new FloatBufferImpl(this , remaining() >> 2, null, pos,
097: isDirect);
098: }
099:
100: public IntBuffer asIntBuffer() {
101: int pos = position + (isDirect ? arrayOffset : 0);
102: return new IntBufferImpl(this , remaining() >> 2, null, pos,
103: isDirect);
104: }
105:
106: public ShortBuffer asShortBuffer() {
107: int pos = position + (isDirect ? arrayOffset : 0);
108: return new ShortBufferImpl(this , remaining() >> 1, null, pos,
109: isDirect);
110: }
111:
112: public byte get() {
113: if (position >= limit) {
114: throw new BufferUnderflowException();
115: }
116: return get(position++);
117: }
118:
119: public byte get(int index) {
120: if (index < 0 || index >= limit) {
121: throw new IndexOutOfBoundsException();
122: }
123: if (isDirect) {
124: return _getByte(arrayOffset + index);
125: } else {
126: return array[arrayOffset + index];
127: }
128: }
129:
130: public ByteBuffer put(byte b) {
131: if (position >= limit) {
132: throw new BufferOverflowException();
133: }
134: return put(position++, b);
135: }
136:
137: public ByteBuffer put(int index, byte b) {
138: if (index < 0 || index >= limit) {
139: throw new IndexOutOfBoundsException();
140: }
141: if (isDirect) {
142: _putByte(arrayOffset + index, b);
143: } else {
144: array[arrayOffset + index] = b;
145: }
146: return this ;
147: }
148:
149: public float getFloat() {
150: return Float.intBitsToFloat(getInt());
151: }
152:
153: public float getFloat(int index) {
154: return Float.intBitsToFloat(getInt(index));
155: }
156:
157: public int getInt() {
158: if (position >= limit - 3) {
159: throw new BufferUnderflowException();
160: }
161: int x = getInt(position);
162: position += 4;
163: return x;
164: }
165:
166: public int getInt(int index) {
167: if (index < 0 || index >= limit - 3) {
168: throw new IndexOutOfBoundsException();
169: }
170: int x0, x1, x2, x3;
171: if (isDirect) {
172: index += arrayOffset;
173: x0 = _getByte(index++);
174: x1 = _getByte(index++);
175: x2 = _getByte(index++);
176: x3 = _getByte(index);
177: } else {
178: index += arrayOffset;
179: x0 = array[index++];
180: x1 = array[index++];
181: x2 = array[index++];
182: x3 = array[index++];
183: }
184:
185: if (GLConfiguration.IS_BIG_ENDIAN) {
186: return ((x0 << 24) | ((x1 & 0xff) << 16)
187: | ((x2 & 0xff) << 8) | (x3 & 0xff));
188: } else {
189: return ((x3 << 24) | ((x2 & 0xff) << 16)
190: | ((x1 & 0xff) << 8) | (x0 & 0xff));
191: }
192: }
193:
194: public short getShort() {
195: if (position >= limit - 1) {
196: throw new BufferUnderflowException();
197: }
198: short s = getShort(position);
199: position += 2;
200: return s;
201: }
202:
203: public short getShort(int index) {
204: if (index < 0 || index >= limit - 1) {
205: throw new IndexOutOfBoundsException();
206: }
207: int x0, x1;
208: if (isDirect) {
209: index += arrayOffset;
210: x0 = _getByte(index++);
211: x1 = _getByte(index++);
212: } else {
213: index += arrayOffset;
214: x0 = array[index++];
215: x1 = array[index++];
216: }
217:
218: if (GLConfiguration.IS_BIG_ENDIAN) {
219: return (short) (((x0 & 0xff) << 8) | (x1 & 0xff));
220: } else {
221: return (short) (((x1 & 0xff) << 8) | (x0 & 0xff));
222: }
223: }
224:
225: public ByteBuffer putFloat(float value) {
226: return putInt(Float.floatToIntBits(value));
227: }
228:
229: public ByteBuffer putFloat(int index, float value) {
230: return putInt(index, Float.floatToIntBits(value));
231: }
232:
233: public ByteBuffer putInt(int value) {
234: if (position >= limit - 3) {
235: throw new BufferOverflowException();
236: }
237: putInt(position, value);
238: position += 4;
239: return this ;
240: }
241:
242: public ByteBuffer putInt(int index, int value) {
243: if (index < 0 || index >= limit - 3) {
244: throw new IndexOutOfBoundsException();
245: }
246:
247: byte x0, x1, x2, x3;
248: if (GLConfiguration.IS_BIG_ENDIAN) {
249: x0 = (byte) (value >> 24);
250: x1 = (byte) (value >> 16);
251: x2 = (byte) (value >> 8);
252: x3 = (byte) (value);
253: } else {
254: x3 = (byte) (value >> 24);
255: x2 = (byte) (value >> 16);
256: x1 = (byte) (value >> 8);
257: x0 = (byte) (value);
258: }
259:
260: if (isDirect) {
261: index += arrayOffset;
262: _putByte(index++, x0);
263: _putByte(index++, x1);
264: _putByte(index++, x2);
265: _putByte(index, x3);
266: } else {
267: index += arrayOffset;
268: array[index++] = x0;
269: array[index++] = x1;
270: array[index++] = x2;
271: array[index] = x3;
272: }
273:
274: return this ;
275: }
276:
277: public ByteBuffer putShort(short value) {
278: if (position >= limit - 1) {
279: throw new BufferOverflowException();
280: }
281: putShort(position, value);
282: position += 2;
283: return this ;
284: }
285:
286: public ByteBuffer putShort(int index, short value) {
287: if (index < 0 || index >= limit - 1) {
288: throw new IndexOutOfBoundsException();
289: }
290:
291: byte x0, x1;
292: if (GLConfiguration.IS_BIG_ENDIAN) {
293: x0 = (byte) (value >> 8);
294: x1 = (byte) (value);
295: } else {
296: x1 = (byte) (value >> 8);
297: x0 = (byte) (value);
298: }
299:
300: if (isDirect) {
301: index += arrayOffset;
302: _putByte(index++, x0);
303: _putByte(index, x1);
304: } else {
305: index += arrayOffset;
306: array[index++] = x0;
307: array[index] = x1;
308: }
309:
310: return this ;
311: }
312:
313: public ByteBuffer slice() {
314: return new ByteBufferImpl(limit - position, array, arrayOffset
315: + position, this );
316: }
317:
318: public boolean isDirect() {
319: return isDirect;
320: }
321:
322: public int nativeAddress() {
323: return arrayOffset;
324: }
325:
326: public static boolean isBigEndian() {
327: return GLConfiguration.IS_BIG_ENDIAN;
328: }
329:
330: // public String toString() {
331: // return "ByteBufferImpl[" +
332: // "array=" + array +
333: // ",arrayOffset=" + arrayOffset +
334: // ",capacity=" + capacity +
335: // ",limit=" + limit +
336: // ",position=" + position +
337: // ",isBigEndian=" + GLConfiguration.IS_BIG_ENDIAN +
338: // ",isDirect=" + isDirect +
339: // ",disposed=" + disposed + "]";
340: // }
341:
342: public void dispose() {
343: // Need revisit
344: this .disposed = true;
345: }
346: }
|