source: trunk/gcc/libjava/gnu/java/nio/ByteBufferImpl.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.5 KB
Line 
1/* ByteBufferImpl.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package gnu.java.nio;
39
40import java.nio.ByteBuffer;
41import java.nio.CharBuffer;
42import java.nio.DoubleBuffer;
43import java.nio.FloatBuffer;
44import java.nio.IntBuffer;
45import java.nio.LongBuffer;
46import java.nio.ReadOnlyBufferException;
47import java.nio.ShortBuffer;
48
49/**
50 * This is a Heap memory implementation
51 */
52public final class ByteBufferImpl extends ByteBuffer
53{
54 private boolean readOnly;
55
56 public ByteBufferImpl (int cap, int off, int lim)
57 {
58 super (cap, lim, off, 0);
59 this.backing_buffer = new byte [cap];
60 readOnly = false;
61 }
62
63 public ByteBufferImpl (byte[] array, int offset, int length)
64 {
65 super (array.length, length, offset, 0);
66 this.backing_buffer = array;
67 readOnly = false;
68 }
69
70 public ByteBufferImpl (ByteBufferImpl copy)
71 {
72 super (copy.capacity (), copy.limit (), copy.position (), 0);
73 backing_buffer = copy.backing_buffer;
74 readOnly = copy.isReadOnly ();
75 }
76
77 void inc_pos (int toAdd)
78 {
79 position (position () + toAdd);
80 }
81
82 private static native byte[] nio_cast(byte[]copy);
83 private static native byte[] nio_cast(char[]copy);
84 private static native byte[] nio_cast(short[]copy);
85 private static native byte[] nio_cast(long[]copy);
86 private static native byte[] nio_cast(int[]copy);
87 private static native byte[] nio_cast(float[]copy);
88 private static native byte[] nio_cast(double[]copy);
89
90 ByteBufferImpl (byte[] copy)
91 {
92 super (copy.length, copy.length, 0, 0);
93 this.backing_buffer = copy != null ? nio_cast (copy) : null;
94 readOnly = false;
95 }
96
97 private static native byte nio_get_Byte (ByteBufferImpl b, int index, int limit);
98
99 private static native void nio_put_Byte (ByteBufferImpl b, int index, int limit, byte value);
100
101 public ByteBuffer asByteBuffer ()
102 {
103 ByteBufferImpl res = new ByteBufferImpl (backing_buffer);
104 res.limit ((limit () * 1) / 1);
105 return res;
106 }
107
108 ByteBufferImpl (char[] copy)
109 {
110 super (copy.length * 2, copy.length * 2, 0, 0);
111 this.backing_buffer = copy != null ? nio_cast (copy) : null;
112 readOnly = false;
113 }
114
115 private static native char nio_get_Char (ByteBufferImpl b, int index, int limit);
116
117 private static native void nio_put_Char (ByteBufferImpl b, int index, int limit, char value);
118
119 public CharBuffer asCharBuffer ()
120 {
121 CharBufferImpl res = new CharBufferImpl (backing_buffer);
122 res.limit ((limit () * 2) / 1);
123 return res;
124 }
125
126 ByteBufferImpl (short[] copy)
127 {
128 super (copy.length, copy.length, 0, 0);
129 this.backing_buffer = copy != null ? nio_cast (copy) : null;
130 readOnly = false;
131 }
132
133 private static native short nio_get_Short (ByteBufferImpl b, int index, int limit);
134
135 private static native void nio_put_Short (ByteBufferImpl b, int index, int limit, short value);
136
137 public ShortBuffer asShortBuffer ()
138 {
139 ShortBufferImpl res = new ShortBufferImpl (backing_buffer);
140 res.limit ((limit () * 2) / 1);
141 return res;
142 }
143
144 ByteBufferImpl (int[] copy)
145 {
146 super (copy.length * 4, copy.length * 4, 0, 0);
147 this.backing_buffer = copy != null ? nio_cast(copy) : null;
148 readOnly = false;
149 }
150
151 private static native int nio_get_Int (ByteBufferImpl b, int index, int limit);
152
153 private static native void nio_put_Int (ByteBufferImpl b, int index, int limit, int value);
154
155 public IntBuffer asIntBuffer ()
156 {
157 IntBufferImpl res = new IntBufferImpl (backing_buffer);
158 res.limit ((limit() * 4) / 1);
159 return res;
160 }
161
162 ByteBufferImpl (long[] copy)
163 {
164 super (copy.length * 8, copy.length * 8, 0, 0);
165 this.backing_buffer = copy != null ? nio_cast (copy) : null;
166 readOnly = false;
167 }
168
169 private static native long nio_get_Long (ByteBufferImpl b, int index, int limit);
170
171 private static native void nio_put_Long (ByteBufferImpl b, int index, int limit, long value);
172
173 public LongBuffer asLongBuffer ()
174 {
175 LongBufferImpl res = new LongBufferImpl (backing_buffer);
176 res.limit ((limit() * 8) / 1);
177 return res;
178 }
179
180 ByteBufferImpl (float[] copy)
181 {
182 super (copy.length * 4, copy.length * 4, 0, 0);
183 this.backing_buffer = copy != null ? nio_cast (copy) : null;
184 readOnly = false;
185 }
186
187 private static native float nio_get_Float (ByteBufferImpl b, int index, int limit);
188
189 private static native void nio_put_Float (ByteBufferImpl b, int index, int limit, float value);
190
191 public FloatBuffer asFloatBuffer ()
192 {
193 FloatBufferImpl res = new FloatBufferImpl (backing_buffer);
194 res.limit ((limit() * 4) / 1);
195 return res;
196 }
197
198 ByteBufferImpl (double[] copy)
199 {
200 super (copy.length * 8, copy.length * 8, 0, 0);
201 this.backing_buffer = copy != null ? nio_cast (copy) : null;
202 readOnly = false;
203 }
204
205 private static native double nio_get_Double (ByteBufferImpl b, int index, int limit);
206
207 private static native void nio_put_Double (ByteBufferImpl b, int index, int limit, double value);
208
209 public DoubleBuffer asDoubleBuffer ()
210 {
211 DoubleBufferImpl res = new DoubleBufferImpl (backing_buffer);
212 res.limit ((limit () * 8) / 1);
213 return res;
214 }
215
216 public boolean isReadOnly()
217 {
218 return readOnly;
219 }
220
221 public ByteBuffer slice()
222 {
223 return new ByteBufferImpl(this);
224 }
225
226 public ByteBuffer duplicate()
227 {
228 return new ByteBufferImpl(this);
229 }
230
231 public ByteBuffer asReadOnlyBuffer()
232 {
233 ByteBufferImpl a = new ByteBufferImpl(this);
234 a.readOnly = true;
235 return a;
236 }
237
238 public ByteBuffer compact()
239 {
240 return this;
241 }
242
243 public boolean isDirect()
244 {
245 return false;
246 }
247
248 final public byte get()
249 {
250 byte e = backing_buffer[position()];
251 position(position()+1);
252 return e;
253 }
254
255 final public ByteBuffer put(byte b)
256 {
257 if (readOnly)
258 throw new ReadOnlyBufferException ();
259
260 backing_buffer[position()] = b;
261 position(position()+1);
262 return this;
263 }
264
265 final public byte get(int index)
266 {
267 return backing_buffer[index];
268 }
269
270 final public ByteBuffer put(int index, byte b)
271 {
272 if (readOnly)
273 throw new ReadOnlyBufferException ();
274
275 backing_buffer[index] = b;
276 return this;
277 }
278
279 final public char getChar ()
280 {
281 char a = nio_get_Char (this, position (), limit ());
282 inc_pos (2);
283 return a;
284 }
285
286 final public ByteBuffer putChar (char value)
287 {
288 if (readOnly)
289 throw new ReadOnlyBufferException ();
290
291 nio_put_Char (this, position (), limit (), value);
292 inc_pos (2);
293 return this;
294 }
295
296 final public char getChar (int index)
297 {
298 char a = nio_get_Char (this, index, limit ());
299 return a;
300 }
301
302 final public ByteBuffer putChar (int index, char value)
303 {
304 if (readOnly)
305 throw new ReadOnlyBufferException ();
306
307 nio_put_Char (this, index, limit (), value);
308 return this;
309 }
310
311 final public short getShort ()
312 {
313 short a = nio_get_Short (this, position (), limit ());
314 inc_pos (2);
315 return a;
316 }
317
318 final public ByteBuffer putShort (short value)
319 {
320 if (readOnly)
321 throw new ReadOnlyBufferException ();
322
323 nio_put_Short (this, position (), limit(), value);
324 inc_pos (2);
325 return this;
326 }
327
328 final public short getShort (int index)
329 {
330 short a = nio_get_Short (this, index, limit ());
331 return a;
332 }
333
334 final public ByteBuffer putShort (int index, short value)
335 {
336 if (readOnly)
337 throw new ReadOnlyBufferException ();
338
339 nio_put_Short (this, index, limit (), value);
340 return this;
341 }
342
343 final public int getInt ()
344 {
345 int a = nio_get_Int (this, position (), limit ());
346 inc_pos (4);
347 return a;
348 }
349
350 final public ByteBuffer putInt (int value)
351 {
352 if (readOnly)
353 throw new ReadOnlyBufferException ();
354
355 nio_put_Int (this, position (), limit , value);
356 inc_pos (4);
357 return this;
358 }
359
360 final public int getInt (int index)
361 {
362 int a = nio_get_Int (this, index, limit ());
363 return a;
364 }
365
366 final public ByteBuffer putInt (int index, int value)
367 {
368 if (readOnly)
369 throw new ReadOnlyBufferException ();
370
371 nio_put_Int(this, index, limit (), value);
372 return this;
373 }
374
375 final public long getLong ()
376 {
377 long a = nio_get_Long (this, position (), limit ());
378 inc_pos (8);
379 return a;
380 }
381
382 final public ByteBuffer putLong (long value)
383 {
384 if (readOnly)
385 throw new ReadOnlyBufferException ();
386
387 nio_put_Long (this, position (), limit (), value);
388 inc_pos (8);
389 return this;
390 }
391
392 final public long getLong (int index)
393 {
394 long a = nio_get_Long (this, index, limit ());
395 return a;
396 }
397
398 final public ByteBuffer putLong (int index, long value)
399 {
400 if (readOnly)
401 throw new ReadOnlyBufferException ();
402
403 nio_put_Long (this, index, limit (), value);
404 return this;
405 }
406
407 final public float getFloat ()
408 {
409 float a = nio_get_Float (this, position (), limit ());
410 inc_pos (4);
411 return a;
412 }
413
414 final public ByteBuffer putFloat (float value)
415 {
416 if (readOnly)
417 throw new ReadOnlyBufferException ();
418
419 nio_put_Float (this, position (), limit (), value);
420 inc_pos (4);
421 return this;
422 }
423
424 final public float getFloat (int index)
425 {
426 float a = nio_get_Float (this, index, limit ());
427 return a;
428 }
429
430 final public ByteBuffer putFloat (int index, float value)
431 {
432 if (readOnly)
433 throw new ReadOnlyBufferException ();
434
435 nio_put_Float (this, index, limit(), value);
436 return this;
437 }
438
439 final public double getDouble ()
440 {
441 double a = nio_get_Double (this, position (), limit ());
442 inc_pos (8);
443 return a;
444 }
445
446 final public ByteBuffer putDouble (double value)
447 {
448 if (readOnly)
449 throw new ReadOnlyBufferException ();
450
451 nio_put_Double (this, position(), limit (), value);
452 inc_pos (8);
453 return this;
454 }
455
456 final public double getDouble (int index)
457 {
458 return nio_get_Double (this, index, limit ());
459 }
460
461 final public ByteBuffer putDouble (int index, double value)
462 {
463 if (readOnly)
464 throw new ReadOnlyBufferException ();
465
466 nio_put_Double (this, index, limit (), value);
467 return this;
468 }
469}
Note: See TracBrowser for help on using the repository browser.