From d11f46eedbdfcc8f76b3427add64f8a6fd30dfee Mon Sep 17 00:00:00 2001 From: Serge Camille Date: Wed, 19 Aug 2020 19:41:44 +0200 Subject: [PATCH] Rename ReadFixed to ReadExact, separate Unit test for ReadExact. --- S7.Net.UnitTest/StreamTests.cs | 3 +++ S7.Net/StreamExtensions.cs | 8 ++++---- S7.Net/TPKT.cs | 8 ++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/S7.Net.UnitTest/StreamTests.cs b/S7.Net.UnitTest/StreamTests.cs index 1d33e21..bd3c1d9 100644 --- a/S7.Net.UnitTest/StreamTests.cs +++ b/S7.Net.UnitTest/StreamTests.cs @@ -55,6 +55,9 @@ namespace S7.Net.UnitTest } } + /// + /// These tests are intended to test functions and other stream-related special cases. + /// [TestClass] public class StreamTests { diff --git a/S7.Net/StreamExtensions.cs b/S7.Net/StreamExtensions.cs index dc8db73..759f595 100644 --- a/S7.Net/StreamExtensions.cs +++ b/S7.Net/StreamExtensions.cs @@ -10,14 +10,14 @@ namespace S7.Net public static class StreamExtensions { /// - /// 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 /// /// the Stream to read from /// the buffer to read into /// the offset in the buffer to read into /// the amount of bytes to read into the buffer /// returns the amount of read bytes - 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 } /// - /// 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 /// /// the Stream to read from /// the buffer to read into /// the offset in the buffer to read into /// the amount of bytes to read into the buffer /// returns the amount of read bytes - public static async Task ReadFixedAsync(this Stream stream, byte[] buffer, int offset, int count) + public static async Task ReadExactAsync(this Stream stream, byte[] buffer, int offset, int count) { int read = 0; int received; diff --git a/S7.Net/TPKT.cs b/S7.Net/TPKT.cs index 9a8fbed..23258cc 100644 --- a/S7.Net/TPKT.cs +++ b/S7.Net/TPKT.cs @@ -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 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");