本文整理汇总了C#中System.IO.Stream.TryReadAll方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.TryReadAll方法的具体用法?C# Stream.TryReadAll怎么用?C# Stream.TryReadAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Stream
的用法示例。
在下文中一共展示了Stream.TryReadAll方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteBytes
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception>
public int WriteBytes(Stream source, int byteCount)
{
if (source == null)
{
Throw.ArgumentNull(nameof(source));
}
if (byteCount < 0)
{
Throw.ArgumentOutOfRange(nameof(byteCount));
}
int start = Advance(byteCount);
int bytesRead = source.TryReadAll(_buffer, start, byteCount);
_position = start + bytesRead;
return bytesRead;
}
示例2: TryWriteBytes
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="byteCount"/> is negative.</exception>
/// <exception cref="InvalidOperationException">Builder is not writable, it has been linked with another one.</exception>
/// <returns>Bytes successfully written from the <paramref name="source" />.</returns>
public int TryWriteBytes(Stream source, int byteCount)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (byteCount < 0)
{
throw new ArgumentOutOfRangeException(nameof(byteCount));
}
if (byteCount == 0)
{
return 0;
}
int bytesRead = 0;
int bytesToCurrent = Math.Min(FreeBytes, byteCount);
if (bytesToCurrent > 0)
{
bytesRead = source.TryReadAll(_buffer, Length, bytesToCurrent);
AddLength(bytesRead);
if (bytesRead != bytesToCurrent)
{
return bytesRead;
}
}
int remaining = byteCount - bytesToCurrent;
if (remaining > 0)
{
Expand(remaining);
bytesRead = source.TryReadAll(_buffer, 0, remaining);
AddLength(bytesRead);
bytesRead += bytesToCurrent;
}
return bytesRead;
}