KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > j2me > nio > ByteBuffer


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package j2me.nio;
10
11 /**
12  * Clean-room implementation of ByteBuffer to support
13  * <code>javolution.util.Struct</code> when <code>java.nio</code> is
14  * not available.
15  */

16 public final class ByteBuffer extends Buffer {
17
18     private ByteOrder _order = ByteOrder.BIG_ENDIAN;
19
20     private final byte[] _bytes;
21
22     private ByteBuffer(byte[] bytes) {
23         super(bytes.length, bytes.length, 0, -1);
24         _bytes = bytes;
25     }
26
27     public static ByteBuffer allocateDirect(int capacity) {
28         return new ByteBuffer(new byte[capacity]);
29     }
30
31     public static ByteBuffer allocate(int capacity) {
32         return new ByteBuffer(new byte[capacity]);
33     }
34
35     final public static ByteBuffer wrap(byte[] array) {
36         return new ByteBuffer(array);
37     }
38
39     public ByteBuffer get(byte[] dst, int offset, int length) {
40         for (int i = offset; i < offset + length; i++) {
41             dst[i] = get();
42         }
43
44         return this;
45     }
46
47     public ByteBuffer get(byte[] dst) {
48         return get(dst, 0, dst.length);
49     }
50
51     public ByteBuffer put(ByteBuffer src) {
52         if (src.remaining() > 0) {
53             byte[] toPut = new byte[src.remaining()];
54             src.get(toPut);
55             src.put(toPut);
56         }
57
58         return this;
59     }
60
61     public ByteBuffer put(byte[] src, int offset, int length) {
62         for (int i = offset; i < offset + length; i++)
63             put(src[i]);
64
65         return this;
66     }
67
68     public final ByteBuffer put(byte[] src) {
69         return put(src, 0, src.length);
70     }
71
72     public final boolean hasArray() {
73         return true;
74     }
75
76     public final byte[] array() {
77         return _bytes;
78     }
79
80     public final int arrayOffset() {
81         return 0;
82     }
83
84     public final ByteOrder order() {
85         return _order;
86     }
87
88     public final ByteBuffer order(ByteOrder endian) {
89         _order = endian;
90         return this;
91     }
92
93     public byte get() {
94         return _bytes[_position++];
95     }
96
97     public ByteBuffer put(byte b) {
98         _bytes[_position++] = b;
99         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) + ((_bytes[++index] & 0xff) << 8)
170                     + ((_bytes[++index] & 0xff) << 16)
171                     + ((_bytes[++index] & 0xff) << 24);
172         } else {
173             return (_bytes[index] << 24) + ((_bytes[++index] & 0xff) << 16)
174                     + ((_bytes[++index] & 0xff) << 8)
175                     + (_bytes[++index] & 0xff);
176         }
177     }
178
179     public ByteBuffer putInt(int index, int value) {
180         if (_order == ByteOrder.LITTLE_ENDIAN) {
181             _bytes[index] = (byte) value;
182             _bytes[++index] = (byte) (value >> 8);
183             _bytes[++index] = (byte) (value >> 16);
184             _bytes[++index] = (byte) (value >> 24);
185         } else {
186             _bytes[index] = (byte) (value >> 24);
187             _bytes[++index] = (byte) (value >> 16);
188             _bytes[++index] = (byte) (value >> 8);
189             _bytes[++index] = (byte) value;
190         }
191         return this;
192     }
193
194     public long getLong() {
195         return getLong(_position++);
196     }
197
198     public ByteBuffer putLong(long value) {
199         return putLong(_position++, value);
200     }
201
202     public long getLong(int index) {
203         if (_order == ByteOrder.LITTLE_ENDIAN) {
204             return (_bytes[index] & 0xff) + ((_bytes[++index] & 0xff) << 8)
205                     + ((_bytes[++index] & 0xff) << 16)
206                     + ((_bytes[++index] & 0xffL) << 24)
207                     + ((_bytes[++index] & 0xffL) << 32)
208                     + ((_bytes[++index] & 0xffL) << 40)
209                     + ((_bytes[++index] & 0xffL) << 48)
210                     + (((long) _bytes[++index]) << 56);
211         } else {
212             return (((long) _bytes[index]) << 56)
213                     + ((_bytes[++index] & 0xffL) << 48)
214                     + ((_bytes[++index] & 0xffL) << 40)
215                     + ((_bytes[++index] & 0xffL) << 32)
216                     + ((_bytes[++index] & 0xffL) << 24)
217                     + ((_bytes[++index] & 0xff) << 16)
218                     + ((_bytes[++index] & 0xff) << 8)
219                     + (_bytes[++index] & 0xffL);
220         }
221     }
222
223     public ByteBuffer putLong(int index, long value) {
224         if (_order == ByteOrder.LITTLE_ENDIAN) {
225             _bytes[index] = (byte) value;
226             _bytes[++index] = (byte) (value >> 8);
227             _bytes[++index] = (byte) (value >> 16);
228             _bytes[++index] = (byte) (value >> 24);
229             _bytes[++index] = (byte) (value >> 32);
230             _bytes[++index] = (byte) (value >> 40);
231             _bytes[++index] = (byte) (value >> 48);
232             _bytes[++index] = (byte) (value >> 56);
233         } else {
234             _bytes[index] = (byte) (value >> 56);
235             _bytes[++index] = (byte) (value >> 48);
236             _bytes[++index] = (byte) (value >> 40);
237             _bytes[++index] = (byte) (value >> 32);
238             _bytes[++index] = (byte) (value >> 24);
239             _bytes[++index] = (byte) (value >> 16);
240             _bytes[++index] = (byte) (value >> 8);
241             _bytes[++index] = (byte) value;
242         }
243         return this;
244     }
245
246     /*@JVM-1.1+@
247
248     public float getFloat() {
249         return getFloat(_position++);
250     }
251
252     public ByteBuffer putFloat(float value) {
253         return putFloat(_position++, value);
254     }
255
256     public float getFloat(int index) {
257         return Float.intBitsToFloat(getInt(index));
258     }
259
260     public ByteBuffer putFloat(int index, float value) {
261         return putInt(index, Float.floatToIntBits(value));
262     }
263
264     public double getDouble() {
265         return getDouble(_position++);
266     }
267
268     public ByteBuffer putDouble(double value) {
269         return putDouble(_position++, value);
270     }
271
272     public double getDouble(int index) {
273         return Double.longBitsToDouble(getLong(index));
274     }
275
276     public ByteBuffer putDouble(int index, double value) {
277         return putLong(index, Double.doubleToLongBits(value));
278     }
279     
280     /**/

281 }
Popular Tags