summaryrefslogtreecommitdiff
path: root/qpid/java/common/src/main/java/org/apache/mina/common/FixedSizeByteBufferAllocator.java
blob: 0c311b664529244293549220a7d0a20b6e370f12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package org.apache.mina.common;

import org.apache.mina.common.ByteBuffer;

import java.nio.*;

/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
public class FixedSizeByteBufferAllocator  implements ByteBufferAllocator
{


        private static final int MINIMUM_CAPACITY = 1;

        public FixedSizeByteBufferAllocator  ()
        {
        }

        public ByteBuffer allocate( int capacity, boolean direct )
        {
            java.nio.ByteBuffer nioBuffer;
            if( direct )
            {
                nioBuffer = java.nio.ByteBuffer.allocateDirect( capacity );
            }
            else
            {
                nioBuffer = java.nio.ByteBuffer.allocate( capacity );
            }
            return new FixedSizeByteBuffer( nioBuffer );
        }

        public ByteBuffer wrap( java.nio.ByteBuffer nioBuffer )
        {
            return new FixedSizeByteBuffer( nioBuffer );
        }

        public void dispose()
        {
        }



        private static final class FixedSizeByteBuffer extends ByteBuffer
        {
            private java.nio.ByteBuffer buf;
            private int mark = -1;


            protected FixedSizeByteBuffer( java.nio.ByteBuffer buf )
            {
                this.buf = buf;
                buf.order( ByteOrder.BIG_ENDIAN );
            }

            public synchronized void acquire()
            {
            }

            public void release()
            {
            }

            public java.nio.ByteBuffer buf()
            {
                return buf;
            }

            public boolean isPooled()
            {
                return false;
            }

            public void setPooled( boolean pooled )
            {
            }

            public ByteBuffer duplicate() {
                return new FixedSizeByteBuffer( this.buf.duplicate() );
            }

            public ByteBuffer slice() {
                return new FixedSizeByteBuffer( this.buf.slice() );
            }

            public ByteBuffer asReadOnlyBuffer() {
                return new FixedSizeByteBuffer( this.buf.asReadOnlyBuffer() );
            }

            public byte[] array()
            {
                return buf.array();
            }

            public int arrayOffset()
            {
                return buf.arrayOffset();
            }

            public boolean isDirect()
            {
                return buf.isDirect();
            }

            public boolean isReadOnly()
            {
                return buf.isReadOnly();
            }

            public int capacity()
            {
                return buf.capacity();
            }

            public ByteBuffer capacity( int newCapacity )
                {
                    if( newCapacity > capacity() )
                    {
                        throw new IllegalArgumentException();
                    }

                    return this;
                }



            public boolean isAutoExpand()
            {
                return false;
            }

            public ByteBuffer setAutoExpand( boolean autoExpand )
            {
                if(autoExpand) throw new IllegalArgumentException();
                else return this;
            }

            public ByteBuffer expand( int pos, int expectedRemaining )
            {
                int end = pos + expectedRemaining;
                if( end > capacity() )
                {
                    // The buffer needs expansion.
                    capacity( end );
                }

                if( end > limit() )
                {
                    // We call limit() directly to prevent StackOverflowError
                    buf.limit( end );
                }
                return this;
            }

            public int position()
            {
                return buf.position();
            }

            public ByteBuffer position( int newPosition )
            {

                buf.position( newPosition );
                if( mark > newPosition )
                {
                    mark = -1;
                }
                return this;
            }

            public int limit()
            {
                return buf.limit();
            }

            public ByteBuffer limit( int newLimit )
            {
                buf.limit( newLimit );
                if( mark > newLimit )
                {
                    mark = -1;
                }
                return this;
            }

            public ByteBuffer mark()
            {
                buf.mark();
                mark = position();
                return this;
            }

            public int markValue()
            {
                return mark;
            }

            public ByteBuffer reset()
            {
                buf.reset();
                return this;
            }

            public ByteBuffer clear()
            {
                buf.clear();
                mark = -1;
                return this;
            }

            public ByteBuffer flip()
            {
                buf.flip();
                mark = -1;
                return this;
            }

            public ByteBuffer rewind()
            {
                buf.rewind();
                mark = -1;
                return this;
            }

            public byte get()
            {
                return buf.get();
            }

            public ByteBuffer put( byte b )
            {
                buf.put( b );
                return this;
            }

            public byte get( int index )
            {
                return buf.get( index );
            }

            public ByteBuffer put( int index, byte b )
            {
                buf.put( index, b );
                return this;
            }

            public ByteBuffer get( byte[] dst, int offset, int length )
            {
                buf.get( dst, offset, length );
                return this;
            }

            public ByteBuffer put( java.nio.ByteBuffer src )
            {
                buf.put( src );
                return this;
            }

            public ByteBuffer put( byte[] src, int offset, int length )
            {
                buf.put( src, offset, length );
                return this;
            }

            public ByteBuffer compact()
            {
                buf.compact();
                mark = -1;
                return this;
            }

            public ByteOrder order()
            {
                return buf.order();
            }

            public ByteBuffer order( ByteOrder bo )
            {
                buf.order( bo );
                return this;
            }

            public char getChar()
            {
                return buf.getChar();
            }

            public ByteBuffer putChar( char value )
            {
                buf.putChar( value );
                return this;
            }

            public char getChar( int index )
            {
                return buf.getChar( index );
            }

            public ByteBuffer putChar( int index, char value )
            {
                buf.putChar( index, value );
                return this;
            }

            public CharBuffer asCharBuffer()
            {
                return buf.asCharBuffer();
            }

            public short getShort()
            {
                return buf.getShort();
            }

            public ByteBuffer putShort( short value )
            {
                buf.putShort( value );
                return this;
            }

            public short getShort( int index )
            {
                return buf.getShort( index );
            }

            public ByteBuffer putShort( int index, short value )
            {
                buf.putShort( index, value );
                return this;
            }

            public ShortBuffer asShortBuffer()
            {
                return buf.asShortBuffer();
            }

            public int getInt()
            {
                return buf.getInt();
            }

            public ByteBuffer putInt( int value )
            {
                buf.putInt( value );
                return this;
            }

            public int getInt( int index )
            {
                return buf.getInt( index );
            }

            public ByteBuffer putInt( int index, int value )
            {
                buf.putInt( index, value );
                return this;
            }

            public IntBuffer asIntBuffer()
            {
                return buf.asIntBuffer();
            }

            public long getLong()
            {
                return buf.getLong();
            }

            public ByteBuffer putLong( long value )
            {
                buf.putLong( value );
                return this;
            }

            public long getLong( int index )
            {
                return buf.getLong( index );
            }

            public ByteBuffer putLong( int index, long value )
            {
                buf.putLong( index, value );
                return this;
            }

            public LongBuffer asLongBuffer()
            {
                return buf.asLongBuffer();
            }

            public float getFloat()
            {
                return buf.getFloat();
            }

            public ByteBuffer putFloat( float value )
            {
                buf.putFloat( value );
                return this;
            }

            public float getFloat( int index )
            {
                return buf.getFloat( index );
            }

            public ByteBuffer putFloat( int index, float value )
            {
                buf.putFloat( index, value );
                return this;
            }

            public FloatBuffer asFloatBuffer()
            {
                return buf.asFloatBuffer();
            }

            public double getDouble()
            {
                return buf.getDouble();
            }

            public ByteBuffer putDouble( double value )
            {
                buf.putDouble( value );
                return this;
            }

            public double getDouble( int index )
            {
                return buf.getDouble( index );
            }

            public ByteBuffer putDouble( int index, double value )
            {
                buf.putDouble( index, value );
                return this;
            }

            public DoubleBuffer asDoubleBuffer()
            {
                return buf.asDoubleBuffer();
            }


        }


}