blob: 6c1d911704fb015a81457ebbbfe83d754546ff68 (
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
|
// **********************************************************************
//
// Copyright (c) 2003-2009 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.
//
// **********************************************************************
package IceInternal;
//
// An instance of java.nio.ByteBuffer cannot grow beyond its initial capacity.
// This class wraps a ByteBuffer and supports reallocation.
//
public class Buffer
{
public
Buffer(int maxCapacity, boolean direct)
{
b = _emptyBuffer;
_size = 0;
_capacity = 0;
_maxCapacity = maxCapacity;
_direct = direct;
}
public int
size()
{
return _size;
}
public boolean
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)
{
final int sz = (b == _emptyBuffer) ? n : b.position() + n;
if(sz > _size)
{
resize(sz, false);
}
}
public void
resize(int n, boolean reading)
{
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)
{
if(n > _capacity)
{
_capacity = java.lang.Math.max(n, java.lang.Math.min(2 * _capacity, _maxCapacity));
_capacity = java.lang.Math.max(240, _capacity);
}
else if(n < _capacity)
{
_capacity = n;
}
else
{
return;
}
try
{
java.nio.ByteBuffer buf;
if(_direct)
{
buf = java.nio.ByteBuffer.allocateDirect(_capacity);
}
else
{
buf = java.nio.ByteBuffer.allocate(_capacity);
}
if(b == _emptyBuffer)
{
b = buf;
}
else
{
final int pos = b.position();
b.position(0);
b.limit(java.lang.Math.min(_capacity, b.capacity()));
buf.put(b);
b = buf;
b.limit(b.capacity());
b.position(pos);
}
b.order(java.nio.ByteOrder.LITTLE_ENDIAN);
}
catch(OutOfMemoryError ex)
{
Ice.MarshalException e = new Ice.MarshalException();
e.reason = "OutOfMemoryError occurred while allocating a ByteBuffer";
e.initCause(ex);
throw e;
}
}
public java.nio.ByteBuffer b;
// Sentinel used for null buffer.
public java.nio.ByteBuffer _emptyBuffer = java.nio.ByteBuffer.allocate(0);
private int _size;
private int _capacity; // Cache capacity to avoid excessive method calls.
private int _maxCapacity;
private boolean _direct; // Use direct buffers?
private int _shrinkCounter;
}
|