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
|
/*
*
* 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.
*
*/
//package org.apache.mina.common.support;
//
//import java.nio.ByteOrder;
//import java.nio.CharBuffer;
//import java.nio.DoubleBuffer;
//import java.nio.FloatBuffer;
//import java.nio.IntBuffer;
//import java.nio.LongBuffer;
//import java.nio.ShortBuffer;
//
//import org.apache.mina.common.ByteBuffer;
//import org.apache.mina.common.ByteBufferAllocator;
namespace Qpid.Buffer
{
/// <summary>
/// A base implementation of <see cref="ByteBuffer"/>. This implementation
/// assumes that <see cref="ByteBuffer.Buf"/> always returns a correct NIO
/// <see cref="FixedByteBuffer"/> instance. Most implementations could
/// extend this class and implement their own buffer management mechanism.
/// </summary>
/// <seealso cref="ByteBufferAllocator"/>
public abstract class BaseByteBuffer : ByteBuffer
{
private bool _autoExpand;
/// <summary>
/// We don't have any access to Buffer.markValue(),
/// so we need to track it down, which will cause small extra overhead.
/// </summary>
private int _mark = -1;
protected BaseByteBuffer()
{
}
public override bool isDirect()
{
return buf().isDirect();
}
public override bool isReadOnly()
{
return buf().isReadOnly();
}
public override int capacity()
{
return buf().capacity();
}
public override ByteBuffer capacity(int newCapacity)
{
if( newCapacity > capacity() )
{
// Allocate a new buffer and transfer all settings to it.
int pos = position();
int lim = limit();
ByteOrder bo = order();
capacity0( newCapacity );
buf().limit( lim );
if( _mark >= 0 )
{
buf().position( _mark );
buf().mark();
}
buf().position( pos );
buf().order( bo );
}
return this;
}
/// <summary>
/// Implement this method to increase the capacity of this buffer.
/// </summary>
/// <param name="newCapacity">is always greater than the current capacity.</param>
protected abstract void capacity0( int newCapacity );
public override bool isAutoExpand()
{
return _autoExpand;
}
public override ByteBuffer setAutoExpand(bool autoExpand)
{
_autoExpand = autoExpand;
return this;
}
public override 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 override int position()
{
return buf().position();
}
public override ByteBuffer position(int newPosition)
{
autoExpand( newPosition, 0 );
buf().position( newPosition );
if( _mark > newPosition )
{
_mark = -1;
}
return this;
}
public override int limit()
{
return buf().limit();
}
public override ByteBuffer limit(int newLimit)
{
autoExpand( newLimit, 0 );
buf().limit( newLimit );
if( _mark > newLimit )
{
_mark = -1;
}
return this;
}
public override ByteBuffer mark()
{
buf().mark();
_mark = position();
return this;
}
public override int markValue()
{
return _mark;
}
public override ByteBuffer reset()
{
buf().reset();
return this;
}
public override ByteBuffer clear()
{
buf().clear();
_mark = -1;
return this;
}
public override ByteBuffer flip()
{
buf().flip();
_mark = -1;
return this;
}
public override ByteBuffer rewind()
{
buf().rewind();
_mark = -1;
return this;
}
public override byte get()
{
return buf().get();
}
public override ByteBuffer put(byte b)
{
autoExpand( 1 );
buf().put( b );
return this;
}
public override byte get(int index)
{
return buf().get( index );
}
public override ByteBuffer put(int index, byte b)
{
autoExpand( index, 1 );
buf().put( index, b );
return this;
}
public override ByteBuffer get(byte[] dst, int offset, int length)
{
buf().get( dst, offset, length );
return this;
}
public override ByteBuffer get(byte[] dst)
{
buf().get(dst);
return this;
}
public override ByteBuffer put(FixedByteBuffer src)
{
autoExpand( src.remaining() );
buf().put( src );
return this;
}
public override ByteBuffer put(byte[] src, int offset, int length)
{
autoExpand( length );
buf().put( src, offset, length );
return this;
}
public override ByteBuffer compact()
{
buf().compact();
_mark = -1;
return this;
}
public override ByteOrder order()
{
return buf().order();
}
public override ByteBuffer order(ByteOrder bo)
{
buf().order( bo );
return this;
}
public override char getChar()
{
return buf().getChar();
}
public override ByteBuffer putChar(char value)
{
autoExpand( 2 );
buf().putChar( value );
return this;
}
public override char getChar(int index)
{
return buf().getChar( index );
}
public override ByteBuffer putChar(int index, char value)
{
autoExpand( index, 2 );
buf().putChar( index, value );
return this;
}
// public CharBuffer asCharBuffer()
// {
// return buf().asCharBuffer();
// }
public override short getShort()
{
return buf().getShort();
}
public override ByteBuffer putShort(short value)
{
autoExpand( 2 );
buf().putShort( value );
return this;
}
public override short getShort(int index)
{
return buf().getShort( index );
}
public override ByteBuffer putShort(int index, short value)
{
autoExpand( index, 2 );
buf().putShort( index, value );
return this;
}
public override ushort GetUnsignedShort()
{
return buf().getUnsignedShort();
}
// public ShortBuffer asShortBuffer()
// {
// return buf().asShortBuffer();
// }
public override int getInt()
{
return buf().getInt();
}
public override uint GetUnsignedInt()
{
return buf().getUnsignedInt();
}
public override ByteBuffer putInt(int value)
{
autoExpand( 4 );
buf().putInt( value );
return this;
}
public override int getInt(int index)
{
return buf().getInt( index );
}
public override ByteBuffer putInt(int index, int value)
{
autoExpand( index, 4 );
buf().putInt( index, value );
return this;
}
// public IntBuffer asIntBuffer()
// {
// return buf().asIntBuffer();
// }
public override long getLong()
{
return buf().getLong();
}
public override ByteBuffer putLong(long value)
{
autoExpand( 8 );
buf().putLong( value );
return this;
}
public override long getLong(int index)
{
return buf().getLong( index );
}
public override ByteBuffer putLong(int index, long value)
{
autoExpand( index, 8 );
buf().putLong( index, value );
return this;
}
// public LongBuffer asLongBuffer()
// {
// return buf().asLongBuffer();
// }
public override float getFloat()
{
return buf().getFloat();
}
public override ByteBuffer putFloat(float value)
{
autoExpand( 4 );
buf().putFloat( value );
return this;
}
public override float getFloat(int index)
{
return buf().getFloat( index );
}
public override ByteBuffer putFloat(int index, float value)
{
autoExpand( index, 4 );
buf().putFloat( index, value );
return this;
}
// public FloatBuffer asFloatBuffer()
// {
// return buf().asFloatBuffer();
// }
public override double getDouble()
{
return buf().getDouble();
}
public override ByteBuffer putDouble(double value)
{
autoExpand( 8 );
buf().putDouble( value );
return this;
}
public override double getDouble(int index)
{
return buf().getDouble( index );
}
public override ByteBuffer putDouble(int index, double value)
{
autoExpand( index, 8 );
buf().putDouble( index, value );
return this;
}
public override ByteBuffer put(byte[] src)
{
buf().put(src);
return this;
}
// public DoubleBuffer asDoubleBuffer()
// {
// return buf().asDoubleBuffer();
// }
}
}
|