-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,50 @@ | ||
namespace NewLife.Siemens.Common | ||
namespace NewLife.Siemens.Common; | ||
|
||
/// <summary>数据流扩展</summary> | ||
public static class StreamExtensions | ||
{ | ||
/// <summary> | ||
/// Extensions for Streams | ||
/// Reads bytes from the stream into the buffer until exactly the requested number of bytes (or EOF) have been read | ||
/// </summary> | ||
public static class StreamExtensions | ||
/// <param name="stream">the Stream to read from</param> | ||
/// <param name="buffer">the buffer to read into</param> | ||
/// <param name="offset">the offset in the buffer to read into</param> | ||
/// <param name="count">the amount of bytes to read into the buffer</param> | ||
/// <returns>returns the amount of read bytes</returns> | ||
public static Int32 ReadExact(this Stream stream, Byte[] buffer, Int32 offset, Int32 count) | ||
{ | ||
/// <summary> | ||
/// Reads bytes from the stream into the buffer until exactly the requested number of bytes (or EOF) have been read | ||
/// </summary> | ||
/// <param name="stream">the Stream to read from</param> | ||
/// <param name="buffer">the buffer to read into</param> | ||
/// <param name="offset">the offset in the buffer to read into</param> | ||
/// <param name="count">the amount of bytes to read into the buffer</param> | ||
/// <returns>returns the amount of read bytes</returns> | ||
public static Int32 ReadExact(this Stream stream, Byte[] buffer, Int32 offset, Int32 count) | ||
var read = 0; | ||
Int32 received; | ||
do | ||
{ | ||
var read = 0; | ||
Int32 received; | ||
do | ||
{ | ||
received = stream.Read(buffer, offset + read, count - read); | ||
read += received; | ||
} | ||
while (read < count && received > 0); | ||
|
||
return read; | ||
received = stream.Read(buffer, offset + read, count - read); | ||
read += received; | ||
} | ||
while (read < count && received > 0); | ||
|
||
/// <summary> | ||
/// Reads bytes from the stream into the buffer until exactly the requested number of bytes (or EOF) have been read | ||
/// </summary> | ||
/// <param name="stream">the Stream to read from</param> | ||
/// <param name="buffer">the buffer to read into</param> | ||
/// <param name="offset">the offset in the buffer to read into</param> | ||
/// <param name="count">the amount of bytes to read into the buffer</param> | ||
/// <returns>returns the amount of read bytes</returns> | ||
public static async Task<Int32> ReadExactAsync(this Stream stream, Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) | ||
{ | ||
var read = 0; | ||
Int32 received; | ||
do | ||
{ | ||
received = await stream.ReadAsync(buffer, offset + read, count - read, cancellationToken).ConfigureAwait(false); | ||
read += received; | ||
} | ||
while (read < count && received > 0); | ||
return read; | ||
} | ||
|
||
return read; | ||
/// <summary> | ||
/// Reads bytes from the stream into the buffer until exactly the requested number of bytes (or EOF) have been read | ||
/// </summary> | ||
/// <param name="stream">the Stream to read from</param> | ||
/// <param name="buffer">the buffer to read into</param> | ||
/// <param name="offset">the offset in the buffer to read into</param> | ||
/// <param name="count">the amount of bytes to read into the buffer</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>returns the amount of read bytes</returns> | ||
public static async Task<Int32> ReadExactAsync(this Stream stream, Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) | ||
{ | ||
var read = 0; | ||
Int32 received; | ||
do | ||
{ | ||
received = await stream.ReadAsync(buffer, offset + read, count - read, cancellationToken).ConfigureAwait(false); | ||
read += received; | ||
} | ||
while (read < count && received > 0); | ||
|
||
return read; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters