|
1 /* |
|
2 * Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 #warn This file is preprocessed before being compiled |
|
27 |
|
28 package java.nio; |
|
29 |
|
30 |
|
31 /** |
|
32 #if[rw] |
|
33 * A read/write Heap$Type$Buffer. |
|
34 #else[rw] |
|
35 * A read-only Heap$Type$Buffer. This class extends the corresponding |
|
36 * read/write class, overriding the mutation methods to throw a {@link |
|
37 * ReadOnlyBufferException} and overriding the view-buffer methods to return an |
|
38 * instance of this class rather than of the superclass. |
|
39 #end[rw] |
|
40 */ |
|
41 |
|
42 class Heap$Type$Buffer$RW$ |
|
43 extends {#if[ro]?Heap}$Type$Buffer |
|
44 { |
|
45 |
|
46 // For speed these fields are actually declared in X-Buffer; |
|
47 // these declarations are here as documentation |
|
48 /* |
|
49 #if[rw] |
|
50 protected final $type$[] hb; |
|
51 protected final int offset; |
|
52 #end[rw] |
|
53 */ |
|
54 |
|
55 Heap$Type$Buffer$RW$(int cap, int lim) { // package-private |
|
56 #if[rw] |
|
57 super(-1, 0, lim, cap, new $type$[cap], 0); |
|
58 /* |
|
59 hb = new $type$[cap]; |
|
60 offset = 0; |
|
61 */ |
|
62 #else[rw] |
|
63 super(cap, lim); |
|
64 this.isReadOnly = true; |
|
65 #end[rw] |
|
66 } |
|
67 |
|
68 Heap$Type$Buffer$RW$($type$[] buf, int off, int len) { // package-private |
|
69 #if[rw] |
|
70 super(-1, off, off + len, buf.length, buf, 0); |
|
71 /* |
|
72 hb = buf; |
|
73 offset = 0; |
|
74 */ |
|
75 #else[rw] |
|
76 super(buf, off, len); |
|
77 this.isReadOnly = true; |
|
78 #end[rw] |
|
79 } |
|
80 |
|
81 protected Heap$Type$Buffer$RW$($type$[] buf, |
|
82 int mark, int pos, int lim, int cap, |
|
83 int off) |
|
84 { |
|
85 #if[rw] |
|
86 super(mark, pos, lim, cap, buf, off); |
|
87 /* |
|
88 hb = buf; |
|
89 offset = off; |
|
90 */ |
|
91 #else[rw] |
|
92 super(buf, mark, pos, lim, cap, off); |
|
93 this.isReadOnly = true; |
|
94 #end[rw] |
|
95 } |
|
96 |
|
97 public $Type$Buffer slice() { |
|
98 return new Heap$Type$Buffer$RW$(hb, |
|
99 -1, |
|
100 0, |
|
101 this.remaining(), |
|
102 this.remaining(), |
|
103 this.position() + offset); |
|
104 } |
|
105 |
|
106 public $Type$Buffer duplicate() { |
|
107 return new Heap$Type$Buffer$RW$(hb, |
|
108 this.markValue(), |
|
109 this.position(), |
|
110 this.limit(), |
|
111 this.capacity(), |
|
112 offset); |
|
113 } |
|
114 |
|
115 public $Type$Buffer asReadOnlyBuffer() { |
|
116 #if[rw] |
|
117 return new Heap$Type$BufferR(hb, |
|
118 this.markValue(), |
|
119 this.position(), |
|
120 this.limit(), |
|
121 this.capacity(), |
|
122 offset); |
|
123 #else[rw] |
|
124 return duplicate(); |
|
125 #end[rw] |
|
126 } |
|
127 |
|
128 #if[rw] |
|
129 |
|
130 protected int ix(int i) { |
|
131 return i + offset; |
|
132 } |
|
133 |
|
134 public $type$ get() { |
|
135 return hb[ix(nextGetIndex())]; |
|
136 } |
|
137 |
|
138 public $type$ get(int i) { |
|
139 return hb[ix(checkIndex(i))]; |
|
140 } |
|
141 |
|
142 public $Type$Buffer get($type$[] dst, int offset, int length) { |
|
143 checkBounds(offset, length, dst.length); |
|
144 if (length > remaining()) |
|
145 throw new BufferUnderflowException(); |
|
146 System.arraycopy(hb, ix(position()), dst, offset, length); |
|
147 position(position() + length); |
|
148 return this; |
|
149 } |
|
150 |
|
151 public boolean isDirect() { |
|
152 return false; |
|
153 } |
|
154 |
|
155 #end[rw] |
|
156 |
|
157 public boolean isReadOnly() { |
|
158 return {#if[rw]?false:true}; |
|
159 } |
|
160 |
|
161 public $Type$Buffer put($type$ x) { |
|
162 #if[rw] |
|
163 hb[ix(nextPutIndex())] = x; |
|
164 return this; |
|
165 #else[rw] |
|
166 throw new ReadOnlyBufferException(); |
|
167 #end[rw] |
|
168 } |
|
169 |
|
170 public $Type$Buffer put(int i, $type$ x) { |
|
171 #if[rw] |
|
172 hb[ix(checkIndex(i))] = x; |
|
173 return this; |
|
174 #else[rw] |
|
175 throw new ReadOnlyBufferException(); |
|
176 #end[rw] |
|
177 } |
|
178 |
|
179 public $Type$Buffer put($type$[] src, int offset, int length) { |
|
180 #if[rw] |
|
181 checkBounds(offset, length, src.length); |
|
182 if (length > remaining()) |
|
183 throw new BufferOverflowException(); |
|
184 System.arraycopy(src, offset, hb, ix(position()), length); |
|
185 position(position() + length); |
|
186 return this; |
|
187 #else[rw] |
|
188 throw new ReadOnlyBufferException(); |
|
189 #end[rw] |
|
190 } |
|
191 |
|
192 public $Type$Buffer put($Type$Buffer src) { |
|
193 #if[rw] |
|
194 if (src instanceof Heap$Type$Buffer) { |
|
195 if (src == this) |
|
196 throw new IllegalArgumentException(); |
|
197 Heap$Type$Buffer sb = (Heap$Type$Buffer)src; |
|
198 int n = sb.remaining(); |
|
199 if (n > remaining()) |
|
200 throw new BufferOverflowException(); |
|
201 System.arraycopy(sb.hb, sb.ix(sb.position()), |
|
202 hb, ix(position()), n); |
|
203 sb.position(sb.position() + n); |
|
204 position(position() + n); |
|
205 } else if (src.isDirect()) { |
|
206 int n = src.remaining(); |
|
207 if (n > remaining()) |
|
208 throw new BufferOverflowException(); |
|
209 src.get(hb, ix(position()), n); |
|
210 position(position() + n); |
|
211 } else { |
|
212 super.put(src); |
|
213 } |
|
214 return this; |
|
215 #else[rw] |
|
216 throw new ReadOnlyBufferException(); |
|
217 #end[rw] |
|
218 } |
|
219 |
|
220 public $Type$Buffer compact() { |
|
221 #if[rw] |
|
222 System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); |
|
223 position(remaining()); |
|
224 limit(capacity()); |
|
225 discardMark(); |
|
226 return this; |
|
227 #else[rw] |
|
228 throw new ReadOnlyBufferException(); |
|
229 #end[rw] |
|
230 } |
|
231 |
|
232 |
|
233 |
|
234 #if[byte] |
|
235 |
|
236 byte _get(int i) { // package-private |
|
237 return hb[i]; |
|
238 } |
|
239 |
|
240 void _put(int i, byte b) { // package-private |
|
241 #if[rw] |
|
242 hb[i] = b; |
|
243 #else[rw] |
|
244 throw new ReadOnlyBufferException(); |
|
245 #end[rw] |
|
246 } |
|
247 |
|
248 // char |
|
249 |
|
250 #if[rw] |
|
251 |
|
252 public char getChar() { |
|
253 return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); |
|
254 } |
|
255 |
|
256 public char getChar(int i) { |
|
257 return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); |
|
258 } |
|
259 |
|
260 #end[rw] |
|
261 |
|
262 public $Type$Buffer putChar(char x) { |
|
263 #if[rw] |
|
264 Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); |
|
265 return this; |
|
266 #else[rw] |
|
267 throw new ReadOnlyBufferException(); |
|
268 #end[rw] |
|
269 } |
|
270 |
|
271 public $Type$Buffer putChar(int i, char x) { |
|
272 #if[rw] |
|
273 Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); |
|
274 return this; |
|
275 #else[rw] |
|
276 throw new ReadOnlyBufferException(); |
|
277 #end[rw] |
|
278 } |
|
279 |
|
280 public CharBuffer asCharBuffer() { |
|
281 int size = this.remaining() >> 1; |
|
282 int off = offset + position(); |
|
283 return (bigEndian |
|
284 ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, |
|
285 -1, |
|
286 0, |
|
287 size, |
|
288 size, |
|
289 off)) |
|
290 : (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this, |
|
291 -1, |
|
292 0, |
|
293 size, |
|
294 size, |
|
295 off))); |
|
296 } |
|
297 |
|
298 |
|
299 // short |
|
300 |
|
301 #if[rw] |
|
302 |
|
303 public short getShort() { |
|
304 return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); |
|
305 } |
|
306 |
|
307 public short getShort(int i) { |
|
308 return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); |
|
309 } |
|
310 |
|
311 #end[rw] |
|
312 |
|
313 public $Type$Buffer putShort(short x) { |
|
314 #if[rw] |
|
315 Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); |
|
316 return this; |
|
317 #else[rw] |
|
318 throw new ReadOnlyBufferException(); |
|
319 #end[rw] |
|
320 } |
|
321 |
|
322 public $Type$Buffer putShort(int i, short x) { |
|
323 #if[rw] |
|
324 Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); |
|
325 return this; |
|
326 #else[rw] |
|
327 throw new ReadOnlyBufferException(); |
|
328 #end[rw] |
|
329 } |
|
330 |
|
331 public ShortBuffer asShortBuffer() { |
|
332 int size = this.remaining() >> 1; |
|
333 int off = offset + position(); |
|
334 return (bigEndian |
|
335 ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, |
|
336 -1, |
|
337 0, |
|
338 size, |
|
339 size, |
|
340 off)) |
|
341 : (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this, |
|
342 -1, |
|
343 0, |
|
344 size, |
|
345 size, |
|
346 off))); |
|
347 } |
|
348 |
|
349 |
|
350 // int |
|
351 |
|
352 #if[rw] |
|
353 |
|
354 public int getInt() { |
|
355 return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); |
|
356 } |
|
357 |
|
358 public int getInt(int i) { |
|
359 return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); |
|
360 } |
|
361 |
|
362 #end[rw] |
|
363 |
|
364 public $Type$Buffer putInt(int x) { |
|
365 #if[rw] |
|
366 Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); |
|
367 return this; |
|
368 #else[rw] |
|
369 throw new ReadOnlyBufferException(); |
|
370 #end[rw] |
|
371 } |
|
372 |
|
373 public $Type$Buffer putInt(int i, int x) { |
|
374 #if[rw] |
|
375 Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); |
|
376 return this; |
|
377 #else[rw] |
|
378 throw new ReadOnlyBufferException(); |
|
379 #end[rw] |
|
380 } |
|
381 |
|
382 public IntBuffer asIntBuffer() { |
|
383 int size = this.remaining() >> 2; |
|
384 int off = offset + position(); |
|
385 return (bigEndian |
|
386 ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, |
|
387 -1, |
|
388 0, |
|
389 size, |
|
390 size, |
|
391 off)) |
|
392 : (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this, |
|
393 -1, |
|
394 0, |
|
395 size, |
|
396 size, |
|
397 off))); |
|
398 } |
|
399 |
|
400 |
|
401 // long |
|
402 |
|
403 #if[rw] |
|
404 |
|
405 public long getLong() { |
|
406 return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); |
|
407 } |
|
408 |
|
409 public long getLong(int i) { |
|
410 return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); |
|
411 } |
|
412 |
|
413 #end[rw] |
|
414 |
|
415 public $Type$Buffer putLong(long x) { |
|
416 #if[rw] |
|
417 Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); |
|
418 return this; |
|
419 #else[rw] |
|
420 throw new ReadOnlyBufferException(); |
|
421 #end[rw] |
|
422 } |
|
423 |
|
424 public $Type$Buffer putLong(int i, long x) { |
|
425 #if[rw] |
|
426 Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); |
|
427 return this; |
|
428 #else[rw] |
|
429 throw new ReadOnlyBufferException(); |
|
430 #end[rw] |
|
431 } |
|
432 |
|
433 public LongBuffer asLongBuffer() { |
|
434 int size = this.remaining() >> 3; |
|
435 int off = offset + position(); |
|
436 return (bigEndian |
|
437 ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, |
|
438 -1, |
|
439 0, |
|
440 size, |
|
441 size, |
|
442 off)) |
|
443 : (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this, |
|
444 -1, |
|
445 0, |
|
446 size, |
|
447 size, |
|
448 off))); |
|
449 } |
|
450 |
|
451 |
|
452 // float |
|
453 |
|
454 #if[rw] |
|
455 |
|
456 public float getFloat() { |
|
457 return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); |
|
458 } |
|
459 |
|
460 public float getFloat(int i) { |
|
461 return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); |
|
462 } |
|
463 |
|
464 #end[rw] |
|
465 |
|
466 public $Type$Buffer putFloat(float x) { |
|
467 #if[rw] |
|
468 Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); |
|
469 return this; |
|
470 #else[rw] |
|
471 throw new ReadOnlyBufferException(); |
|
472 #end[rw] |
|
473 } |
|
474 |
|
475 public $Type$Buffer putFloat(int i, float x) { |
|
476 #if[rw] |
|
477 Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); |
|
478 return this; |
|
479 #else[rw] |
|
480 throw new ReadOnlyBufferException(); |
|
481 #end[rw] |
|
482 } |
|
483 |
|
484 public FloatBuffer asFloatBuffer() { |
|
485 int size = this.remaining() >> 2; |
|
486 int off = offset + position(); |
|
487 return (bigEndian |
|
488 ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, |
|
489 -1, |
|
490 0, |
|
491 size, |
|
492 size, |
|
493 off)) |
|
494 : (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this, |
|
495 -1, |
|
496 0, |
|
497 size, |
|
498 size, |
|
499 off))); |
|
500 } |
|
501 |
|
502 |
|
503 // double |
|
504 |
|
505 #if[rw] |
|
506 |
|
507 public double getDouble() { |
|
508 return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); |
|
509 } |
|
510 |
|
511 public double getDouble(int i) { |
|
512 return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); |
|
513 } |
|
514 |
|
515 #end[rw] |
|
516 |
|
517 public $Type$Buffer putDouble(double x) { |
|
518 #if[rw] |
|
519 Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); |
|
520 return this; |
|
521 #else[rw] |
|
522 throw new ReadOnlyBufferException(); |
|
523 #end[rw] |
|
524 } |
|
525 |
|
526 public $Type$Buffer putDouble(int i, double x) { |
|
527 #if[rw] |
|
528 Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); |
|
529 return this; |
|
530 #else[rw] |
|
531 throw new ReadOnlyBufferException(); |
|
532 #end[rw] |
|
533 } |
|
534 |
|
535 public DoubleBuffer asDoubleBuffer() { |
|
536 int size = this.remaining() >> 3; |
|
537 int off = offset + position(); |
|
538 return (bigEndian |
|
539 ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this, |
|
540 -1, |
|
541 0, |
|
542 size, |
|
543 size, |
|
544 off)) |
|
545 : (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this, |
|
546 -1, |
|
547 0, |
|
548 size, |
|
549 size, |
|
550 off))); |
|
551 } |
|
552 |
|
553 |
|
554 #end[byte] |
|
555 |
|
556 |
|
557 #if[char] |
|
558 |
|
559 String toString(int start, int end) { // package-private |
|
560 try { |
|
561 return new String(hb, start + offset, end - start); |
|
562 } catch (StringIndexOutOfBoundsException x) { |
|
563 throw new IndexOutOfBoundsException(); |
|
564 } |
|
565 } |
|
566 |
|
567 |
|
568 // --- Methods to support CharSequence --- |
|
569 |
|
570 public CharBuffer subSequence(int start, int end) { |
|
571 if ((start < 0) |
|
572 || (end > length()) |
|
573 || (start > end)) |
|
574 throw new IndexOutOfBoundsException(); |
|
575 int pos = position(); |
|
576 return new HeapCharBuffer$RW$(hb, |
|
577 -1, |
|
578 pos + start, |
|
579 pos + end, |
|
580 capacity(), |
|
581 offset); |
|
582 } |
|
583 |
|
584 #end[char] |
|
585 |
|
586 |
|
587 #if[!byte] |
|
588 |
|
589 public ByteOrder order() { |
|
590 return ByteOrder.nativeOrder(); |
|
591 } |
|
592 |
|
593 #end[!byte] |
|
594 |
|
595 } |