FixedSizeByteBufferAllocator.java
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 allocateint capacity, boolean direct )
038         {
039             java.nio.ByteBuffer nioBuffer;
040             ifdirect )
041             {
042                 nioBuffer = java.nio.ByteBuffer.allocateDirectcapacity );
043             }
044             else
045             {
046                 nioBuffer = java.nio.ByteBuffer.allocatecapacity );
047             }
048             return new FixedSizeByteBuffernioBuffer );
049         }
050 
051         public ByteBuffer wrapjava.nio.ByteBuffer nioBuffer )
052         {
053             return new FixedSizeByteBuffernioBuffer );
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 FixedSizeByteBufferjava.nio.ByteBuffer buf )
069             {
070                 this.buf = buf;
071                 buf.orderByteOrder.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 setPooledboolean pooled )
093             {
094             }
095 
096             public ByteBuffer duplicate() {
097                 return new FixedSizeByteBufferthis.buf.duplicate() );
098             }
099 
100             public ByteBuffer slice() {
101                 return new FixedSizeByteBufferthis.buf.slice() );
102             }
103 
104             public ByteBuffer asReadOnlyBuffer() {
105                 return new FixedSizeByteBufferthis.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 capacityint newCapacity )
134                 {
135                     ifnewCapacity > 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 setAutoExpandboolean autoExpand )
151             {
152                 if(autoExpandthrow new IllegalArgumentException();
153                 else return this;
154             }
155 
156             public ByteBuffer expandint pos, int expectedRemaining )
157             {
158                 int end = pos + expectedRemaining;
159                 ifend > capacity() )
160                 {
161                     // The buffer needs expansion.
162                     capacityend );
163                 }
164 
165                 ifend > limit() )
166                 {
167                     // We call limit() directly to prevent StackOverflowError
168                     buf.limitend );
169                 }
170                 return this;
171             }
172 
173             public int position()
174             {
175                 return buf.position();
176             }
177 
178             public ByteBuffer positionint newPosition )
179             {
180 
181                 buf.positionnewPosition );
182                 ifmark > 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 limitint newLimit )
195             {
196                 buf.limitnewLimit );
197                 ifmark > 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 putbyte )
249             {
250                 buf.put);
251                 return this;
252             }
253 
254             public byte getint index )
255             {
256                 return buf.getindex );
257             }
258 
259             public ByteBuffer putint index, byte )
260             {
261                 buf.putindex, b );
262                 return this;
263             }
264 
265             public ByteBuffer getbyte[] dst, int offset, int length )
266             {
267                 buf.getdst, offset, length );
268                 return this;
269             }
270 
271             public ByteBuffer putjava.nio.ByteBuffer src )
272             {
273                 buf.putsrc );
274                 return this;
275             }
276 
277             public ByteBuffer putbyte[] src, int offset, int length )
278             {
279                 buf.putsrc, 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 orderByteOrder bo )
296             {
297                 buf.orderbo );
298                 return this;
299             }
300 
301             public char getChar()
302             {
303                 return buf.getChar();
304             }
305 
306             public ByteBuffer putCharchar value )
307             {
308                 buf.putCharvalue );
309                 return this;
310             }
311 
312             public char getCharint index )
313             {
314                 return buf.getCharindex );
315             }
316 
317             public ByteBuffer putCharint index, char value )
318             {
319                 buf.putCharindex, 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 putShortshort value )
334             {
335                 buf.putShortvalue );
336                 return this;
337             }
338 
339             public short getShortint index )
340             {
341                 return buf.getShortindex );
342             }
343 
344             public ByteBuffer putShortint index, short value )
345             {
346                 buf.putShortindex, 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 putIntint value )
361             {
362                 buf.putIntvalue );
363                 return this;
364             }
365 
366             public int getIntint index )
367             {
368                 return buf.getIntindex );
369             }
370 
371             public ByteBuffer putIntint index, int value )
372             {
373                 buf.putIntindex, 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 putLonglong value )
388             {
389                 buf.putLongvalue );
390                 return this;
391             }
392 
393             public long getLongint index )
394             {
395                 return buf.getLongindex );
396             }
397 
398             public ByteBuffer putLongint index, long value )
399             {
400                 buf.putLongindex, 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 putFloatfloat value )
415             {
416                 buf.putFloatvalue );
417                 return this;
418             }
419 
420             public float getFloatint index )
421             {
422                 return buf.getFloatindex );
423             }
424 
425             public ByteBuffer putFloatint index, float value )
426             {
427                 buf.putFloatindex, 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 putDoubledouble value )
442             {
443                 buf.putDoublevalue );
444                 return this;
445             }
446 
447             public double getDoubleint index )
448             {
449                 return buf.getDoubleindex );
450             }
451 
452             public ByteBuffer putDoubleint index, double value )
453             {
454                 buf.putDoubleindex, value );
455                 return this;
456             }
457 
458             public DoubleBuffer asDoubleBuffer()
459             {
460                 return buf.asDoubleBuffer();
461             }
462 
463 
464         }
465 
466 
467 }