]>
Commit | Line | Data |
---|---|---|
7f73f46f MK |
1 | /* ByteBuffer.java -- |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU Classpath. | |
5 | ||
6 | GNU Classpath is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Classpath is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Classpath; see the file COPYING. If not, write to the | |
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. | |
20 | ||
21 | Linking this library statically or dynamically with other modules is | |
22 | making a combined work based on this library. Thus, the terms and | |
23 | conditions of the GNU General Public License cover the whole | |
24 | combination. | |
25 | ||
26 | As a special exception, the copyright holders of this library give you | |
27 | permission to link this library with independent modules to produce an | |
28 | executable, regardless of the license terms of these independent | |
29 | modules, and to copy and distribute the resulting executable under | |
30 | terms of your choice, provided that you also meet, for each linked | |
31 | independent module, the terms and conditions of the license of that | |
32 | module. An independent module is a module which is not derived from | |
33 | or based on this library. If you modify this library, you may extend | |
34 | this exception to your version of the library, but you are not | |
35 | obligated to do so. If you do not wish to do so, delete this | |
36 | exception statement from your version. */ | |
37 | ||
38 | package java.nio; | |
39 | ||
ad2e63d5 MK |
40 | import gnu.java.nio.ByteBufferImpl; |
41 | ||
d19e783c MK |
42 | /** |
43 | * @since 1.4 | |
44 | */ | |
45 | public abstract class ByteBuffer extends Buffer implements Comparable | |
7f73f46f | 46 | { |
8c9c9dfb MK |
47 | private ByteOrder endian = ByteOrder.BIG_ENDIAN; |
48 | ||
d19e783c | 49 | int offset; |
d19e783c MK |
50 | byte[] backing_buffer; |
51 | ||
52 | /** | |
53 | * Allocates a new direct byte buffer. | |
54 | */ | |
55 | public static ByteBuffer allocateDirect (int capacity) | |
56 | { | |
57 | throw new Error ("direct buffers are not implemented"); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Allocates a new byte buffer. | |
62 | */ | |
61d31826 MK |
63 | public static ByteBuffer allocate (int capacity) |
64 | { | |
ad2e63d5 | 65 | return new ByteBufferImpl (capacity, 0, capacity); |
61d31826 MK |
66 | } |
67 | ||
d19e783c MK |
68 | /** |
69 | * Wraps a byte array into a buffer. | |
70 | * | |
71 | * @exception IndexOutOfBoundsException If the preconditions on the offset | |
72 | * and length parameters do not hold | |
73 | */ | |
61d31826 MK |
74 | final public static ByteBuffer wrap (byte[] array, int offset, int length) |
75 | { | |
ad2e63d5 | 76 | return new ByteBufferImpl (array, offset, length); |
61d31826 MK |
77 | } |
78 | ||
d19e783c MK |
79 | /** |
80 | * Wraps a byte array into a buffer. | |
81 | */ | |
61d31826 MK |
82 | final public static ByteBuffer wrap (byte[] array) |
83 | { | |
84 | return wrap (array, 0, array.length); | |
85 | } | |
d19e783c MK |
86 | |
87 | ByteBuffer (int capacity, int limit, int position, int mark) | |
88 | { | |
89 | super (capacity, limit, position, mark); | |
90 | } | |
91 | ||
8c9c9dfb MK |
92 | /** |
93 | * This method transfers bytes from this buffer into | |
94 | * the given destination array. | |
95 | * | |
96 | * @param dst The destination array | |
97 | * @param offset The offset within the array of the first byte to be written; | |
98 | * must be non-negative and no larger than dst.length. | |
99 | * @param length The maximum number of bytes to be written to the given array; | |
100 | * must be non-negative and no larger than dst.length - offset. | |
101 | * | |
102 | * @exception BufferUnderflowException If there are fewer than length bytes | |
103 | * remaining in this buffer. | |
104 | * @exception IndexOutOfBoundsException - If the preconditions on the offset | |
105 | * and length parameters do not hold. | |
106 | */ | |
107 | public ByteBuffer get (byte[] dst, int offset, int length) | |
108 | { | |
109 | if ((offset < 0) | |
110 | || (offset > dst.length) | |
111 | || (length < 0) | |
112 | || (length > (dst.length - offset))) | |
113 | throw new IndexOutOfBoundsException (); | |
114 | ||
115 | for (int i = offset; i < offset + length; i++) | |
116 | { | |
117 | dst [i] = get(); | |
118 | } | |
119 | ||
120 | return this; | |
121 | } | |
122 | ||
123 | /** | |
124 | * This method transfers bytes from this buffer into the given | |
125 | * destination array. | |
126 | * | |
127 | * @param dst The byte array to write into. | |
128 | * | |
129 | * @exception BufferUnderflowException If there are fewer than dst.length | |
130 | * bytes remaining in this buffer. | |
131 | */ | |
132 | public ByteBuffer get (byte[] dst) | |
133 | { | |
134 | return get (dst, 0, dst.length); | |
135 | } | |
136 | ||
d19e783c MK |
137 | /** |
138 | * Writes the content of src into the buffer. | |
139 | * | |
140 | * @param src The source data. | |
141 | * | |
142 | * @exception BufferOverflowException If there is insufficient space in this | |
143 | * buffer for the remaining bytes in the source buffer. | |
144 | * @exception IllegalArgumentException If the source buffer is this buffer. | |
145 | * @exception ReadOnlyBufferException If this buffer is read only. | |
146 | */ | |
147 | public ByteBuffer put (ByteBuffer src) | |
61d31826 | 148 | { |
d19e783c MK |
149 | if (src == this) |
150 | throw new IllegalArgumentException (); | |
151 | ||
61d31826 MK |
152 | while (src.hasRemaining ()) |
153 | put (src.get ()); | |
154 | ||
155 | return this; | |
156 | } | |
d19e783c MK |
157 | |
158 | /** | |
159 | * Writes the content of the the array src into the buffer. | |
160 | * | |
161 | * @param src The array to copy into the buffer. | |
162 | * @param offset The offset within the array of the first byte to be read; | |
163 | * must be non-negative and no larger than src.length. | |
164 | * @param length The number of bytes to be read from the given array; | |
165 | * must be non-negative and no larger than src.length - offset. | |
166 | * | |
167 | * @exception BufferOverflowException If there is insufficient space in this | |
168 | * buffer for the remaining bytes in the source buffer. | |
169 | * @exception IndexOutOfBoundsException If the preconditions on the offset | |
170 | * and length parameters do not hold. | |
171 | * @exception ReadOnlyBufferException If this buffer is read only. | |
172 | */ | |
173 | public ByteBuffer put (byte[] src, int offset, int length) | |
61d31826 | 174 | { |
d19e783c MK |
175 | if ((offset < 0) || |
176 | (offset > src.length) || | |
177 | (length < 0) || | |
178 | (length > src.length - offset)) | |
179 | throw new IndexOutOfBoundsException (); | |
180 | ||
61d31826 MK |
181 | for (int i = offset; i < offset + length; i++) |
182 | put (src [i]); | |
d19e783c | 183 | |
61d31826 MK |
184 | return this; |
185 | } | |
d19e783c MK |
186 | |
187 | /** | |
188 | * Writes the content of the the array src into the buffer. | |
189 | * | |
190 | * @param src The array to copy into the buffer. | |
191 | * | |
192 | * @exception BufferOverflowException If there is insufficient space in this | |
193 | * buffer for the remaining bytes in the source buffer. | |
194 | * @exception ReadOnlyBufferException If this buffer is read only. | |
195 | */ | |
61d31826 MK |
196 | public final ByteBuffer put (byte[] src) |
197 | { | |
198 | return put (src, 0, src.length); | |
199 | } | |
200 | ||
d19e783c | 201 | /** |
f1b62339 | 202 | * Tells whether or not this buffer is backed by an accessible array. |
d19e783c MK |
203 | */ |
204 | public final boolean hasArray () | |
205 | { | |
206 | return (backing_buffer != null | |
194ea9ce | 207 | && !isReadOnly ()); |
d19e783c MK |
208 | } |
209 | ||
210 | /** | |
211 | * Returns the byte array that backs this buffer. | |
212 | * | |
213 | * @exception ReadOnlyBufferException If this buffer is backed by an array | |
214 | * but is read-only. | |
215 | * @exception UnsupportedOperationException If this buffer is not backed | |
216 | * by an accessible array. | |
217 | */ | |
218 | public final byte[] array () | |
219 | { | |
220 | if (backing_buffer == null) | |
221 | throw new UnsupportedOperationException (); | |
222 | ||
194ea9ce | 223 | if (isReadOnly ()) |
d19e783c MK |
224 | throw new ReadOnlyBufferException (); |
225 | ||
226 | return backing_buffer; | |
227 | } | |
228 | ||
229 | /** | |
230 | * Returns the offset within this buffer's backing array of the first element | |
231 | * of the buffer | |
232 | * | |
233 | * @exception ReadOnlyBufferException If this buffer is backed by an array | |
234 | * but is read-only. | |
235 | * @exception UnsupportedOperationException If this buffer is not backed | |
236 | * by an accessible array. | |
237 | */ | |
238 | public final int arrayOffset () | |
239 | { | |
240 | if (backing_buffer == null) | |
241 | throw new UnsupportedOperationException (); | |
242 | ||
194ea9ce | 243 | if (isReadOnly ()) |
d19e783c MK |
244 | throw new ReadOnlyBufferException (); |
245 | ||
246 | return offset; | |
247 | } | |
248 | ||
b772d2f5 MK |
249 | /** |
250 | * Returns the current hash code of this buffer. | |
251 | */ | |
252 | public int hashCode() | |
253 | { | |
4b6eac52 | 254 | // FIXME: Check what SUN calculates here |
b772d2f5 MK |
255 | return super.hashCode(); |
256 | } | |
257 | ||
d19e783c | 258 | /** |
8c9c9dfb MK |
259 | * Tells whether or not this buffer is equal to another object. |
260 | */ | |
261 | public boolean equals (Object obj) | |
262 | { | |
263 | if (obj != null && | |
264 | obj instanceof ByteBuffer) | |
265 | { | |
266 | return compareTo (obj) == 0; | |
267 | } | |
268 | ||
269 | return false; | |
270 | } | |
271 | ||
272 | /** | |
273 | * Compares this buffer to another object. | |
274 | * | |
275 | * @exception ClassCastException If the argument is not a byte buffer | |
276 | */ | |
277 | public int compareTo (Object obj) | |
278 | { | |
279 | ByteBuffer a = (ByteBuffer) obj; | |
280 | ||
281 | if (a.remaining() != remaining()) | |
282 | { | |
283 | return 1; | |
284 | } | |
285 | ||
286 | if (! hasArray() || | |
287 | ! a.hasArray()) | |
288 | { | |
289 | return 1; | |
290 | } | |
291 | ||
292 | int r = remaining(); | |
293 | int i1 = position (); | |
294 | int i2 = a.position (); | |
295 | ||
296 | for (int i = 0; i < r; i++) | |
297 | { | |
298 | int t = (int) (get (i1) - a.get (i2)); | |
299 | ||
300 | if (t != 0) | |
301 | { | |
302 | return (int) t; | |
303 | } | |
304 | } | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Retrieves this buffer's byte order. | |
311 | */ | |
312 | public final ByteOrder order() | |
313 | { | |
314 | return endian; | |
315 | } | |
316 | ||
317 | /** | |
318 | * Modifies this buffer's byte order. | |
319 | */ | |
320 | public final ByteBuffer order (ByteOrder endian) | |
321 | { | |
322 | this.endian = endian; | |
323 | return this; | |
324 | } | |
325 | ||
326 | /** | |
327 | * Reads the byte at this buffer's current position, | |
328 | * and then increments the position. | |
329 | * | |
330 | * @exception BufferUnderflowException If the buffer's current position | |
331 | * is not smaller than its limit. | |
d19e783c | 332 | */ |
61d31826 MK |
333 | public abstract byte get (); |
334 | ||
d19e783c MK |
335 | /** |
336 | * Relative put method. | |
337 | * | |
338 | * @exception BufferOverflowException If this buffer's current position is | |
339 | * not smaller than its limit. | |
340 | * @exception ReadOnlyBufferException If this buffer is read-only. | |
341 | */ | |
61d31826 | 342 | public abstract ByteBuffer put (byte b); |
8c9c9dfb MK |
343 | |
344 | /** | |
345 | * Absolute get method. | |
346 | * | |
4b6eac52 MK |
347 | * @exception IndexOutOfBoundsException If index < 0 or index >= this |
348 | * buffers limit. | |
8c9c9dfb MK |
349 | */ |
350 | public abstract byte get (int index); | |
351 | ||
352 | /** | |
353 | * Absolute put method. | |
354 | * | |
355 | * @exception ReadOnlyBufferException If this buffer is read-only | |
4b6eac52 MK |
356 | * @exception IndexOutOfBoundsException If index < 0 or index >= this |
357 | * buffers limit. | |
8c9c9dfb MK |
358 | */ |
359 | public abstract ByteBuffer put (int index, byte b); | |
360 | ||
361 | /** | |
362 | * Compacts this buffer. | |
363 | * | |
364 | * @exception ReadOnlyBufferException If this buffer is read-only | |
365 | */ | |
366 | public abstract ByteBuffer compact(); | |
367 | ||
368 | /** | |
369 | * Tells whether or not this buffer is direct. | |
370 | */ | |
371 | public abstract boolean isDirect(); | |
372 | ||
373 | /** | |
374 | * Creates a new byte buffer whose content is a shared subsequence of this | |
375 | * buffer's content. | |
376 | */ | |
377 | public abstract ByteBuffer slice(); | |
378 | ||
379 | /** | |
380 | * Creates a new byte buffer that shares this buffer's content. | |
381 | */ | |
382 | public abstract ByteBuffer duplicate(); | |
383 | ||
384 | /** | |
385 | * Creates a new, read-only byte buffer that shares this buffer's content. | |
386 | */ | |
387 | public abstract ByteBuffer asReadOnlyBuffer(); | |
388 | ||
389 | /** | |
390 | * Creates a view of this byte buffer as a short buffer. | |
391 | */ | |
392 | public abstract ShortBuffer asShortBuffer(); | |
393 | ||
394 | /** | |
395 | * Creates a view of this byte buffer as a char buffer. | |
396 | */ | |
397 | public abstract CharBuffer asCharBuffer(); | |
398 | ||
399 | /** | |
400 | * Creates a view of this byte buffer as an integer buffer. | |
401 | */ | |
402 | public abstract IntBuffer asIntBuffer(); | |
403 | ||
404 | /** | |
405 | * Creates a view of this byte buffer as a long buffer. | |
406 | */ | |
407 | public abstract LongBuffer asLongBuffer(); | |
408 | ||
409 | /** | |
410 | * Creates a view of this byte buffer as a float buffer. | |
411 | */ | |
412 | public abstract FloatBuffer asFloatBuffer(); | |
413 | ||
414 | /** | |
415 | * Creates a view of this byte buffer as a double buffer. | |
416 | */ | |
417 | public abstract DoubleBuffer asDoubleBuffer(); | |
418 | ||
419 | /** | |
420 | * Relative get method for reading a character value. | |
421 | * | |
422 | * @exception BufferUnderflowException If there are fewer than two bytes | |
423 | * remaining in this buffer. | |
424 | */ | |
425 | public abstract char getChar(); | |
426 | ||
427 | /** | |
428 | * Relative put method for writing a character value. | |
429 | * | |
430 | * @exception BufferOverflowException If this buffer's current position is | |
431 | * not smaller than its limit. | |
432 | */ | |
433 | public abstract ByteBuffer putChar(char value); | |
434 | ||
435 | /** | |
436 | * Absolute get method for reading a character value. | |
437 | * | |
438 | * @exception IndexOutOfBoundsException If there are fewer than two bytes | |
439 | * remaining in this buffer | |
440 | */ | |
441 | public abstract char getChar(int index); | |
442 | ||
443 | /** | |
444 | * Absolute put method for writing a character value. | |
445 | * | |
446 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
447 | * than the buffer's limit, minus one. | |
448 | */ | |
449 | public abstract ByteBuffer putChar(int index, char value); | |
450 | ||
451 | /** | |
452 | * Relative get method for reading a short value. | |
453 | * | |
454 | * @exception BufferUnderflowException If index is negative or not smaller | |
455 | * than the buffer's limit, minus one. | |
456 | */ | |
457 | public abstract short getShort(); | |
458 | ||
459 | /** | |
460 | * Relative put method for writing a short value. | |
461 | * | |
462 | * @exception BufferOverflowException If this buffer's current position is | |
463 | * not smaller than its limit. | |
464 | */ | |
465 | public abstract ByteBuffer putShort(short value); | |
466 | ||
467 | /** | |
468 | * Absolute get method for reading a short value. | |
469 | * | |
470 | * @exception IndexOutOfBoundsException If there are fewer than two bytes | |
471 | * remaining in this buffer | |
472 | */ | |
473 | public abstract short getShort(int index); | |
474 | ||
475 | /** | |
476 | * Absolute put method for writing a short value. | |
477 | * | |
478 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
479 | * than the buffer's limit, minus one. | |
480 | */ | |
481 | public abstract ByteBuffer putShort(int index, short value); | |
482 | ||
483 | /** | |
484 | * Relative get method for reading an integer value. | |
485 | * | |
486 | * @exception BufferUnderflowException If there are fewer than four bytes | |
487 | * remaining in this buffer. | |
488 | */ | |
489 | public abstract int getInt(); | |
490 | ||
491 | /** | |
492 | * Relative put method for writing an integer value. | |
493 | * | |
494 | * @exception BufferOverflowException If this buffer's current position is | |
495 | * not smaller than its limit. | |
496 | */ | |
497 | public abstract ByteBuffer putInt(int value); | |
498 | ||
499 | /** | |
500 | * Absolute get method for reading an integer value. | |
501 | * | |
502 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
503 | * than the buffer's limit, minus three. | |
504 | */ | |
505 | public abstract int getInt(int index); | |
506 | ||
507 | /** | |
508 | * Absolute put method for writing an integer value. | |
509 | * | |
510 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
511 | * than the buffer's limit, minus three. | |
512 | */ | |
513 | public abstract ByteBuffer putInt(int index, int value); | |
514 | ||
515 | /** | |
516 | * Relative get method for reading a long value. | |
517 | * | |
518 | * @exception BufferUnderflowException If there are fewer than eight bytes | |
519 | * remaining in this buffer. | |
520 | */ | |
521 | public abstract long getLong(); | |
522 | ||
523 | /** | |
524 | * Relative put method for writing a long value. | |
525 | * | |
526 | * @exception BufferOverflowException If this buffer's current position is | |
527 | * not smaller than its limit. | |
528 | */ | |
529 | public abstract ByteBuffer putLong(long value); | |
530 | ||
531 | /** | |
532 | * Absolute get method for reading a long value. | |
533 | * | |
534 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
535 | * than the buffer's limit, minus seven. | |
536 | */ | |
537 | public abstract long getLong(int index); | |
538 | ||
539 | /** | |
540 | * Absolute put method for writing a float value. | |
541 | * | |
542 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
543 | * than the buffer's limit, minus seven. | |
544 | */ | |
545 | public abstract ByteBuffer putLong(int index, long value); | |
546 | ||
547 | /** | |
548 | * Relative get method for reading a float value. | |
549 | * | |
550 | * @exception BufferUnderflowException If there are fewer than four bytes | |
551 | * remaining in this buffer. | |
552 | */ | |
553 | public abstract float getFloat(); | |
554 | ||
555 | /** | |
556 | * Relative put method for writing a float value. | |
557 | * | |
558 | * @exception BufferOverflowException If there are fewer than four bytes | |
559 | * remaining in this buffer. | |
560 | */ | |
561 | public abstract ByteBuffer putFloat(float value); | |
562 | ||
563 | /** | |
564 | * Absolute get method for reading a float value. | |
565 | * | |
566 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
567 | * than the buffer's limit, minus three. | |
568 | */ | |
569 | public abstract float getFloat(int index); | |
570 | ||
571 | /** | |
572 | * Relative put method for writing a float value. | |
573 | * | |
574 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
575 | * than the buffer's limit, minus three. | |
576 | */ | |
577 | public abstract ByteBuffer putFloat(int index, float value); | |
578 | ||
579 | /** | |
580 | * Relative get method for reading a double value. | |
581 | * | |
582 | * @exception BufferUnderflowException If there are fewer than eight bytes | |
583 | * remaining in this buffer. | |
584 | */ | |
585 | public abstract double getDouble(); | |
586 | ||
587 | /** | |
588 | * Relative put method for writing a double value. | |
589 | * | |
590 | * @exception BufferOverflowException If this buffer's current position is | |
591 | * not smaller than its limit. | |
592 | */ | |
593 | public abstract ByteBuffer putDouble(double value); | |
594 | ||
595 | /** | |
596 | * Absolute get method for reading a double value. | |
597 | * | |
598 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
599 | * than the buffer's limit, minus seven. | |
600 | */ | |
601 | public abstract double getDouble(int index); | |
602 | ||
603 | /** | |
604 | * Absolute put method for writing a double value. | |
605 | * | |
606 | * @exception IndexOutOfBoundsException If index is negative or not smaller | |
607 | * than the buffer's limit, minus seven. | |
608 | */ | |
609 | public abstract ByteBuffer putDouble(int index, double value); | |
610 | ||
611 | /** | |
612 | * Returns a string summarizing the state of this buffer. | |
613 | */ | |
614 | public String toString () | |
615 | { | |
616 | return getClass ().getName () + | |
617 | "[pos=" + position () + | |
618 | " lim=" + limit () + | |
619 | " cap=" + capacity () + "]"; | |
620 | } | |
7f73f46f | 621 | } |