Rename ReadFixed to ReadExact, separate Unit test for ReadExact.

This commit is contained in:
Serge Camille
2020-08-19 19:41:44 +02:00
parent 09c8b18d3d
commit d11f46eedb
3 changed files with 11 additions and 8 deletions

View File

@@ -10,14 +10,14 @@ namespace S7.Net
public static class StreamExtensions
{
/// <summary>
/// Reads a fixed amount of bytes from the stream into the buffer
/// 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 int ReadFixed(this Stream stream, byte[] buffer, int offset, int count)
public static int ReadExact(this Stream stream, byte[] buffer, int offset, int count)
{
int read = 0;
int received;
@@ -32,14 +32,14 @@ namespace S7.Net
}
/// <summary>
/// Reads a fixed amount of bytes from the stream into the buffer
/// 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<int> ReadFixedAsync(this Stream stream, byte[] buffer, int offset, int count)
public static async Task<int> ReadExactAsync(this Stream stream, byte[] buffer, int offset, int count)
{
int read = 0;
int received;

View File

@@ -32,14 +32,14 @@ namespace S7.Net
public static TPKT Read(Stream stream)
{
var buf = new byte[4];
int len = stream.ReadFixed(buf, 0, 4);
int len = stream.ReadExact(buf, 0, 4);
if (len < 4) throw new TPKTInvalidException("TPKT is incomplete / invalid");
var version = buf[0];
var reserved1 = buf[1];
var length = buf[2] * 256 + buf[3]; //BigEndian
var data = new byte[length - 4];
len = stream.ReadFixed(data, 0, data.Length);
len = stream.ReadExact(data, 0, data.Length);
if (len < data.Length)
throw new TPKTInvalidException("TPKT is incomplete / invalid");
@@ -60,7 +60,7 @@ namespace S7.Net
public static async Task<TPKT> ReadAsync(Stream stream)
{
var buf = new byte[4];
int len = await stream.ReadFixedAsync(buf, 0, 4);
int len = await stream.ReadExactAsync(buf, 0, 4);
if (len < 4) throw new TPKTInvalidException("TPKT is incomplete / invalid");
var version = buf[0];
@@ -68,7 +68,7 @@ namespace S7.Net
var length = buf[2] * 256 + buf[3]; //BigEndian
var data = new byte[length - 4];
len = await stream.ReadFixedAsync(data, 0, data.Length);
len = await stream.ReadExactAsync(data, 0, data.Length);
if (len < data.Length)
throw new TPKTInvalidException("TPKT payload incomplete / invalid");