001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2005 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package j2me.nio;
010:
011: /**
012: * Clean-room implementation of ByteBuffer to support
013: * <code>javolution.util.Struct</code> when <code>java.nio</code> is
014: * not available.
015: */
016: public final class ByteBuffer extends Buffer {
017:
018: private ByteOrder _order = ByteOrder.BIG_ENDIAN;
019:
020: private final byte[] _bytes;
021:
022: private ByteBuffer(byte[] bytes) {
023: super (bytes.length, bytes.length, 0, -1);
024: _bytes = bytes;
025: }
026:
027: public static ByteBuffer allocateDirect(int capacity) {
028: return new ByteBuffer(new byte[capacity]);
029: }
030:
031: public static ByteBuffer allocate(int capacity) {
032: return new ByteBuffer(new byte[capacity]);
033: }
034:
035: final public static ByteBuffer wrap(byte[] array) {
036: return new ByteBuffer(array);
037: }
038:
039: public ByteBuffer get(byte[] dst, int offset, int length) {
040: for (int i = offset; i < offset + length; i++) {
041: dst[i] = get();
042: }
043:
044: return this ;
045: }
046:
047: public ByteBuffer get(byte[] dst) {
048: return get(dst, 0, dst.length);
049: }
050:
051: public ByteBuffer put(ByteBuffer src) {
052: if (src.remaining() > 0) {
053: byte[] toPut = new byte[src.remaining()];
054: src.get(toPut);
055: src.put(toPut);
056: }
057:
058: return this ;
059: }
060:
061: public ByteBuffer put(byte[] src, int offset, int length) {
062: for (int i = offset; i < offset + length; i++)
063: put(src[i]);
064:
065: return this ;
066: }
067:
068: public final ByteBuffer put(byte[] src) {
069: return put(src, 0, src.length);
070: }
071:
072: public final boolean hasArray() {
073: return true;
074: }
075:
076: public final byte[] array() {
077: return _bytes;
078: }
079:
080: public final int arrayOffset() {
081: return 0;
082: }
083:
084: public final ByteOrder order() {
085: return _order;
086: }
087:
088: public final ByteBuffer order(ByteOrder endian) {
089: _order = endian;
090: return this ;
091: }
092:
093: public byte get() {
094: return _bytes[_position++];
095: }
096:
097: public ByteBuffer put(byte b) {
098: _bytes[_position++] = b;
099: return this ;
100: }
101:
102: public byte get(int index) {
103: return _bytes[index];
104: }
105:
106: public ByteBuffer put(int index, byte b) {
107: _bytes[index] = b;
108: return this ;
109: }
110:
111: public boolean isDirect() {
112: return false;
113: }
114:
115: public char getChar() {
116: return getChar(_position++);
117: }
118:
119: public ByteBuffer putChar(char value) {
120: putChar(_position++, value);
121: return this ;
122: }
123:
124: public char getChar(int index) {
125: return (char) getShort(index);
126: }
127:
128: public ByteBuffer putChar(int index, char value) {
129: return putShort(index, (short) value);
130: }
131:
132: public short getShort() {
133: return getShort(_position++);
134: }
135:
136: public ByteBuffer putShort(short value) {
137: return putShort(_position++, value);
138: }
139:
140: public short getShort(int index) {
141: if (_order == ByteOrder.LITTLE_ENDIAN) {
142: return (short) ((_bytes[index] & 0xff) + (_bytes[++index] << 8));
143: } else {
144: return (short) ((_bytes[index] << 8) + (_bytes[++index] & 0xff));
145: }
146: }
147:
148: public ByteBuffer putShort(int index, short value) {
149: if (_order == ByteOrder.LITTLE_ENDIAN) {
150: _bytes[index] = (byte) value;
151: _bytes[++index] = (byte) (value >> 8);
152: } else {
153: _bytes[index] = (byte) (value >> 8);
154: _bytes[++index] = (byte) value;
155: }
156: return this ;
157: }
158:
159: public int getInt() {
160: return getInt(_position++);
161: }
162:
163: public ByteBuffer putInt(int value) {
164: return putInt(_position++, value);
165: }
166:
167: public int getInt(int index) {
168: if (_order == ByteOrder.LITTLE_ENDIAN) {
169: return (_bytes[index] & 0xff)
170: + ((_bytes[++index] & 0xff) << 8)
171: + ((_bytes[++index] & 0xff) << 16)
172: + ((_bytes[++index] & 0xff) << 24);
173: } else {
174: return (_bytes[index] << 24)
175: + ((_bytes[++index] & 0xff) << 16)
176: + ((_bytes[++index] & 0xff) << 8)
177: + (_bytes[++index] & 0xff);
178: }
179: }
180:
181: public ByteBuffer putInt(int index, int value) {
182: if (_order == ByteOrder.LITTLE_ENDIAN) {
183: _bytes[index] = (byte) value;
184: _bytes[++index] = (byte) (value >> 8);
185: _bytes[++index] = (byte) (value >> 16);
186: _bytes[++index] = (byte) (value >> 24);
187: } else {
188: _bytes[index] = (byte) (value >> 24);
189: _bytes[++index] = (byte) (value >> 16);
190: _bytes[++index] = (byte) (value >> 8);
191: _bytes[++index] = (byte) value;
192: }
193: return this ;
194: }
195:
196: public long getLong() {
197: return getLong(_position++);
198: }
199:
200: public ByteBuffer putLong(long value) {
201: return putLong(_position++, value);
202: }
203:
204: public long getLong(int index) {
205: if (_order == ByteOrder.LITTLE_ENDIAN) {
206: return (_bytes[index] & 0xff)
207: + ((_bytes[++index] & 0xff) << 8)
208: + ((_bytes[++index] & 0xff) << 16)
209: + ((_bytes[++index] & 0xffL) << 24)
210: + ((_bytes[++index] & 0xffL) << 32)
211: + ((_bytes[++index] & 0xffL) << 40)
212: + ((_bytes[++index] & 0xffL) << 48)
213: + (((long) _bytes[++index]) << 56);
214: } else {
215: return (((long) _bytes[index]) << 56)
216: + ((_bytes[++index] & 0xffL) << 48)
217: + ((_bytes[++index] & 0xffL) << 40)
218: + ((_bytes[++index] & 0xffL) << 32)
219: + ((_bytes[++index] & 0xffL) << 24)
220: + ((_bytes[++index] & 0xff) << 16)
221: + ((_bytes[++index] & 0xff) << 8)
222: + (_bytes[++index] & 0xffL);
223: }
224: }
225:
226: public ByteBuffer putLong(int index, long value) {
227: if (_order == ByteOrder.LITTLE_ENDIAN) {
228: _bytes[index] = (byte) value;
229: _bytes[++index] = (byte) (value >> 8);
230: _bytes[++index] = (byte) (value >> 16);
231: _bytes[++index] = (byte) (value >> 24);
232: _bytes[++index] = (byte) (value >> 32);
233: _bytes[++index] = (byte) (value >> 40);
234: _bytes[++index] = (byte) (value >> 48);
235: _bytes[++index] = (byte) (value >> 56);
236: } else {
237: _bytes[index] = (byte) (value >> 56);
238: _bytes[++index] = (byte) (value >> 48);
239: _bytes[++index] = (byte) (value >> 40);
240: _bytes[++index] = (byte) (value >> 32);
241: _bytes[++index] = (byte) (value >> 24);
242: _bytes[++index] = (byte) (value >> 16);
243: _bytes[++index] = (byte) (value >> 8);
244: _bytes[++index] = (byte) value;
245: }
246: return this ;
247: }
248:
249: /*@JVM-1.1+@
250:
251: public float getFloat() {
252: return getFloat(_position++);
253: }
254:
255: public ByteBuffer putFloat(float value) {
256: return putFloat(_position++, value);
257: }
258:
259: public float getFloat(int index) {
260: return Float.intBitsToFloat(getInt(index));
261: }
262:
263: public ByteBuffer putFloat(int index, float value) {
264: return putInt(index, Float.floatToIntBits(value));
265: }
266:
267: public double getDouble() {
268: return getDouble(_position++);
269: }
270:
271: public ByteBuffer putDouble(double value) {
272: return putDouble(_position++, value);
273: }
274:
275: public double getDouble(int index) {
276: return Double.longBitsToDouble(getLong(index));
277: }
278:
279: public ByteBuffer putDouble(int index, double value) {
280: return putLong(index, Double.doubleToLongBits(value));
281: }
282:
283: /**/
284: }
|