-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
StreamUtility.cs
140 lines (119 loc) · 4.85 KB
/
StreamUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// StreamUtility.cs
//
// Author: Kees van Spelde <[email protected]>
//
// Copyright (c) 2013-2022 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.Collections.Generic;
using System.IO;
using System.Text;
namespace MsgReader.Helpers
{
/// <summary>
/// Utility to help reading bytes and strings of a <see cref="Stream"/>
/// </summary>
internal static class StreamUtility
{
#region ReadLineAsBytes
/// <summary>
/// Read a line from the stream.
/// A line is interpreted as all the bytes read until a CRLF or LF is encountered.<br/>
/// CRLF pair or LF is not included in the string.
/// </summary>
/// <param name="stream">The stream from which the line is to be read</param>
/// <returns>A line read from the stream returned as a byte array or <see langword="null"/> if no bytes were readable from the stream</returns>
/// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <see langword="null"/></exception>
public static byte[] ReadLineAsBytes(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
using (var memoryStream = StreamHelpers.Manager.GetStream())
{
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();
}
}
public static bool ReadLineAsBytes(ref byte[] stream, int currentPos, ref int EndPos)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
//using (var memoryStream = StreamHelpers.Manager.GetStream())
{
EndPos = currentPos;
if (EndPos > stream.Length) return false;
//bytes = new List<byte>();
while (true)
{
byte justRead = stream[EndPos];
EndPos++;
// Do not write \r or \n
if (justRead != '\r' && justRead != '\n')
{; }// bytes.Add(justRead);
// Last point in CRLF pair
if (justRead == '\n')
break;
}
return true;
//return bytes.ToArray();
}
}
#endregion
#region ReadLineAsAscii
/// <summary>
/// Read a line from the stream. <see cref="ReadLineAsBytes"/> for more documentation.
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <returns>A line read from the stream or <see langword="null"/> if nothing could be read from the stream</returns>
/// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <see langword="null"/></exception>
public static string ReadLineAsAscii(Stream stream)
{
var readFromStream = ReadLineAsBytes(stream);
return readFromStream != null ? Encoding.ASCII.GetString(readFromStream) : null;
}
public static string ReadLineAsAscii(ref byte[] stream, int currentPos, ref int endPos)
{
bool retval = ReadLineAsBytes(ref stream, currentPos, ref endPos);
if (!retval) return null;
var readFromStream = Encoding.ASCII.GetString(stream, currentPos, endPos - currentPos).Replace("\r\n","").Replace("\n", "");
return readFromStream;
//return readFromStream != null ? Encoding.ASCII.GetString(readFromStream) : null;
}
#endregion
}
}