// // StreamUtility.cs // // Author: Kees van Spelde // // Copyright (c) 2013-2018 Magic-Sessions. (www.magic-sessions.com) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // using System; using System.IO; using System.Text; namespace MsgReader.Helpers { /// /// Utility to help reading bytes and strings of a /// internal static class StreamUtility { #region ReadLineAsBytes /// /// Read a line from the stream. /// A line is interpreted as all the bytes read until a CRLF or LF is encountered.
/// CRLF pair or LF is not included in the string. ///
/// The stream from which the line is to be read /// A line read from the stream returned as a byte array or if no bytes were readable from the stream /// If is public static byte[] ReadLineAsBytes(Stream stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); using (var memoryStream = new MemoryStream()) { while (true) { var justRead = stream.ReadByte(); if (justRead == -1 && memoryStream.Length > 0) break; // Check if we started at the end of the stream we read from // and we have not read anything from it yet if (justRead == -1 && memoryStream.Length == 0) return null; var readChar = (char) justRead; // Do not write \r or \n if (readChar != '\r' && readChar != '\n') memoryStream.WriteByte((byte) justRead); // Last point in CRLF pair if (readChar == '\n') break; } return memoryStream.ToArray(); } } #endregion #region ReadLineAsAscii /// /// Read a line from the stream. for more documentation. /// /// The stream to read from /// A line read from the stream or if nothing could be read from the stream /// If is public static string ReadLineAsAscii(Stream stream) { var readFromStream = ReadLineAsBytes(stream); return readFromStream != null ? Encoding.ASCII.GetString(readFromStream) : null; } #endregion } }