KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.tc.exception.ImplementMe;
7
8 /**
9  * @author teck A JDK-1.3 compatible version of java.nio.ByteBuffer
10  */

11 class TCByteBufferJDK13 extends AbstractTCByteBuffer {
12   private final int _offset;
13   private final int _capacity;
14   private int _limit;
15   private int _position;
16   private final byte _data[];
17   private final boolean _readOnly;
18
19   TCByteBufferJDK13(final int capacity, final boolean direct) {
20     // "direct" parameter has no meaning here (yet)
21
this(capacity);
22   }
23
24   private TCByteBufferJDK13(final int capacity) {
25     this(new byte[capacity]);
26   }
27
28   TCByteBufferJDK13(byte[] data) {
29     _data = data;
30     _capacity = data.length;
31     _limit = _capacity;
32     _position = 0;
33     _offset = 0;
34     _readOnly = false;
35   }
36
37   private TCByteBufferJDK13(final byte data[], final int limit, final int position, final int offset, final boolean readOnly) {
38     _offset = offset;
39     _capacity = data.length - _offset;
40     _data = data;
41     _limit = limit;
42     _position = position;
43     _readOnly = readOnly;
44   }
45
46   public int capacity() {
47     return _capacity;
48   }
49
50   public TCByteBuffer clear() {
51     _limit = _data.length;
52     _position = _offset;
53     return this;
54   }
55
56   public int position() {
57     return _position - _offset;
58   }
59
60   public TCByteBuffer flip() {
61     _limit = _position;
62     _position = _offset;
63     return this;
64   }
65
66   public boolean hasRemaining() {
67     return _position < _limit;
68   }
69
70   public int limit() {
71     return _limit - _offset;
72   }
73
74   public TCByteBuffer limit(final int newLimit) {
75     if ((newLimit > _capacity) || (newLimit < 0)) { throw new IllegalArgumentException JavaDoc(); }
76
77     _limit = newLimit + _offset;
78     if (_position > _limit) {
79       _position = _limit;
80     }
81
82     return this;
83   }
84
85   public TCByteBuffer position(final int newPosition) {
86     if ((newPosition > _limit) || (newPosition < 0)) throw new IllegalArgumentException JavaDoc();
87     _position = newPosition + _offset;
88
89     return this;
90   }
91
92   public int remaining() {
93     return _limit - _position;
94   }
95
96   public TCByteBuffer rewind() {
97     _position = _offset;
98     return this;
99   }
100
101   public boolean isNioBuffer() {
102     return false;
103   }
104
105   public Object JavaDoc getNioBuffer() {
106     throw new UnsupportedOperationException JavaDoc("This buffer does not provide a backing java.nio.ByteBuffer");
107   }
108
109   public boolean isDirect() {
110     return false;
111   }
112
113   public byte[] array() {
114     return _data;
115   }
116
117   public byte get() {
118     if (_position < _limit) {
119       byte rv = _data[_position];
120       _position++;
121       return rv;
122     }
123     throw new TCBufferUnderflowException();
124   }
125
126   public TCByteBuffer get(final byte[] dst) {
127     return get(dst, 0, dst.length);
128   }
129
130   public TCByteBuffer get(final byte[] dst, final int offset, final int length) {
131     if (length > remaining()) { throw new TCBufferUnderflowException(); }
132
133     System.arraycopy(_data, _position, dst, offset, length);
134     _position += length;
135     return this;
136   }
137
138   public byte get(final int index) {
139     if ((index < 0) || (index > limit())) { throw new IndexOutOfBoundsException JavaDoc(); }
140
141     return _data[index];
142   }
143
144   public int getInt() {
145     throw new ImplementMe();
146   }
147
148   public long getLong() {
149     throw new ImplementMe();
150   }
151
152   public TCByteBuffer put(final byte b) {
153     if (_readOnly) { throw new TCReadOnlyBufferException(); }
154
155     if (_position < _limit) {
156       _data[_position] = b;
157       _position++;
158       return this;
159     }
160
161     throw new TCBufferOverflowException();
162   }
163
164   public TCByteBuffer put(final byte[] src) {
165     if (_readOnly) { throw new TCReadOnlyBufferException(); }
166
167     return put(src, 0, src.length);
168   }
169
170   public TCByteBuffer put(final byte[] src, final int offset, final int length) {
171     if (_readOnly) { throw new TCReadOnlyBufferException(); }
172
173     if (length > remaining()) { throw new TCBufferOverflowException(); }
174
175     System.arraycopy(src, offset, _data, _position, length);
176     _position += length;
177
178     return this;
179   }
180
181   public TCByteBuffer put(final int index, final byte b) {
182     if (_readOnly) { throw new TCReadOnlyBufferException(); }
183
184     if ((index < 0) || (index > limit())) { throw new IndexOutOfBoundsException JavaDoc(); }
185
186     _data[index] = b;
187
188     return this;
189   }
190
191   public TCByteBuffer duplicate() {
192     return new TCByteBufferJDK13(_data, _limit, _position, _offset, _readOnly);
193   }
194
195   public TCByteBuffer put(TCByteBuffer src) {
196     if (src.remaining() > remaining()) { throw new TCBufferOverflowException(); }
197
198     if (src == this) { throw new IllegalArgumentException JavaDoc("Source buffer can not be this buffer"); }
199
200     if (_readOnly) { throw new TCReadOnlyBufferException(); }
201
202     int length = src.remaining();
203
204     System.arraycopy(src.array(), src.position() + src.arrayOffset(), _data, _position, length);
205     src.position(src.position() + length);
206
207     _position += length;
208
209     return this;
210   }
211
212   public TCByteBuffer putInt(int i) {
213
214     throw new ImplementMe();
215   }
216
217   public TCByteBuffer putLong(long l) {
218
219     throw new ImplementMe();
220   }
221
222   public TCByteBuffer slice() {
223     final int newLimit = _position + remaining();
224
225     return new TCByteBufferJDK13(_data, newLimit, _position, _position, _readOnly);
226   }
227
228   public int arrayOffset() {
229     return _offset;
230   }
231
232   public TCByteBuffer asReadOnlyBuffer() {
233     return new TCByteBufferJDK13(_data, _limit, _position, _offset, true);
234   }
235
236   public boolean isReadOnly() {
237     return _readOnly;
238   }
239
240   /*
241    * (non-Javadoc)
242    *
243    * @see com.tc.bytes.TCByteBuffer#putChar(char)
244    */

245   public TCByteBuffer putChar(char c) {
246
247     throw new ImplementMe();
248   }
249
250   /*
251    * (non-Javadoc)
252    *
253    * @see com.tc.bytes.TCByteBuffer#putFloat(float)
254    */

255   public TCByteBuffer putFloat(float f) {
256
257     throw new ImplementMe();
258   }
259
260   /*
261    * (non-Javadoc)
262    *
263    * @see com.tc.bytes.TCByteBuffer#putDouble(double)
264    */

265   public TCByteBuffer putDouble(double d) {
266
267     throw new ImplementMe();
268   }
269
270   /*
271    * (non-Javadoc)
272    *
273    * @see com.tc.bytes.TCByteBuffer#put(int, char)
274    */

275   public TCByteBuffer putChar(int index, char c) {
276
277     throw new ImplementMe();
278   }
279
280   /*
281    * (non-Javadoc)
282    *
283    * @see com.tc.bytes.TCByteBuffer#putInt(int, int)
284    */

285   public TCByteBuffer putInt(int index, int i) {
286
287     throw new ImplementMe();
288   }
289
290   /*
291    * (non-Javadoc)
292    *
293    * @see com.tc.bytes.TCByteBuffer#putLong(int, long)
294    */

295   public TCByteBuffer putLong(int index, long l) {
296
297     throw new ImplementMe();
298   }
299
300   /*
301    * (non-Javadoc)
302    *
303    * @see com.tc.bytes.TCByteBuffer#putFloat(int, float)
304    */

305   public TCByteBuffer putFloat(int index, float f) {
306
307     throw new ImplementMe();
308   }
309
310   /*
311    * (non-Javadoc)
312    *
313    * @see com.tc.bytes.TCByteBuffer#putDouble(int, double)
314    */

315   public TCByteBuffer putDouble(int index, double d) {
316
317     throw new ImplementMe();
318   }
319
320   /*
321    * (non-Javadoc)
322    *
323    * @see com.tc.bytes.TCByteBuffer#getBoolean()
324    */

325   public boolean getBoolean() {
326
327     throw new ImplementMe();
328   }
329
330   /*
331    * (non-Javadoc)
332    *
333    * @see com.tc.bytes.TCByteBuffer#getBoolean(int)
334    */

335   public boolean getBoolean(int index) {
336
337     throw new ImplementMe();
338   }
339
340   /*
341    * (non-Javadoc)
342    *
343    * @see com.tc.bytes.TCByteBuffer#getChar()
344    */

345   public char getChar() {
346
347     throw new ImplementMe();
348   }
349
350   /*
351    * (non-Javadoc)
352    *
353    * @see com.tc.bytes.TCByteBuffer#getChar(int)
354    */

355   public char getChar(int index) {
356
357     throw new ImplementMe();
358   }
359
360   /*
361    * (non-Javadoc)
362    *
363    * @see com.tc.bytes.TCByteBuffer#getDouble()
364    */

365   public double getDouble() {
366
367     throw new ImplementMe();
368   }
369
370   /*
371    * (non-Javadoc)
372    *
373    * @see com.tc.bytes.TCByteBuffer#getDouble(int)
374    */

375   public double getDouble(int index) {
376
377     throw new ImplementMe();
378   }
379
380   /*
381    * (non-Javadoc)
382    *
383    * @see com.tc.bytes.TCByteBuffer#getFloat()
384    */

385   public float getFloat() {
386
387     throw new ImplementMe();
388   }
389
390   /*
391    * (non-Javadoc)
392    *
393    * @see com.tc.bytes.TCByteBuffer#getFloat(int)
394    */

395   public float getFloat(int index) {
396
397     throw new ImplementMe();
398   }
399
400   /*
401    * (non-Javadoc)
402    *
403    * @see com.tc.bytes.TCByteBuffer#getInt(int)
404    */

405   public int getInt(int index) {
406
407     throw new ImplementMe();
408   }
409
410   /*
411    * (non-Javadoc)
412    *
413    * @see com.tc.bytes.TCByteBuffer#getLong(int)
414    */

415   public long getLong(int index) {
416
417     throw new ImplementMe();
418   }
419
420   /*
421    * (non-Javadoc)
422    *
423    * @see com.tc.bytes.TCByteBuffer#getShort()
424    */

425   public short getShort() {
426
427     throw new ImplementMe();
428   }
429
430   /*
431    * (non-Javadoc)
432    *
433    * @see com.tc.bytes.TCByteBuffer#getShort(int)
434    */

435   public short getShort(int index) {
436
437     throw new ImplementMe();
438   }
439
440   /*
441    * (non-Javadoc)
442    *
443    * @see com.tc.bytes.TCByteBuffer#putBoolean(boolean)
444    */

445   public TCByteBuffer putBoolean(boolean b) {
446
447     throw new ImplementMe();
448   }
449
450   /*
451    * (non-Javadoc)
452    *
453    * @see com.tc.bytes.TCByteBuffer#putBoolean(int, boolean)
454    */

455   public TCByteBuffer putBoolean(int index, boolean b) {
456
457     throw new ImplementMe();
458   }
459
460   /*
461    * (non-Javadoc)
462    *
463    * @see com.tc.bytes.TCByteBuffer#putShort(short)
464    */

465   public TCByteBuffer putShort(short s) {
466
467     throw new ImplementMe();
468   }
469
470   /*
471    * (non-Javadoc)
472    *
473    * @see com.tc.bytes.TCByteBuffer#putShort(int, short)
474    */

475   public TCByteBuffer putShort(int index, short s) {
476
477     throw new ImplementMe();
478   }
479
480   public boolean hasArray() {
481     return true;
482   }
483
484   public void recycle() {
485     // no op
486
}
487
488 }
Popular Tags