1 | /* |
2 | * @(#) $Id: ByteBufferProxy.java 327113 2005-10-21 06:59:15Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */package org.apache.mina.common; |
19 | |
20 | import java.io.FilterOutputStream; |
21 | import java.nio.ByteOrder; |
22 | import java.nio.CharBuffer; |
23 | import java.nio.DoubleBuffer; |
24 | import java.nio.FloatBuffer; |
25 | import java.nio.IntBuffer; |
26 | import java.nio.LongBuffer; |
27 | import java.nio.ShortBuffer; |
28 | import java.nio.charset.CharacterCodingException; |
29 | import java.nio.charset.CharsetDecoder; |
30 | import java.nio.charset.CharsetEncoder; |
31 | |
32 | /** |
33 | * A {@link ByteBuffer} that wraps a buffer and proxies any operations to it. |
34 | * <p> |
35 | * You can think this class like a {@link FilterOutputStream}. All operations |
36 | * are proxied by default so that you can extend this class and override existing |
37 | * operations selectively. You can introduce new operations, too. |
38 | * |
39 | * @author The Apache Directory Project (dev@directory.apache.org) |
40 | * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $ |
41 | */ |
42 | public class ByteBufferProxy extends ByteBuffer |
43 | { |
44 | |
45 | /** |
46 | * The buffer proxied by this proxy. |
47 | */ |
48 | protected ByteBuffer buf; |
49 | |
50 | /** |
51 | * Create a new instance. |
52 | * @param buf the buffer to be proxied |
53 | */ |
54 | protected ByteBufferProxy( ByteBuffer buf ) |
55 | { |
56 | if( buf == null ) |
57 | { |
58 | throw new NullPointerException( "buf" ); |
59 | } |
60 | this.buf = buf; |
61 | } |
62 | |
63 | public void acquire() |
64 | { |
65 | buf.acquire(); |
66 | } |
67 | |
68 | public void release() |
69 | { |
70 | buf.release(); |
71 | } |
72 | |
73 | public boolean isDirect() |
74 | { |
75 | return buf.isDirect(); |
76 | } |
77 | |
78 | public java.nio.ByteBuffer buf() |
79 | { |
80 | return buf.buf(); |
81 | } |
82 | |
83 | public int capacity() |
84 | { |
85 | return buf.capacity(); |
86 | } |
87 | |
88 | public int position() |
89 | { |
90 | return buf.position(); |
91 | } |
92 | |
93 | public ByteBuffer position( int newPosition ) |
94 | { |
95 | buf.position( newPosition ); |
96 | return this; |
97 | } |
98 | |
99 | public int limit() |
100 | { |
101 | return buf.limit(); |
102 | } |
103 | |
104 | public ByteBuffer limit( int newLimit ) |
105 | { |
106 | buf.limit( newLimit ); |
107 | return this; |
108 | } |
109 | |
110 | public ByteBuffer mark() |
111 | { |
112 | buf.mark(); |
113 | return this; |
114 | } |
115 | |
116 | public ByteBuffer reset() |
117 | { |
118 | buf.reset(); |
119 | return this; |
120 | } |
121 | |
122 | public ByteBuffer clear() |
123 | { |
124 | buf.clear(); |
125 | return this; |
126 | } |
127 | |
128 | public ByteBuffer flip() |
129 | { |
130 | buf.flip(); |
131 | return this; |
132 | } |
133 | |
134 | public ByteBuffer rewind() |
135 | { |
136 | buf.rewind(); |
137 | return this; |
138 | } |
139 | |
140 | public int remaining() |
141 | { |
142 | return buf.remaining(); |
143 | } |
144 | |
145 | public boolean hasRemaining() |
146 | { |
147 | return buf.hasRemaining(); |
148 | } |
149 | |
150 | public byte get() |
151 | { |
152 | return buf.get(); |
153 | } |
154 | |
155 | public short getUnsigned() |
156 | { |
157 | return buf.getUnsigned(); |
158 | } |
159 | |
160 | public ByteBuffer put( byte b ) |
161 | { |
162 | buf.put( b ); |
163 | return this; |
164 | } |
165 | |
166 | public byte get( int index ) |
167 | { |
168 | return buf.get( index ); |
169 | } |
170 | |
171 | public short getUnsigned( int index ) |
172 | { |
173 | return buf.getUnsigned( index ); |
174 | } |
175 | |
176 | public ByteBuffer put( int index, byte b ) |
177 | { |
178 | buf.put( index, b ); |
179 | return this; |
180 | } |
181 | |
182 | public ByteBuffer get( byte[] dst, int offset, int length ) |
183 | { |
184 | buf.get( dst, offset, length ); |
185 | return this; |
186 | } |
187 | |
188 | public ByteBuffer get( byte[] dst ) |
189 | { |
190 | buf.get( dst ); |
191 | return this; |
192 | } |
193 | |
194 | public ByteBuffer put( ByteBuffer src ) |
195 | { |
196 | buf.put( src ); |
197 | return this; |
198 | } |
199 | |
200 | public ByteBuffer put( java.nio.ByteBuffer src ) |
201 | { |
202 | buf.put( src ); |
203 | return this; |
204 | } |
205 | |
206 | public ByteBuffer put( byte[] src, int offset, int length ) |
207 | { |
208 | buf.put( src, offset, length ); |
209 | return this; |
210 | } |
211 | |
212 | public ByteBuffer put( byte[] src ) |
213 | { |
214 | buf.put( src ); |
215 | return this; |
216 | } |
217 | |
218 | public ByteBuffer compact() |
219 | { |
220 | buf.compact(); |
221 | return this; |
222 | } |
223 | |
224 | public String toString() |
225 | { |
226 | return buf.toString(); |
227 | } |
228 | |
229 | public int hashCode() |
230 | { |
231 | return buf.hashCode(); |
232 | } |
233 | |
234 | public boolean equals( Object ob ) |
235 | { |
236 | return buf.equals( ob ); |
237 | } |
238 | |
239 | public int compareTo( ByteBuffer that ) |
240 | { |
241 | return buf.compareTo( that ); |
242 | } |
243 | |
244 | public ByteOrder order() |
245 | { |
246 | return buf.order(); |
247 | } |
248 | |
249 | public ByteBuffer order( ByteOrder bo ) |
250 | { |
251 | buf.order( bo ); |
252 | return this; |
253 | } |
254 | |
255 | public char getChar() |
256 | { |
257 | return buf.getChar(); |
258 | } |
259 | |
260 | public ByteBuffer putChar( char value ) |
261 | { |
262 | buf.putChar( value ); |
263 | return this; |
264 | } |
265 | |
266 | public char getChar( int index ) |
267 | { |
268 | return buf.getChar( index ); |
269 | } |
270 | |
271 | public ByteBuffer putChar( int index, char value ) |
272 | { |
273 | buf.putChar( index, value ); |
274 | return this; |
275 | } |
276 | |
277 | public CharBuffer asCharBuffer() |
278 | { |
279 | return buf.asCharBuffer(); |
280 | } |
281 | |
282 | public short getShort() |
283 | { |
284 | return buf.getShort(); |
285 | } |
286 | |
287 | public int getUnsignedShort() |
288 | { |
289 | return buf.getUnsignedShort(); |
290 | } |
291 | |
292 | public ByteBuffer putShort( short value ) |
293 | { |
294 | buf.putShort( value ); |
295 | return this; |
296 | } |
297 | |
298 | public short getShort( int index ) |
299 | { |
300 | return buf.getShort( index ); |
301 | } |
302 | |
303 | public int getUnsignedShort( int index ) |
304 | { |
305 | return buf.getUnsignedShort( index ); |
306 | } |
307 | |
308 | public ByteBuffer putShort( int index, short value ) |
309 | { |
310 | buf.putShort( index, value ); |
311 | return this; |
312 | } |
313 | |
314 | public ShortBuffer asShortBuffer() |
315 | { |
316 | return buf.asShortBuffer(); |
317 | } |
318 | |
319 | public int getInt() |
320 | { |
321 | return buf.getInt(); |
322 | } |
323 | |
324 | public long getUnsignedInt() |
325 | { |
326 | return buf.getUnsignedInt(); |
327 | } |
328 | |
329 | public ByteBuffer putInt( int value ) |
330 | { |
331 | buf.putInt( value ); |
332 | return this; |
333 | } |
334 | |
335 | public int getInt( int index ) |
336 | { |
337 | return buf.getInt( index ); |
338 | } |
339 | |
340 | public long getUnsignedInt( int index ) |
341 | { |
342 | return buf.getUnsignedInt( index ); |
343 | } |
344 | |
345 | public ByteBuffer putInt( int index, int value ) |
346 | { |
347 | buf.putInt( index, value ); |
348 | return this; |
349 | } |
350 | |
351 | public IntBuffer asIntBuffer() |
352 | { |
353 | return buf.asIntBuffer(); |
354 | } |
355 | |
356 | public long getLong() |
357 | { |
358 | return buf.getLong(); |
359 | } |
360 | |
361 | public ByteBuffer putLong( long value ) |
362 | { |
363 | buf.putLong( value ); |
364 | return this; |
365 | } |
366 | |
367 | public long getLong( int index ) |
368 | { |
369 | return buf.getLong( index ); |
370 | } |
371 | |
372 | public ByteBuffer putLong( int index, long value ) |
373 | { |
374 | buf.putLong( index, value ); |
375 | return this; |
376 | } |
377 | |
378 | public LongBuffer asLongBuffer() |
379 | { |
380 | return buf.asLongBuffer(); |
381 | } |
382 | |
383 | public float getFloat() |
384 | { |
385 | return buf.getFloat(); |
386 | } |
387 | |
388 | public ByteBuffer putFloat( float value ) |
389 | { |
390 | buf.putFloat( value ); |
391 | return this; |
392 | } |
393 | |
394 | public float getFloat( int index ) |
395 | { |
396 | return buf.getFloat( index ); |
397 | } |
398 | |
399 | public ByteBuffer putFloat( int index, float value ) |
400 | { |
401 | buf.putFloat( index, value ); |
402 | return this; |
403 | } |
404 | |
405 | public FloatBuffer asFloatBuffer() |
406 | { |
407 | return buf.asFloatBuffer(); |
408 | } |
409 | |
410 | public double getDouble() |
411 | { |
412 | return buf.getDouble(); |
413 | } |
414 | |
415 | public ByteBuffer putDouble( double value ) |
416 | { |
417 | buf.putDouble( value ); |
418 | return this; |
419 | } |
420 | |
421 | public double getDouble( int index ) |
422 | { |
423 | return buf.getDouble( index ); |
424 | } |
425 | |
426 | public ByteBuffer putDouble( int index, double value ) |
427 | { |
428 | buf.putDouble( index, value ); |
429 | return this; |
430 | } |
431 | |
432 | public DoubleBuffer asDoubleBuffer() |
433 | { |
434 | return buf.asDoubleBuffer(); |
435 | } |
436 | |
437 | public String getHexDump() |
438 | { |
439 | return buf.getHexDump(); |
440 | } |
441 | |
442 | public String getString( int fieldSize, CharsetDecoder decoder ) |
443 | throws CharacterCodingException |
444 | { |
445 | return buf.getString( fieldSize, decoder ); |
446 | } |
447 | |
448 | public String getString( CharsetDecoder decoder ) |
449 | throws CharacterCodingException |
450 | { |
451 | return buf.getString( decoder ); |
452 | } |
453 | |
454 | public ByteBuffer putString( CharSequence in, int fieldSize, |
455 | CharsetEncoder encoder ) |
456 | throws CharacterCodingException |
457 | { |
458 | buf.putString( in, fieldSize, encoder ); |
459 | return this; |
460 | } |
461 | |
462 | public ByteBuffer putString( CharSequence in, CharsetEncoder encoder ) |
463 | throws CharacterCodingException |
464 | { |
465 | buf.putString( in, encoder ); |
466 | return this; |
467 | } |
468 | |
469 | public ByteBuffer skip( int size ) |
470 | { |
471 | buf.skip( size ); |
472 | return this; |
473 | } |
474 | |
475 | public ByteBuffer fill( byte value, int size ) |
476 | { |
477 | buf.fill( value, size ); |
478 | return this; |
479 | } |
480 | |
481 | public ByteBuffer fillAndReset( byte value, int size ) |
482 | { |
483 | buf.fillAndReset( value, size ); |
484 | return this; |
485 | } |
486 | |
487 | public ByteBuffer fill( int size ) |
488 | { |
489 | buf.fill( size ); |
490 | return this; |
491 | } |
492 | |
493 | public ByteBuffer fillAndReset( int size ) |
494 | { |
495 | buf.fillAndReset( size ); |
496 | return this; |
497 | } |
498 | |
499 | public boolean isAutoExpand() |
500 | { |
501 | return buf.isAutoExpand(); |
502 | } |
503 | |
504 | public ByteBuffer setAutoExpand( boolean autoExpand ) |
505 | { |
506 | buf.setAutoExpand( autoExpand ); |
507 | return this; |
508 | } |
509 | |
510 | public boolean isPooled() |
511 | { |
512 | return buf.isPooled(); |
513 | } |
514 | |
515 | public void setPooled( boolean pooled ) |
516 | { |
517 | buf.setPooled( pooled ); |
518 | } |
519 | } |