KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > HeapByteBuffer


1 /*
2  * @(#)Heap-X-Buffer.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 // -- This file was mechanically generated: Do not edit! -- //
9

10 package java.nio;
11
12
13 /**
14
15  * A read/write HeapByteBuffer.
16
17
18
19
20
21
22  */

23
24 class HeapByteBuffer
25     extends ByteBuffer JavaDoc
26 {
27
28     // For speed these fields are actually declared in X-Buffer;
29
// these declarations are here as documentation
30
/*
31
32     protected final byte[] hb;
33     protected final int offset;
34
35     */

36
37     HeapByteBuffer(int cap, int lim) { // package-private
38

39     super(-1, 0, lim, cap, new byte[cap], 0);
40     /*
41     hb = new byte[cap];
42     offset = 0;
43     */

44
45
46
47
48     }
49
50     HeapByteBuffer(byte[] buf, int off, int len) { // package-private
51

52     super(-1, off, off + len, buf.length, buf, 0);
53     /*
54     hb = buf;
55     offset = 0;
56     */

57
58
59
60
61     }
62
63     protected HeapByteBuffer(byte[] buf,
64                    int mark, int pos, int lim, int cap,
65                    int off)
66     {
67
68     super(mark, pos, lim, cap, buf, off);
69     /*
70     hb = buf;
71     offset = off;
72     */

73
74
75
76
77     }
78
79     public ByteBuffer JavaDoc slice() {
80     return new HeapByteBuffer JavaDoc(hb,
81                     -1,
82                     0,
83                     this.remaining(),
84                     this.remaining(),
85                     this.position() + offset);
86     }
87
88     public ByteBuffer JavaDoc duplicate() {
89     return new HeapByteBuffer JavaDoc(hb,
90                     this.markValue(),
91                     this.position(),
92                     this.limit(),
93                     this.capacity(),
94                     offset);
95     }
96
97     public ByteBuffer JavaDoc asReadOnlyBuffer() {
98
99     return new HeapByteBufferR JavaDoc(hb,
100                      this.markValue(),
101                      this.position(),
102                      this.limit(),
103                      this.capacity(),
104                      offset);
105
106
107
108     }
109
110
111
112     protected int ix(int i) {
113     return i + offset;
114     }
115
116     public byte get() {
117     return hb[ix(nextGetIndex())];
118     }
119
120     public byte get(int i) {
121     return hb[ix(checkIndex(i))];
122     }
123
124     public ByteBuffer JavaDoc get(byte[] dst, int offset, int length) {
125     checkBounds(offset, length, dst.length);
126     if (length > remaining())
127         throw new BufferUnderflowException JavaDoc();
128     System.arraycopy(hb, ix(position()), dst, offset, length);
129     position(position() + length);
130     return this;
131     }
132
133     public boolean isDirect() {
134     return false;
135     }
136
137
138
139     public boolean isReadOnly() {
140     return false;
141     }
142
143     public ByteBuffer JavaDoc put(byte x) {
144
145     hb[ix(nextPutIndex())] = x;
146     return this;
147
148
149
150     }
151
152     public ByteBuffer JavaDoc put(int i, byte x) {
153
154     hb[ix(checkIndex(i))] = x;
155     return this;
156
157
158
159     }
160
161     public ByteBuffer JavaDoc put(byte[] src, int offset, int length) {
162
163     checkBounds(offset, length, src.length);
164     if (length > remaining())
165         throw new BufferOverflowException JavaDoc();
166     System.arraycopy(src, offset, hb, ix(position()), length);
167     position(position() + length);
168     return this;
169
170
171
172     }
173
174     public ByteBuffer JavaDoc put(ByteBuffer JavaDoc src) {
175
176     if (src instanceof HeapByteBuffer JavaDoc) {
177         if (src == this)
178         throw new IllegalArgumentException JavaDoc();
179         HeapByteBuffer JavaDoc sb = (HeapByteBuffer JavaDoc)src;
180         int n = sb.remaining();
181         if (n > remaining())
182         throw new BufferOverflowException JavaDoc();
183         System.arraycopy(sb.hb, sb.ix(sb.position()),
184                  hb, ix(position()), n);
185         sb.position(sb.position() + n);
186         position(position() + n);
187     } else if (src.isDirect()) {
188         int n = src.remaining();
189         if (n > remaining())
190         throw new BufferOverflowException JavaDoc();
191         src.get(hb, ix(position()), n);
192         position(position() + n);
193     } else {
194         super.put(src);
195     }
196     return this;
197
198
199
200     }
201
202     public ByteBuffer JavaDoc compact() {
203
204     System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
205     position(remaining());
206     limit(capacity());
207     return this;
208
209
210
211     }
212
213
214
215
216
217     byte _get(int i) { // package-private
218
return hb[i];
219     }
220
221     void _put(int i, byte b) { // package-private
222

223     hb[i] = b;
224
225
226
227     }
228
229     // char
230

231
232
233     public char getChar() {
234     return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian);
235     }
236
237     public char getChar(int i) {
238     return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian);
239     }
240
241
242
243     public ByteBuffer JavaDoc putChar(char x) {
244
245     Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian);
246     return this;
247
248
249
250     }
251
252     public ByteBuffer JavaDoc putChar(int i, char x) {
253
254     Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian);
255     return this;
256
257
258
259     }
260
261     public CharBuffer JavaDoc asCharBuffer() {
262     int size = this.remaining() >> 1;
263     int off = offset + position();
264     return (bigEndian
265         ? (CharBuffer JavaDoc)(new ByteBufferAsCharBufferB JavaDoc(this,
266                                    -1,
267                                    0,
268                                    size,
269                                    size,
270                                    off))
271         : (CharBuffer JavaDoc)(new ByteBufferAsCharBufferL JavaDoc(this,
272                                    -1,
273                                    0,
274                                    size,
275                                    size,
276                                    off)));
277     }
278
279
280     // short
281

282
283
284     public short getShort() {
285     return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian);
286     }
287
288     public short getShort(int i) {
289     return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian);
290     }
291
292
293
294     public ByteBuffer JavaDoc putShort(short x) {
295
296     Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian);
297     return this;
298
299
300
301     }
302
303     public ByteBuffer JavaDoc putShort(int i, short x) {
304
305     Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian);
306     return this;
307
308
309
310     }
311
312     public ShortBuffer JavaDoc asShortBuffer() {
313     int size = this.remaining() >> 1;
314     int off = offset + position();
315     return (bigEndian
316         ? (ShortBuffer JavaDoc)(new ByteBufferAsShortBufferB JavaDoc(this,
317                                  -1,
318                                  0,
319                                  size,
320                                  size,
321                                  off))
322         : (ShortBuffer JavaDoc)(new ByteBufferAsShortBufferL JavaDoc(this,
323                                  -1,
324                                  0,
325                                  size,
326                                  size,
327                                  off)));
328     }
329
330
331     // int
332

333
334
335     public int getInt() {
336     return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
337     }
338
339     public int getInt(int i) {
340     return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian);
341     }
342
343
344
345     public ByteBuffer JavaDoc putInt(int x) {
346
347     Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian);
348     return this;
349
350
351
352     }
353
354     public ByteBuffer JavaDoc putInt(int i, int x) {
355
356     Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian);
357     return this;
358
359
360
361     }
362
363     public IntBuffer JavaDoc asIntBuffer() {
364     int size = this.remaining() >> 2;
365     int off = offset + position();
366     return (bigEndian
367         ? (IntBuffer JavaDoc)(new ByteBufferAsIntBufferB JavaDoc(this,
368                                  -1,
369                                  0,
370                                  size,
371                                  size,
372                                  off))
373         : (IntBuffer JavaDoc)(new ByteBufferAsIntBufferL JavaDoc(this,
374                                  -1,
375                                  0,
376                                  size,
377                                  size,
378                                  off)));
379     }
380
381
382     // long
383

384
385
386     public long getLong() {
387     return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian);
388     }
389
390     public long getLong(int i) {
391     return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian);
392     }
393
394
395
396     public ByteBuffer JavaDoc putLong(long x) {
397
398     Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian);
399     return this;
400
401
402
403     }
404
405     public ByteBuffer JavaDoc putLong(int i, long x) {
406
407     Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian);
408     return this;
409
410
411
412     }
413
414     public LongBuffer JavaDoc asLongBuffer() {
415     int size = this.remaining() >> 3;
416     int off = offset + position();
417     return (bigEndian
418         ? (LongBuffer JavaDoc)(new ByteBufferAsLongBufferB JavaDoc(this,
419                                    -1,
420                                    0,
421                                    size,
422                                    size,
423                                    off))
424         : (LongBuffer JavaDoc)(new ByteBufferAsLongBufferL JavaDoc(this,
425                                    -1,
426                                    0,
427                                    size,
428                                    size,
429                                    off)));
430     }
431
432
433     // float
434

435
436
437     public float getFloat() {
438     return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian);
439     }
440
441     public float getFloat(int i) {
442     return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian);
443     }
444
445
446
447     public ByteBuffer JavaDoc putFloat(float x) {
448
449     Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian);
450     return this;
451
452
453
454     }
455
456     public ByteBuffer JavaDoc putFloat(int i, float x) {
457
458     Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian);
459     return this;
460
461
462
463     }
464
465     public FloatBuffer JavaDoc asFloatBuffer() {
466     int size = this.remaining() >> 2;
467     int off = offset + position();
468     return (bigEndian
469         ? (FloatBuffer JavaDoc)(new ByteBufferAsFloatBufferB JavaDoc(this,
470                                  -1,
471                                  0,
472                                  size,
473                                  size,
474                                  off))
475         : (FloatBuffer JavaDoc)(new ByteBufferAsFloatBufferL JavaDoc(this,
476                                  -1,
477                                  0,
478                                  size,
479                                  size,
480                                  off)));
481     }
482
483
484     // double
485

486
487
488     public double getDouble() {
489     return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian);
490     }
491
492     public double getDouble(int i) {
493     return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian);
494     }
495
496
497
498     public ByteBuffer JavaDoc putDouble(double x) {
499
500     Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian);
501     return this;
502
503
504
505     }
506
507     public ByteBuffer JavaDoc putDouble(int i, double x) {
508
509     Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian);
510     return this;
511
512
513
514     }
515
516     public DoubleBuffer JavaDoc asDoubleBuffer() {
517     int size = this.remaining() >> 3;
518     int off = offset + position();
519     return (bigEndian
520         ? (DoubleBuffer JavaDoc)(new ByteBufferAsDoubleBufferB JavaDoc(this,
521                                    -1,
522                                    0,
523                                    size,
524                                    size,
525                                    off))
526         : (DoubleBuffer JavaDoc)(new ByteBufferAsDoubleBufferL JavaDoc(this,
527                                    -1,
528                                    0,
529                                    size,
530                                    size,
531                                    off)));
532     }
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573 }
574
Popular Tags