Skip to content

Change TryReadString to use TryReadBytesWithContinue #3422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6374,7 +6374,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
}
else
{
result = TryReadByteArrayWithContinue(stateObj, length, out b);
result = stateObj.TryReadByteArrayWithContinue(length, out b);
if (result != TdsOperationStatus.Done)
{
return result;
Expand Down Expand Up @@ -6495,48 +6495,6 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
return TdsOperationStatus.Done;
}

private TdsOperationStatus TryReadByteArrayWithContinue(TdsParserStateObject stateObj, int length, out byte[] bytes)
{
bytes = null;
int offset = 0;
byte[] temp = null;
(bool canContinue, bool isStarting, bool isContinuing) = stateObj.GetSnapshotStatuses();
if (canContinue)
{
if (isContinuing || isStarting)
{
temp = stateObj.TryTakeSnapshotStorage() as byte[];
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
}
if (temp != null)
{
offset = stateObj.GetSnapshotTotalSize();
}
}


if (temp == null)
{
temp = new byte[length];
}

TdsOperationStatus result = stateObj.TryReadByteArray(temp, length, out _, offset, isStarting || isContinuing);

if (result == TdsOperationStatus.Done)
{
bytes = temp;
}
else if (result == TdsOperationStatus.NeedMoreData)
{
if (isStarting || isContinuing)
{
stateObj.SetSnapshotStorage(temp);
}
}

return result;
}

private TdsOperationStatus TryReadSqlDateTime(SqlBuffer value, byte tdsType, int length, byte scale, TdsParserStateObject stateObj)
{
Span<byte> datetimeBuffer = ((uint)length <= 16) ? stackalloc byte[16] : new byte[length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6570,7 +6570,7 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
}
else
{
result = TryReadByteArrayWithContinue(stateObj, length, out b);
result = stateObj.TryReadByteArrayWithContinue(length, out b);
if (result != TdsOperationStatus.Done)
{
return result;
Expand Down Expand Up @@ -6691,48 +6691,6 @@ internal TdsOperationStatus TryReadSqlValue(SqlBuffer value,
return TdsOperationStatus.Done;
}

private TdsOperationStatus TryReadByteArrayWithContinue(TdsParserStateObject stateObj, int length, out byte[] bytes)
{
bytes = null;
int offset = 0;
byte[] temp = null;
(bool canContinue, bool isStarting, bool isContinuing) = stateObj.GetSnapshotStatuses();
if (canContinue)
{
if (isContinuing || isStarting)
{
temp = stateObj.TryTakeSnapshotStorage() as byte[];
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
}
if (temp != null)
{
offset = stateObj.GetSnapshotTotalSize();
}
}


if (temp == null)
{
temp = new byte[length];
}

TdsOperationStatus result = stateObj.TryReadByteArray(temp, length, out _, offset, isStarting || isContinuing);

if (result == TdsOperationStatus.Done)
{
bytes = temp;
}
else if (result == TdsOperationStatus.NeedMoreData)
{
if (isStarting || isContinuing)
{
stateObj.SetSnapshotStorage(temp);
}
}

return result;
}

private TdsOperationStatus TryReadSqlDateTime(SqlBuffer value, byte tdsType, int length, byte scale, TdsParserStateObject stateObj)
{
Span<byte> datetimeBuffer = ((uint)length <= 16) ? stackalloc byte[16] : new byte[length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,48 @@ public TdsOperationStatus TryReadByteArray(Span<byte> buff, int len, out int tot
return TdsOperationStatus.Done;
}

public TdsOperationStatus TryReadByteArrayWithContinue(int length, out byte[] bytes)
{
bytes = null;
int offset = 0;
byte[] temp = null;
(bool canContinue, bool isStarting, bool isContinuing) = GetSnapshotStatuses();
if (canContinue)
{
if (isContinuing || isStarting)
{
temp = TryTakeSnapshotStorage() as byte[];
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
}
if (temp != null)
{
offset = GetSnapshotTotalSize();
}
}


if (temp == null)
{
temp = new byte[length];
}

TdsOperationStatus result = TryReadByteArray(temp, length, out _, offset, isStarting || isContinuing);

if (result == TdsOperationStatus.Done)
{
bytes = temp;
}
else if (result == TdsOperationStatus.NeedMoreData)
{
if (isStarting || isContinuing)
{
SetSnapshotStorage(temp);
}
}

return result;
}

// Takes no arguments and returns a byte from the buffer. If the buffer is empty, it is filled
// before the byte is returned.
internal TdsOperationStatus TryReadByte(out byte value)
Expand Down Expand Up @@ -1843,21 +1885,13 @@ internal TdsOperationStatus TryReadString(int length, out string value)

if (((_inBytesUsed + cBytes) > _inBytesRead) || (_inBytesPacket < cBytes))
{
if (_bTmp == null || _bTmp.Length < cBytes)
{
_bTmp = new byte[cBytes];
}

TdsOperationStatus result = TryReadByteArray(_bTmp, cBytes);
TdsOperationStatus result = TryReadByteArrayWithContinue(cBytes, out buf);
if (result != TdsOperationStatus.Done)
{
value = null;
return result;
}

// assign local to point to parser scratch buffer
buf = _bTmp;

AssertValidState();
}
else
Expand Down
Loading
Loading