diff --git a/Aaru.Helpers.csproj b/Aaru.Helpers.csproj index e4eecd899..604834a5c 100644 --- a/Aaru.Helpers.csproj +++ b/Aaru.Helpers.csproj @@ -47,6 +47,7 @@ + diff --git a/Extensions.cs b/Extensions.cs new file mode 100644 index 000000000..c1b8479c0 --- /dev/null +++ b/Extensions.cs @@ -0,0 +1,72 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Filename : Extensions.cs +// Author(s) : Natalia Portillo +// +// Component : Helpers. +// +// --[ Description ] ---------------------------------------------------------- +// +// Provides class extensions. +// +// --[ License ] -------------------------------------------------------------- +// +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2022 Natalia Portillo +// ****************************************************************************/ + +namespace Aaru.Helpers; + +using System.IO; + +public static class Extensions +{ + /// + /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the + /// position within the stream by the number of bytes read.
Guarantees the whole count of bytes is read or EOF is + /// found + ///
+ /// Stream to extend + /// + /// An array of bytes. When this method returns, the buffer contains the specified byte array with the + /// values between and ( + - 1) replaced by the bytes + /// read from the current source. + /// + /// + /// The zero-based byte offset in at which to begin storing the data read from + /// the current stream. + /// + /// The maximum number of bytes to be read from the current stream. + /// + /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if the end + /// of the stream has been reached. + /// + public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count) + { + var pos = 0; + int read; + + do + { + read = s.Read(buffer, pos + offset, count - pos); + pos += read; + } while(read > 0); + + return pos; + } +} \ No newline at end of file