Skip to content

Commit 3715fb1

Browse files
committed
Fixed buffer resizing.
1 parent e1dd4ef commit 3715fb1

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

Provider/src/FirebirdSql.Data.FirebirdClient/Common/ReadBuffer.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ public ReadBuffer(int bufferSize)
4040

4141
public void AddRange(byte[] source, int count)
4242
{
43-
if ((_length + count) >= _buffer.Length)
44-
{
45-
var newBuffer = new byte[_buffer.Length * 2];
46-
Array.Copy(_buffer, newBuffer, _buffer.Length);
47-
_buffer = newBuffer;
48-
}
43+
ResizeBufferIfNeeded(count);
4944
for (var i = 0; i < count; i++)
5045
{
5146
if (_writePosition == _buffer.Length)
@@ -57,8 +52,8 @@ public void AddRange(byte[] source, int count)
5752
}
5853

5954
public int ReadInto(ref byte[] destination, int offset, int count)
60-
{
61-
count = Math.Min(count, Length);
55+
{
56+
count = Math.Min(count, _length);
6257
for (var i = 0; i < count; i++)
6358
{
6459
if (_readPosition == _buffer.Length)
@@ -69,5 +64,19 @@ public int ReadInto(ref byte[] destination, int offset, int count)
6964
_length -= count;
7065
return count;
7166
}
67+
68+
void ResizeBufferIfNeeded(int count)
69+
{
70+
var requiredLength = _length + count;
71+
if (requiredLength > _buffer.Length)
72+
{
73+
var newLength = _buffer.Length * 2;
74+
while (requiredLength > newLength)
75+
newLength *= 2;
76+
var newBuffer = new byte[newLength];
77+
Array.Copy(_buffer, newBuffer, _buffer.Length);
78+
_buffer = newBuffer;
79+
}
80+
}
7281
}
7382
}

0 commit comments

Comments
 (0)