KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > bytes > TCByteBufferJDK14


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.bytes;
5
6 import java.nio.ByteBuffer JavaDoc;
7
8 /**
9  * @author teck A thin wrapper to a real java.nio.ByteBuffer instance
10  */

11
12 // XXX: Should we wrap the native java.nio overflow, underflow and readOnly exceptions with the TC versions?
13
// This would make the TCByteBuffer interface consistent w.r.t. exceptions (whilst being blind to JDK13 vs JDK14)
14
class TCByteBufferJDK14 extends AbstractTCByteBuffer {
15
16   private final ByteBuffer JavaDoc buffer;
17   private final TCByteBufferJDK14 root;
18
19   TCByteBufferJDK14(int capacity, boolean direct) {
20     if (direct) {
21       buffer = ByteBuffer.allocateDirect(capacity);
22     } else {
23       buffer = ByteBuffer.allocate(capacity);
24     }
25     root = this;
26   }
27
28   private TCByteBufferJDK14(ByteBuffer JavaDoc buf) {
29     buffer = buf;
30     this.root = null;
31   }
32
33   private TCByteBufferJDK14(ByteBuffer JavaDoc buf, TCByteBufferJDK14 root) {
34     buffer = buf;
35     this.root = root;
36   }
37
38   static TCByteBufferJDK14 wrap(byte[] data) {
39     return new TCByteBufferJDK14(ByteBuffer.wrap(data));
40   }
41
42   protected ByteBuffer JavaDoc getBuffer() {
43     return buffer;
44   }
45
46   public TCByteBuffer clear() {
47     buffer.clear();
48     return this;
49   }
50
51   public int capacity() {
52     return buffer.capacity();
53   }
54
55   public int position() {
56     return buffer.position();
57   }
58
59   public TCByteBuffer flip() {
60     buffer.flip();
61     return this;
62   }
63
64   public boolean hasRemaining() {
65     return buffer.hasRemaining();
66   }
67
68   public int limit() {
69     return buffer.limit();
70   }
71
72   public TCByteBuffer limit(int newLimit) {
73     buffer.limit(newLimit);
74     return this;
75   }
76
77   public TCByteBuffer position(int newPosition) {
78     buffer.position(newPosition);
79     return this;
80   }
81
82   public int remaining() {
83     return buffer.remaining();
84   }
85
86   public com.tc.bytes.TCByteBuffer rewind() {
87     buffer.rewind();
88     return this;
89   }
90
91   public boolean isNioBuffer() {
92     return true;
93   }
94
95   public Object JavaDoc getNioBuffer() {
96     return buffer;
97   }
98
99   public boolean isDirect() {
100     return buffer.isDirect();
101   }
102
103   public byte[] array() {
104     return buffer.array();
105   }
106
107   public byte get() {
108     return buffer.get();
109   }
110
111   public boolean getBoolean() {
112     // XXX: Um-- why isn't there a getBoolean in ByteBuffer?
113
return buffer.get() > 0;
114   }
115
116   public boolean getBoolean(int index) {
117     return buffer.get(index) > 0;
118   }
119
120   public char getChar() {
121     return buffer.getChar();
122   }
123
124   public char getChar(int index) {
125     return buffer.getChar(index);
126   }
127
128   public double getDouble() {
129     return buffer.getDouble();
130   }
131
132   public double getDouble(int index) {
133     return buffer.getDouble(index);
134   }
135
136   public float getFloat() {
137     return buffer.getFloat();
138   }
139
140   public float getFloat(int index) {
141     return buffer.getFloat(index);
142   }
143
144   public int getInt() {
145     return buffer.getInt();
146   }
147
148   public int getInt(int index) {
149     return buffer.getInt(index);
150   }
151
152   public long getLong() {
153     return buffer.getLong();
154   }
155
156   public long getLong(int index) {
157     return buffer.getLong(index);
158   }
159
160   public short getShort() {
161     return buffer.getShort();
162   }
163
164   public short getShort(int index) {
165     return buffer.getShort(index);
166   }
167
168   public TCByteBuffer get(byte[] dst) {
169     buffer.get(dst);
170     return this;
171   }
172
173   public TCByteBuffer get(byte[] dst, int offset, int length) {
174     buffer.get(dst, offset, length);
175     return this;
176   }
177
178   public byte get(int index) {
179     return buffer.get(index);
180   }
181
182   public TCByteBuffer put(byte b) {
183     buffer.put(b);
184     return this;
185   }
186
187   public TCByteBuffer put(byte[] src) {
188     buffer.put(src);
189     return this;
190   }
191
192   public TCByteBuffer put(byte[] src, int offset, int length) {
193     buffer.put(src, offset, length);
194     return this;
195   }
196
197   public TCByteBuffer put(int index, byte b) {
198     buffer.put(index, b);
199     return this;
200   }
201
202   public TCByteBuffer putBoolean(boolean b) {
203     // XXX: Why isn't there a putBoolean in ByteBuffer?
204
buffer.put((b) ? (byte) 1 : (byte) 0);
205     return this;
206   }
207
208   public TCByteBuffer putBoolean(int index, boolean b) {
209     buffer.put(index, (b) ? (byte) 1 : (byte) 0);
210     return this;
211   }
212
213   public TCByteBuffer putChar(char c) {
214     buffer.putChar(c);
215     return this;
216   }
217
218   public TCByteBuffer putChar(int index, char c) {
219     buffer.putChar(index, c);
220     return this;
221   }
222
223   public TCByteBuffer putDouble(double d) {
224     buffer.putDouble(d);
225     return this;
226   }
227
228   public TCByteBuffer putDouble(int index, double d) {
229     buffer.putDouble(index, d);
230     return this;
231   }
232
233   public TCByteBuffer putFloat(float f) {
234     buffer.putFloat(f);
235     return this;
236   }
237
238   public TCByteBuffer putFloat(int index, float f) {
239     buffer.putFloat(index, f);
240     return this;
241   }
242
243   public TCByteBuffer putInt(int i) {
244     buffer.putInt(i);
245     return this;
246   }
247
248   public TCByteBuffer putInt(int index, int i) {
249     buffer.putInt(index, i);
250     return this;
251   }
252
253   public TCByteBuffer putLong(long l) {
254     buffer.putLong(l);
255     return this;
256   }
257
258   public TCByteBuffer putLong(int index, long l) {
259     buffer.putLong(index, l);
260     return this;
261   }
262
263   public TCByteBuffer putShort(short s) {
264     buffer.putShort(s);
265     return this;
266   }
267
268   public TCByteBuffer putShort(int index, short s) {
269     buffer.putShort(index, s);
270     return this;
271   }
272
273   public TCByteBuffer duplicate() {
274     return new TCByteBufferJDK14(buffer.duplicate(), root);
275   }
276
277   public TCByteBuffer put(TCByteBuffer src) {
278     if (!src.isNioBuffer()) { throw new IllegalArgumentException JavaDoc("src buffer is not backed by a java.nio.ByteBuffer"); }
279
280     buffer.put((ByteBuffer JavaDoc) src.getNioBuffer());
281     return this;
282   }
283
284   public TCByteBuffer slice() {
285     return new TCByteBufferJDK14(buffer.slice(), root);
286   }
287
288   public int arrayOffset() {
289     return buffer.arrayOffset();
290   }
291
292   public TCByteBuffer asReadOnlyBuffer() {
293     return new TCByteBufferJDK14(buffer.asReadOnlyBuffer(), root);
294   }
295
296   public boolean isReadOnly() {
297     return buffer.isReadOnly();
298   }
299
300   public String JavaDoc toString() {
301     return (buffer == null) ? "TCByteBufferJDK14(null buffer)" : "TCByteBufferJDK14@" + System.identityHashCode(this)
302                                                                  + "(" + buffer.toString() + ")";
303   }
304
305   public boolean hasArray() {
306     return buffer.hasArray();
307   }
308
309   // Can be called only once on any of the views and the root is gone
310
public void recycle() {
311     if (root != null) {
312       TCByteBufferFactory.returnBuffer(root.reInit());
313     }
314   }
315
316   private TCByteBufferJDK14 reInit() {
317     clear();
318     return this;
319   }
320
321   /* This is the debug version. PLEASE DONT DELETE */
322
323   // private static final TCLogger logger = TCLogging.getLogger(TCByteBufferJDK14.class);
324
//
325
// private final ByteBuffer buffer;
326
// private final TCByteBufferJDK14 root;
327
// private List childs;
328
// private static final boolean debug = true;
329
// private static final boolean debugFinalize = false;
330
// private ActivityMonitor monitor;
331
//
332
// TCByteBufferJDK14(int capacity, boolean direct) {
333
// if (direct) {
334
// buffer = ByteBuffer.allocateDirect(capacity);
335
// } else {
336
// buffer = ByteBuffer.allocate(capacity);
337
// }
338
// root = this;
339
// if (debug) {
340
// childs = new ArrayList();
341
// monitor = new ActivityMonitor();
342
// monitor.addActivity("TCBB", "Created");
343
// }
344
// }
345
//
346
// private TCByteBufferJDK14(ByteBuffer buf) {
347
// buffer = buf;
348
// this.root = null;
349
// if (debug) childs = new ArrayList();
350
// }
351
//
352
// private TCByteBufferJDK14(ByteBuffer buf, TCByteBufferJDK14 root) {
353
// buffer = buf;
354
// childs = null;
355
// this.root = root;
356
// if (debug) this.root.addChild(this);
357
// }
358
//
359
// private void addChild(TCByteBufferJDK14 child) {
360
// if (debug) childs.add(child);
361
// }
362
//
363
// static TCByteBufferJDK14 wrap(byte[] data) {
364
// return new TCByteBufferJDK14(ByteBuffer.wrap(data));
365
// }
366
//
367
// protected ByteBuffer getBuffer() {
368
// if (debug) {
369
// checkState();
370
// logGet();
371
// }
372
// return buffer;
373
// }
374
//
375
// public TCByteBuffer clear() {
376
// buffer.clear();
377
// if (debug) {
378
// childs.clear();
379
// monitor.clear();
380
// }
381
// return this;
382
// }
383
//
384
// public int capacity() {
385
// if (debug) checkState();
386
// return buffer.capacity();
387
// }
388
//
389
// public int position() {
390
// if (debug) checkState();
391
// return buffer.position();
392
// }
393
//
394
// public TCByteBuffer flip() {
395
// if (debug) checkState();
396
// buffer.flip();
397
// return this;
398
// }
399
//
400
// private void checkState() {
401
// if (debug && this != root) {
402
// // This doesnt check for the root itself, I dont know how to check for the root itself being modified once check
403
// // back in
404
// Assert.assertNotNull(root);
405
// Assert.assertTrue(root.isChild(this));
406
// }
407
// }
408
//
409
// private boolean isChild(TCByteBufferJDK14 child) {
410
// return childs.contains(child);
411
// }
412
//
413
// public boolean hasRemaining() {
414
// if (debug) checkState();
415
// return buffer.hasRemaining();
416
// }
417
//
418
// public int limit() {
419
// if (debug) checkState();
420
// return buffer.limit();
421
// }
422
//
423
// public TCByteBuffer limit(int newLimit) {
424
// if (debug) checkState();
425
// buffer.limit(newLimit);
426
// return this;
427
// }
428
//
429
// public TCByteBuffer position(int newPosition) {
430
// if (debug) checkState();
431
// buffer.position(newPosition);
432
// return this;
433
// }
434
//
435
// public int remaining() {
436
// if (debug) checkState();
437
// return buffer.remaining();
438
// }
439
//
440
// public com.tc.bytes.TCByteBuffer rewind() {
441
// if (debug) checkState();
442
// buffer.rewind();
443
// return this;
444
// }
445
//
446
// public boolean isNioBuffer() {
447
// return true;
448
// }
449
//
450
// public Object getNioBuffer() {
451
// if (debug) checkState();
452
// return buffer;
453
// }
454
//
455
// public boolean isDirect() {
456
// return buffer.isDirect();
457
// }
458
//
459
// public byte[] array() {
460
// // Not fool proof
461
// if (debug) checkState();
462
// return buffer.array();
463
// }
464
//
465
// public byte get() {
466
// if (debug) {
467
// checkState();
468
// logGet();
469
// }
470
// return buffer.get();
471
// }
472
//
473
// public boolean getBoolean() {
474
// // XXX: Um-- why isn't there a getBoolean in ByteBuffer?
475
// if (debug) {
476
// checkState();
477
// logGet();
478
// }
479
// return buffer.get() > 0;
480
// }
481
//
482
// public boolean getBoolean(int index) {
483
// if (debug) {
484
// checkState();
485
// logGet();
486
// }
487
// return buffer.get(index) > 0;
488
// }
489
//
490
// public char getChar() {
491
// if (debug) {
492
// checkState();
493
// logGet();
494
// }
495
// return buffer.getChar();
496
// }
497
//
498
// public char getChar(int index) {
499
// if (debug) {
500
// checkState();
501
// logGet();
502
// }
503
// return buffer.getChar(index);
504
// }
505
//
506
// public double getDouble() {
507
// if (debug) {
508
// checkState();
509
// logGet();
510
// }
511
// return buffer.getDouble();
512
// }
513
//
514
// public double getDouble(int index) {
515
// if (debug) {
516
// checkState();
517
// logGet();
518
// }
519
// return buffer.getDouble(index);
520
// }
521
//
522
// public float getFloat() {
523
// if (debug) {
524
// checkState();
525
// logGet();
526
// }
527
// return buffer.getFloat();
528
// }
529
//
530
// public float getFloat(int index) {
531
// if (debug) {
532
// checkState();
533
// logGet();
534
// }
535
// return buffer.getFloat(index);
536
// }
537
//
538
// public int getInt() {
539
// if (debug) {
540
// checkState();
541
// logGet();
542
// }
543
// return buffer.getInt();
544
// }
545
//
546
// public int getInt(int index) {
547
// if (debug) {
548
// checkState();
549
// logGet();
550
// }
551
// return buffer.getInt(index);
552
// }
553
//
554
// public long getLong() {
555
// if (debug) {
556
// checkState();
557
// logGet();
558
// }
559
// return buffer.getLong();
560
// }
561
//
562
// public long getLong(int index) {
563
// if (debug) {
564
// checkState();
565
// logGet();
566
// }
567
// return buffer.getLong(index);
568
// }
569
//
570
// public short getShort() {
571
// if (debug) {
572
// checkState();
573
// logGet();
574
// }
575
// return buffer.getShort();
576
// }
577
//
578
// public short getShort(int index) {
579
// if (debug) {
580
// checkState();
581
// logGet();
582
// }
583
// return buffer.getShort(index);
584
// }
585
//
586
// public TCByteBuffer get(byte[] dst) {
587
// if (debug) {
588
// checkState();
589
// logGet();
590
// }
591
// buffer.get(dst);
592
// return this;
593
// }
594
//
595
// public TCByteBuffer get(byte[] dst, int offset, int length) {
596
// if (debug) {
597
// checkState();
598
// logGet();
599
// }
600
// buffer.get(dst, offset, length);
601
// return this;
602
// }
603
//
604
// public byte get(int index) {
605
// if (debug) {
606
// checkState();
607
// logGet();
608
// }
609
// return buffer.get(index);
610
// }
611
//
612
// public TCByteBuffer put(byte b) {
613
// if (debug) {
614
// checkState();
615
// logPut();
616
// }
617
// buffer.put(b);
618
// return this;
619
// }
620
//
621
// public TCByteBuffer put(byte[] src) {
622
// if (debug) {
623
// checkState();
624
// logPut();
625
// }
626
// buffer.put(src);
627
// return this;
628
// }
629
//
630
// public TCByteBuffer put(byte[] src, int offset, int length) {
631
// if (debug) {
632
// checkState();
633
// logPut();
634
// }
635
// buffer.put(src, offset, length);
636
// return this;
637
// }
638
//
639
// public TCByteBuffer put(int index, byte b) {
640
// if (debug) {
641
// checkState();
642
// logPut();
643
// }
644
// buffer.put(index, b);
645
// return this;
646
// }
647
//
648
// public TCByteBuffer putBoolean(boolean b) {
649
// // XXX: Why isn't there a putBoolean in ByteBuffer?
650
// if (debug) {
651
// checkState();
652
// logPut();
653
// }
654
// buffer.put((b) ? (byte) 1 : (byte) 0);
655
// return this;
656
// }
657
//
658
// public TCByteBuffer putBoolean(int index, boolean b) {
659
// if (debug) {
660
// checkState();
661
// logPut();
662
// }
663
// buffer.put(index, (b) ? (byte) 1 : (byte) 0);
664
// return this;
665
// }
666
//
667
// public TCByteBuffer putChar(char c) {
668
// if (debug) {
669
// checkState();
670
// logPut();
671
// }
672
// buffer.putChar(c);
673
// return this;
674
// }
675
//
676
// public TCByteBuffer putChar(int index, char c) {
677
// if (debug) {
678
// checkState();
679
// logPut();
680
// }
681
// buffer.putChar(index, c);
682
// return this;
683
// }
684
//
685
// public TCByteBuffer putDouble(double d) {
686
// if (debug) {
687
// checkState();
688
// logPut();
689
// }
690
// buffer.putDouble(d);
691
// return this;
692
// }
693
//
694
// public TCByteBuffer putDouble(int index, double d) {
695
// if (debug) {
696
// checkState();
697
// logPut();
698
// }
699
// buffer.putDouble(index, d);
700
// return this;
701
// }
702
//
703
// public TCByteBuffer putFloat(float f) {
704
// if (debug) {
705
// checkState();
706
// logPut();
707
// }
708
// buffer.putFloat(f);
709
// return this;
710
// }
711
//
712
// public TCByteBuffer putFloat(int index, float f) {
713
// if (debug) {
714
// checkState();
715
// logPut();
716
// }
717
// buffer.putFloat(index, f);
718
// return this;
719
// }
720
//
721
// public TCByteBuffer putInt(int i) {
722
// if (debug) {
723
// checkState();
724
// logPut();
725
// }
726
// buffer.putInt(i);
727
// return this;
728
// }
729
//
730
// public TCByteBuffer putInt(int index, int i) {
731
// if (debug) {
732
// checkState();
733
// logPut();
734
// }
735
// buffer.putInt(index, i);
736
// return this;
737
// }
738
//
739
// public TCByteBuffer putLong(long l) {
740
// if (debug) {
741
// checkState();
742
// logPut();
743
// }
744
// buffer.putLong(l);
745
// return this;
746
// }
747
//
748
// public TCByteBuffer putLong(int index, long l) {
749
// if (debug) checkState();
750
// logPut();
751
// buffer.putLong(index, l);
752
// return this;
753
// }
754
//
755
// public TCByteBuffer putShort(short s) {
756
// if (debug) {
757
// checkState();
758
// logPut();
759
// }
760
// buffer.putShort(s);
761
// return this;
762
// }
763
//
764
// public TCByteBuffer putShort(int index, short s) {
765
// if (debug) {
766
// checkState();
767
// logPut();
768
// }
769
// buffer.putShort(index, s);
770
// return this;
771
// }
772
//
773
// public TCByteBuffer duplicate() {
774
// if (debug) checkState();
775
// return new TCByteBufferJDK14(buffer.duplicate(), root);
776
// }
777
//
778
// public TCByteBuffer put(TCByteBuffer src) {
779
// if (debug) {
780
// checkState();
781
// logPut();
782
// }
783
//
784
// if (!src.isNioBuffer()) { throw new IllegalArgumentException("src buffer is not backed by a java.nio.ByteBuffer");
785
// }
786
//
787
// buffer.put((ByteBuffer) src.getNioBuffer());
788
// return this;
789
// }
790
//
791
// public TCByteBuffer slice() {
792
// if (debug) checkState();
793
// return new TCByteBufferJDK14(buffer.slice(), root);
794
// }
795
//
796
// public int arrayOffset() {
797
// if (debug) checkState();
798
// return buffer.arrayOffset();
799
// }
800
//
801
// public TCByteBuffer asReadOnlyBuffer() {
802
// if (debug) checkState();
803
// return new TCByteBufferJDK14(buffer.asReadOnlyBuffer(), root);
804
// }
805
//
806
// public boolean isReadOnly() {
807
// if (debug) checkState();
808
// return buffer.isReadOnly();
809
// }
810
//
811
// public String toString() {
812
// if (debug) checkState();
813
// return (buffer == null) ? "null buffer" : buffer.toString();
814
// }
815
//
816
// public boolean hasArray() {
817
// if (debug) checkState();
818
// return buffer.hasArray();
819
// }
820
//
821
// // Can be called only once on any of the views and the root is gone
822
// public void recycle() {
823
// if (debug) checkState();
824
// if(root != null) TCByteBufferFactory.returnBuffer(root.reInit());
825
// }
826
//
827
// private TCByteBufferJDK14 reInit() {
828
// clear();
829
// return this;
830
// }
831
//
832
// void logGet() {
833
// if(root !=null) {
834
// root.monitor.clear();
835
// root.monitor.addActivity("TCBB", "get");
836
// }
837
// }
838
//
839
// void logPut() {
840
// if(root !=null) {
841
// root.monitor.clear();
842
// root.monitor.addActivity("TCBB", "put");
843
// }
844
// }
845
//
846
// static int count;
847
// static {
848
// CommonShutDownHook.addShutdownHook(new Runnable() {
849
// public void run() {
850
// logger.info("No of Root Buffers finalized = " + count);
851
// }
852
// });
853
// }
854
//
855
// public void finalize() {
856
// if (this == root) {
857
// count++;
858
// if (debugFinalize) monitor.printActivityFor("TCBB");
859
// }
860
// }
861
}
Popular Tags