// Reads off from the network buffer and caches bytes. Only reads one column value in the current row.
internal static bool TryCreate(SqlMetaDataPriv metadata, TdsParser parser, TdsParserStateObject stateObj, out SqlCachedBuffer buffer)
{
int cb = 0;
ulong plplength;
byte[] byteArr;
List <byte[]> cachedBytes = new List <byte[]>();
buffer = null;
// the very first length is already read.
if (!parser.TryPlpBytesLeft(stateObj, out plplength))
{
return(false);
}
// For now we only handle Plp data from the parser directly.
Debug.Assert(metadata.metaType.IsPlp, "SqlCachedBuffer call on a non-plp data");
do
{
if (plplength == 0)
{
break;
}
do
{
cb = (plplength > (ulong)_maxChunkSize) ? _maxChunkSize : (int)plplength;
byteArr = new byte[cb];
if (!stateObj.TryReadPlpBytes(ref byteArr, 0, cb, out cb))
{
return(false);
}
Debug.Assert(cb == byteArr.Length);
if (cachedBytes.Count == 0)
{
// Add the Byte order mark if needed if we read the first array
AddByteOrderMark(byteArr, cachedBytes);
}
cachedBytes.Add(byteArr);
plplength -= (ulong)cb;
} while (plplength > 0);
if (!parser.TryPlpBytesLeft(stateObj, out plplength))
{
return(false);
}
} while (plplength > 0);
Debug.Assert(stateObj._longlen == 0 && stateObj._longlenleft == 0);
buffer = new SqlCachedBuffer(cachedBytes);
return(true);
}