author | xdono |
Thu, 02 Oct 2008 19:58:32 -0700 | |
changeset 1247 | b4c26443dee5 |
parent 1224 | 8c1bc7c5dd00 |
child 1634 | 3871c2046043 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 |
return this; |
|
226 |
#else[rw] |
|
227 |
throw new ReadOnlyBufferException(); |
|
228 |
#end[rw] |
|
229 |
} |
|
230 |
||
231 |
||
232 |
||
233 |
#if[byte] |
|
234 |
||
235 |
byte _get(int i) { // package-private |
|
236 |
return hb[i]; |
|
237 |
} |
|
238 |
||
239 |
void _put(int i, byte b) { // package-private |
|
240 |
#if[rw] |
|
241 |
hb[i] = b; |
|
242 |
#else[rw] |
|
243 |
throw new ReadOnlyBufferException(); |
|
244 |
#end[rw] |
|
245 |
} |
|
246 |
||
247 |
// char |
|
248 |
||
249 |
#if[rw] |
|
250 |
||
251 |
public char getChar() { |
|
252 |
return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); |
|
253 |
} |
|
254 |
||
255 |
public char getChar(int i) { |
|
256 |
return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); |
|
257 |
} |
|
258 |
||
259 |
#end[rw] |
|
260 |
||
261 |
public $Type$Buffer putChar(char x) { |
|
262 |
#if[rw] |
|
263 |
Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); |
|
264 |
return this; |
|
265 |
#else[rw] |
|
266 |
throw new ReadOnlyBufferException(); |
|
267 |
#end[rw] |
|
268 |
} |
|
269 |
||
270 |
public $Type$Buffer putChar(int i, char x) { |
|
271 |
#if[rw] |
|
272 |
Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); |
|
273 |
return this; |
|
274 |
#else[rw] |
|
275 |
throw new ReadOnlyBufferException(); |
|
276 |
#end[rw] |
|
277 |
} |
|
278 |
||
279 |
public CharBuffer asCharBuffer() { |
|
280 |
int size = this.remaining() >> 1; |
|
281 |
int off = offset + position(); |
|
282 |
return (bigEndian |
|
283 |
? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this, |
|
284 |
-1, |
|
285 |
0, |
|
286 |
size, |
|
287 |
size, |
|
288 |
off)) |
|
289 |
: (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this, |
|
290 |
-1, |
|
291 |
0, |
|
292 |
size, |
|
293 |
size, |
|
294 |
off))); |
|
295 |
} |
|
296 |
||
297 |
||
298 |
// short |
|
299 |
||
300 |
#if[rw] |
|
301 |
||
302 |
public short getShort() { |
|
303 |
return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); |
|
304 |
} |
|
305 |
||
306 |
public short getShort(int i) { |
|
307 |
return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); |
|
308 |
} |
|
309 |
||
310 |
#end[rw] |
|
311 |
||
312 |
public $Type$Buffer putShort(short x) { |
|
313 |
#if[rw] |
|
314 |
Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); |
|
315 |
return this; |
|
316 |
#else[rw] |
|
317 |
throw new ReadOnlyBufferException(); |
|
318 |
#end[rw] |
|
319 |
} |
|
320 |
||
321 |
public $Type$Buffer putShort(int i, short x) { |
|
322 |
#if[rw] |
|
323 |
Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); |
|
324 |
return this; |
|
325 |
#else[rw] |
|
326 |
throw new ReadOnlyBufferException(); |
|
327 |
#end[rw] |
|
328 |
} |
|
329 |
||
330 |
public ShortBuffer asShortBuffer() { |
|
331 |
int size = this.remaining() >> 1; |
|
332 |
int off = offset + position(); |
|
333 |
return (bigEndian |
|
334 |
? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this, |
|
335 |
-1, |
|
336 |
0, |
|
337 |
size, |
|
338 |
size, |
|
339 |
off)) |
|
340 |
: (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this, |
|
341 |
-1, |
|
342 |
0, |
|
343 |
size, |
|
344 |
size, |
|
345 |
off))); |
|
346 |
} |
|
347 |
||
348 |
||
349 |
// int |
|
350 |
||
351 |
#if[rw] |
|
352 |
||
353 |
public int getInt() { |
|
354 |
return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); |
|
355 |
} |
|
356 |
||
357 |
public int getInt(int i) { |
|
358 |
return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); |
|
359 |
} |
|
360 |
||
361 |
#end[rw] |
|
362 |
||
363 |
public $Type$Buffer putInt(int x) { |
|
364 |
#if[rw] |
|
365 |
Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); |
|
366 |
return this; |
|
367 |
#else[rw] |
|
368 |
throw new ReadOnlyBufferException(); |
|
369 |
#end[rw] |
|
370 |
} |
|
371 |
||
372 |
public $Type$Buffer putInt(int i, int x) { |
|
373 |
#if[rw] |
|
374 |
Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); |
|
375 |
return this; |
|
376 |
#else[rw] |
|
377 |
throw new ReadOnlyBufferException(); |
|
378 |
#end[rw] |
|
379 |
} |
|
380 |
||
381 |
public IntBuffer asIntBuffer() { |
|
382 |
int size = this.remaining() >> 2; |
|
383 |
int off = offset + position(); |
|
384 |
return (bigEndian |
|
385 |
? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this, |
|
386 |
-1, |
|
387 |
0, |
|
388 |
size, |
|
389 |
size, |
|
390 |
off)) |
|
391 |
: (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this, |
|
392 |
-1, |
|
393 |
0, |
|
394 |
size, |
|
395 |
size, |
|
396 |
off))); |
|
397 |
} |
|
398 |
||
399 |
||
400 |
// long |
|
401 |
||
402 |
#if[rw] |
|
403 |
||
404 |
public long getLong() { |
|
405 |
return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); |
|
406 |
} |
|
407 |
||
408 |
public long getLong(int i) { |
|
409 |
return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); |
|
410 |
} |
|
411 |
||
412 |
#end[rw] |
|
413 |
||
414 |
public $Type$Buffer putLong(long x) { |
|
415 |
#if[rw] |
|
416 |
Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); |
|
417 |
return this; |
|
418 |
#else[rw] |
|
419 |
throw new ReadOnlyBufferException(); |
|
420 |
#end[rw] |
|
421 |
} |
|
422 |
||
423 |
public $Type$Buffer putLong(int i, long x) { |
|
424 |
#if[rw] |
|
425 |
Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); |
|
426 |
return this; |
|
427 |
#else[rw] |
|
428 |
throw new ReadOnlyBufferException(); |
|
429 |
#end[rw] |
|
430 |
} |
|
431 |
||
432 |
public LongBuffer asLongBuffer() { |
|
433 |
int size = this.remaining() >> 3; |
|
434 |
int off = offset + position(); |
|
435 |
return (bigEndian |
|
436 |
? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this, |
|
437 |
-1, |
|
438 |
0, |
|
439 |
size, |
|
440 |
size, |
|
441 |
off)) |
|
442 |
: (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this, |
|
443 |
-1, |
|
444 |
0, |
|
445 |
size, |
|
446 |
size, |
|
447 |
off))); |
|
448 |
} |
|
449 |
||
450 |
||
451 |
// float |
|
452 |
||
453 |
#if[rw] |
|
454 |
||
455 |
public float getFloat() { |
|
456 |
return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); |
|
457 |
} |
|
458 |
||
459 |
public float getFloat(int i) { |
|
460 |
return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); |
|
461 |
} |
|
462 |
||
463 |
#end[rw] |
|
464 |
||
465 |
public $Type$Buffer putFloat(float x) { |
|
466 |
#if[rw] |
|
467 |
Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); |
|
468 |
return this; |
|
469 |
#else[rw] |
|
470 |
throw new ReadOnlyBufferException(); |
|
471 |
#end[rw] |
|
472 |
} |
|
473 |
||
474 |
public $Type$Buffer putFloat(int i, float x) { |
|
475 |
#if[rw] |
|
476 |
Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); |
|
477 |
return this; |
|
478 |
#else[rw] |
|
479 |
throw new ReadOnlyBufferException(); |
|
480 |
#end[rw] |
|
481 |
} |
|
482 |
||
483 |
public FloatBuffer asFloatBuffer() { |
|
484 |
int size = this.remaining() >> 2; |
|
485 |
int off = offset + position(); |
|
486 |
return (bigEndian |
|
487 |
? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this, |
|
488 |
-1, |
|
489 |
0, |
|
490 |
size, |
|
491 |
size, |
|
492 |
off)) |
|
493 |
: (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this, |
|
494 |
-1, |
|
495 |
0, |
|
496 |
size, |
|
497 |
size, |
|
498 |
off))); |
|
499 |
} |
|
500 |
||
501 |
||
502 |
// double |
|
503 |
||
504 |
#if[rw] |
|
505 |
||
506 |
public double getDouble() { |
|
507 |
return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); |
|
508 |
} |
|
509 |
||
510 |
public double getDouble(int i) { |
|
511 |
return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); |
|
512 |
} |
|
513 |
||
514 |
#end[rw] |
|
515 |
||
516 |
public $Type$Buffer putDouble(double x) { |
|
517 |
#if[rw] |
|
518 |
Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); |
|
519 |
return this; |
|
520 |
#else[rw] |
|
521 |
throw new ReadOnlyBufferException(); |
|
522 |
#end[rw] |
|
523 |
} |
|
524 |
||
525 |
public $Type$Buffer putDouble(int i, double x) { |
|
526 |
#if[rw] |
|
527 |
Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); |
|
528 |
return this; |
|
529 |
#else[rw] |
|
530 |
throw new ReadOnlyBufferException(); |
|
531 |
#end[rw] |
|
532 |
} |
|
533 |
||
534 |
public DoubleBuffer asDoubleBuffer() { |
|
535 |
int size = this.remaining() >> 3; |
|
536 |
int off = offset + position(); |
|
537 |
return (bigEndian |
|
538 |
? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this, |
|
539 |
-1, |
|
540 |
0, |
|
541 |
size, |
|
542 |
size, |
|
543 |
off)) |
|
544 |
: (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this, |
|
545 |
-1, |
|
546 |
0, |
|
547 |
size, |
|
548 |
size, |
|
549 |
off))); |
|
550 |
} |
|
551 |
||
552 |
||
553 |
#end[byte] |
|
554 |
||
555 |
||
556 |
#if[char] |
|
557 |
||
558 |
String toString(int start, int end) { // package-private |
|
559 |
try { |
|
560 |
return new String(hb, start + offset, end - start); |
|
561 |
} catch (StringIndexOutOfBoundsException x) { |
|
562 |
throw new IndexOutOfBoundsException(); |
|
563 |
} |
|
564 |
} |
|
565 |
||
566 |
||
567 |
// --- Methods to support CharSequence --- |
|
568 |
||
1224
8c1bc7c5dd00
6733145: (bf) CharBuffer.subSequence can be updated to take advantage of covariant returns
martin
parents:
2
diff
changeset
|
569 |
public CharBuffer subSequence(int start, int end) { |
2 | 570 |
if ((start < 0) |
571 |
|| (end > length()) |
|
572 |
|| (start > end)) |
|
573 |
throw new IndexOutOfBoundsException(); |
|
574 |
int len = end - start; |
|
575 |
return new HeapCharBuffer$RW$(hb, |
|
576 |
-1, 0, len, len, |
|
577 |
offset + position() + start); |
|
578 |
} |
|
579 |
||
580 |
#end[char] |
|
581 |
||
582 |
||
583 |
#if[!byte] |
|
584 |
||
585 |
public ByteOrder order() { |
|
586 |
return ByteOrder.nativeOrder(); |
|
587 |
} |
|
588 |
||
589 |
#end[!byte] |
|
590 |
||
591 |
} |