blob: b41d8f6906ef0ba9ad7b8201293af359b3da8b85 (
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
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace IceInternal
{
using System.Diagnostics;
//
// An instance of ByteBuffer cannot grow beyond its initial capacity.
// This class wraps a ByteBuffer and supports reallocation.
//
public class Buffer
{
public Buffer() : this(ByteBuffer.ByteOrder.LITTLE_ENDIAN)
{
}
public Buffer(ByteBuffer.ByteOrder order)
{
b = _emptyBuffer;
_size = 0;
_capacity = 0;
_order = order;
}
public Buffer(byte[] data) : this(data, ByteBuffer.ByteOrder.LITTLE_ENDIAN)
{
}
public Buffer(byte[] data, ByteBuffer.ByteOrder order)
{
b = ByteBuffer.wrap(data);
b.order(order);
_size = data.Length;
_capacity = 0;
_order = order;
}
public int size()
{
return _size;
}
public bool empty()
{
return _size == 0;
}
public void clear()
{
b = _emptyBuffer;
_size = 0;
_capacity = 0;
}
//
// Call expand(n) to add room for n additional bytes. Note that expand()
// examines the current position of the buffer first; we don't want to
// expand the buffer if the caller is writing to a location that is
// already in the buffer.
//
public void expand(int n)
{
int sz = (b == _emptyBuffer) ? n : b.position() + n;
if(sz > _size)
{
resize(sz, false);
}
}
public void resize(int n, bool reading)
{
Debug.Assert(b == _emptyBuffer || _capacity > 0);
if(n == 0)
{
clear();
}
else if(n > _capacity)
{
reserve(n);
}
_size = n;
//
// When used for reading, we want to set the buffer's limit to the new size.
//
if(reading)
{
b.limit(_size);
}
}
public void reset()
{
if(_size > 0 && _size * 2 < _capacity)
{
//
// If the current buffer size is smaller than the
// buffer capacity, we shrink the buffer memory to the
// current size. This is to avoid holding on to too much
// memory if it's not needed anymore.
//
if(++_shrinkCounter > 2)
{
reserve(_size);
_shrinkCounter = 0;
}
}
else
{
_shrinkCounter = 0;
}
_size = 0;
if(b != _emptyBuffer)
{
b.limit(b.capacity());
b.position(0);
}
}
private void reserve(int n)
{
Debug.Assert(_capacity == b.capacity());
if(n > _capacity)
{
_capacity = System.Math.Max(n, 2 * _capacity);
_capacity = System.Math.Max(240, _capacity);
}
else if(n < _capacity)
{
_capacity = n;
}
else
{
return;
}
try
{
ByteBuffer buf = ByteBuffer.allocate(_capacity);
if(b == _emptyBuffer)
{
b = buf;
}
else
{
int pos = b.position();
b.position(0);
b.limit(System.Math.Min(_capacity, b.capacity()));
buf.put(b);
b = buf;
b.limit(b.capacity());
b.position(pos);
}
b.order(_order);
}
catch(System.OutOfMemoryException)
{
_capacity = b.capacity(); // Restore the previous capacity
throw;
}
catch(System.Exception ex)
{
_capacity = b.capacity(); // Restore the previous capacity.
Ice.MarshalException e = new Ice.MarshalException(ex);
e.reason = "unexpected exception while trying to allocate a ByteBuffer:\n" + ex;
throw e;
}
finally
{
Debug.Assert(_capacity == b.capacity());
}
}
public ByteBuffer b;
// Sentinel used for null buffer.
static private ByteBuffer _emptyBuffer = new ByteBuffer();
private int _size;
private int _capacity; // Cache capacity to avoid excessive method calls.
private int _shrinkCounter;
private ByteBuffer.ByteOrder _order;
}
}
|