001 package org.apache.mina.common;
002
003 import org.apache.mina.common.ByteBuffer;
004
005 import java.nio.*;
006
007 /*
008 *
009 * Licensed to the Apache Software Foundation (ASF) under one
010 * or more contributor license agreements. See the NOTICE file
011 * distributed with this work for additional information
012 * regarding copyright ownership. The ASF licenses this file
013 * to you under the Apache License, Version 2.0 (the
014 * "License"); you may not use this file except in compliance
015 * with the License. You may obtain a copy of the License at
016 *
017 * http://www.apache.org/licenses/LICENSE-2.0
018 *
019 * Unless required by applicable law or agreed to in writing,
020 * software distributed under the License is distributed on an
021 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
022 * KIND, either express or implied. See the License for the
023 * specific language governing permissions and limitations
024 * under the License.
025 *
026 */
027 public class FixedSizeByteBufferAllocator implements ByteBufferAllocator
028 {
029
030
031 private static final int MINIMUM_CAPACITY = 1;
032
033 public FixedSizeByteBufferAllocator ()
034 {
035 }
036
037 public ByteBuffer allocate( int capacity, boolean direct )
038 {
039 java.nio.ByteBuffer nioBuffer;
040 if( direct )
041 {
042 nioBuffer = java.nio.ByteBuffer.allocateDirect( capacity );
043 }
044 else
045 {
046 nioBuffer = java.nio.ByteBuffer.allocate( capacity );
047 }
048 return new FixedSizeByteBuffer( nioBuffer );
049 }
050
051 public ByteBuffer wrap( java.nio.ByteBuffer nioBuffer )
052 {
053 return new FixedSizeByteBuffer( nioBuffer );
054 }
055
056 public void dispose()
057 {
058 }
059
060
061
062 private static final class FixedSizeByteBuffer extends ByteBuffer
063 {
064 private java.nio.ByteBuffer buf;
065 private int mark = -1;
066
067
068 protected FixedSizeByteBuffer( java.nio.ByteBuffer buf )
069 {
070 this.buf = buf;
071 buf.order( ByteOrder.BIG_ENDIAN );
072 }
073
074 public synchronized void acquire()
075 {
076 }
077
078 public void release()
079 {
080 }
081
082 public java.nio.ByteBuffer buf()
083 {
084 return buf;
085 }
086
087 public boolean isPooled()
088 {
089 return false;
090 }
091
092 public void setPooled( boolean pooled )
093 {
094 }
095
096 public ByteBuffer duplicate() {
097 return new FixedSizeByteBuffer( this.buf.duplicate() );
098 }
099
100 public ByteBuffer slice() {
101 return new FixedSizeByteBuffer( this.buf.slice() );
102 }
103
104 public ByteBuffer asReadOnlyBuffer() {
105 return new FixedSizeByteBuffer( this.buf.asReadOnlyBuffer() );
106 }
107
108 public byte[] array()
109 {
110 return buf.array();
111 }
112
113 public int arrayOffset()
114 {
115 return buf.arrayOffset();
116 }
117
118 public boolean isDirect()
119 {
120 return buf.isDirect();
121 }
122
123 public boolean isReadOnly()
124 {
125 return buf.isReadOnly();
126 }
127
128 public int capacity()
129 {
130 return buf.capacity();
131 }
132
133 public ByteBuffer capacity( int newCapacity )
134 {
135 if( newCapacity > capacity() )
136 {
137 throw new IllegalArgumentException();
138 }
139
140 return this;
141 }
142
143
144
145 public boolean isAutoExpand()
146 {
147 return false;
148 }
149
150 public ByteBuffer setAutoExpand( boolean autoExpand )
151 {
152 if(autoExpand) throw new IllegalArgumentException();
153 else return this;
154 }
155
156 public ByteBuffer expand( int pos, int expectedRemaining )
157 {
158 int end = pos + expectedRemaining;
159 if( end > capacity() )
160 {
161 // The buffer needs expansion.
162 capacity( end );
163 }
164
165 if( end > limit() )
166 {
167 // We call limit() directly to prevent StackOverflowError
168 buf.limit( end );
169 }
170 return this;
171 }
172
173 public int position()
174 {
175 return buf.position();
176 }
177
178 public ByteBuffer position( int newPosition )
179 {
180
181 buf.position( newPosition );
182 if( mark > newPosition )
183 {
184 mark = -1;
185 }
186 return this;
187 }
188
189 public int limit()
190 {
191 return buf.limit();
192 }
193
194 public ByteBuffer limit( int newLimit )
195 {
196 buf.limit( newLimit );
197 if( mark > newLimit )
198 {
199 mark = -1;
200 }
201 return this;
202 }
203
204 public ByteBuffer mark()
205 {
206 buf.mark();
207 mark = position();
208 return this;
209 }
210
211 public int markValue()
212 {
213 return mark;
214 }
215
216 public ByteBuffer reset()
217 {
218 buf.reset();
219 return this;
220 }
221
222 public ByteBuffer clear()
223 {
224 buf.clear();
225 mark = -1;
226 return this;
227 }
228
229 public ByteBuffer flip()
230 {
231 buf.flip();
232 mark = -1;
233 return this;
234 }
235
236 public ByteBuffer rewind()
237 {
238 buf.rewind();
239 mark = -1;
240 return this;
241 }
242
243 public byte get()
244 {
245 return buf.get();
246 }
247
248 public ByteBuffer put( byte b )
249 {
250 buf.put( b );
251 return this;
252 }
253
254 public byte get( int index )
255 {
256 return buf.get( index );
257 }
258
259 public ByteBuffer put( int index, byte b )
260 {
261 buf.put( index, b );
262 return this;
263 }
264
265 public ByteBuffer get( byte[] dst, int offset, int length )
266 {
267 buf.get( dst, offset, length );
268 return this;
269 }
270
271 public ByteBuffer put( java.nio.ByteBuffer src )
272 {
273 buf.put( src );
274 return this;
275 }
276
277 public ByteBuffer put( byte[] src, int offset, int length )
278 {
279 buf.put( src, offset, length );
280 return this;
281 }
282
283 public ByteBuffer compact()
284 {
285 buf.compact();
286 mark = -1;
287 return this;
288 }
289
290 public ByteOrder order()
291 {
292 return buf.order();
293 }
294
295 public ByteBuffer order( ByteOrder bo )
296 {
297 buf.order( bo );
298 return this;
299 }
300
301 public char getChar()
302 {
303 return buf.getChar();
304 }
305
306 public ByteBuffer putChar( char value )
307 {
308 buf.putChar( value );
309 return this;
310 }
311
312 public char getChar( int index )
313 {
314 return buf.getChar( index );
315 }
316
317 public ByteBuffer putChar( int index, char value )
318 {
319 buf.putChar( index, value );
320 return this;
321 }
322
323 public CharBuffer asCharBuffer()
324 {
325 return buf.asCharBuffer();
326 }
327
328 public short getShort()
329 {
330 return buf.getShort();
331 }
332
333 public ByteBuffer putShort( short value )
334 {
335 buf.putShort( value );
336 return this;
337 }
338
339 public short getShort( int index )
340 {
341 return buf.getShort( index );
342 }
343
344 public ByteBuffer putShort( int index, short value )
345 {
346 buf.putShort( index, value );
347 return this;
348 }
349
350 public ShortBuffer asShortBuffer()
351 {
352 return buf.asShortBuffer();
353 }
354
355 public int getInt()
356 {
357 return buf.getInt();
358 }
359
360 public ByteBuffer putInt( int value )
361 {
362 buf.putInt( value );
363 return this;
364 }
365
366 public int getInt( int index )
367 {
368 return buf.getInt( index );
369 }
370
371 public ByteBuffer putInt( int index, int value )
372 {
373 buf.putInt( index, value );
374 return this;
375 }
376
377 public IntBuffer asIntBuffer()
378 {
379 return buf.asIntBuffer();
380 }
381
382 public long getLong()
383 {
384 return buf.getLong();
385 }
386
387 public ByteBuffer putLong( long value )
388 {
389 buf.putLong( value );
390 return this;
391 }
392
393 public long getLong( int index )
394 {
395 return buf.getLong( index );
396 }
397
398 public ByteBuffer putLong( int index, long value )
399 {
400 buf.putLong( index, value );
401 return this;
402 }
403
404 public LongBuffer asLongBuffer()
405 {
406 return buf.asLongBuffer();
407 }
408
409 public float getFloat()
410 {
411 return buf.getFloat();
412 }
413
414 public ByteBuffer putFloat( float value )
415 {
416 buf.putFloat( value );
417 return this;
418 }
419
420 public float getFloat( int index )
421 {
422 return buf.getFloat( index );
423 }
424
425 public ByteBuffer putFloat( int index, float value )
426 {
427 buf.putFloat( index, value );
428 return this;
429 }
430
431 public FloatBuffer asFloatBuffer()
432 {
433 return buf.asFloatBuffer();
434 }
435
436 public double getDouble()
437 {
438 return buf.getDouble();
439 }
440
441 public ByteBuffer putDouble( double value )
442 {
443 buf.putDouble( value );
444 return this;
445 }
446
447 public double getDouble( int index )
448 {
449 return buf.getDouble( index );
450 }
451
452 public ByteBuffer putDouble( int index, double value )
453 {
454 buf.putDouble( index, value );
455 return this;
456 }
457
458 public DoubleBuffer asDoubleBuffer()
459 {
460 return buf.asDoubleBuffer();
461 }
462
463
464 }
465
466
467 }
|