zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace QiHe.CodeLib
{
    /// <summary>
    /// This class represents a Stream Helper.
    /// </summary>
    public class StreamHelper
    {
        /// <summary>
        /// Write Byte Order Mark(BOM) of streamWriter's Encoding
        /// to streamWriter's underlying stream.
        /// </summary>
        /// <param name="streamWriter">The stream writer.</param>
        public static void WriteByteOrderMark(StreamWriter streamWriter)
        {
            byte[] BOM = streamWriter.Encoding.GetPreamble();
            streamWriter.BaseStream.Write(BOM, 0, BOM.Length);
        }
 
        /// <summary>
        /// Writes the bytes.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="bytes">The bytes.</param>
        public static void WriteBytes(Stream stream, byte[] bytes)
        {
            stream.Write(bytes, 0, bytes.Length);
        }
 
        /// <summary>
        /// Reads the bytes.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        public static byte[] ReadBytes(Stream stream, int count)
        {
            int length = Math.Min((int)stream.Length, count);
            byte[] bytes = new byte[length];
            stream.Read(bytes, 0, bytes.Length);
            return bytes;
        }
 
        /// <summary>
        /// Reads the bytes.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="buffer">The buffer.</param>
        public static void ReadBytes(Stream stream, byte[] buffer)
        {
            stream.Read(buffer, 0, buffer.Length);
        }
 
        /// <summary>
        /// Reads to end.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public static byte[] ReadToEnd(Stream stream)
        {
            byte[] bytes = new byte[stream.Length - stream.Position];
            stream.Read(bytes, 0, bytes.Length);
            return bytes;
        }
 
        /// <summary>
        /// Search the first occurance of values from current position in stream
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="values"></param>
        /// <returns>the stream position after values or -1 if not fount</returns>
        public static long SearchStream(Stream stream, byte[] values)
        {
            int index = 0;
            int code = -1;
            do
            {
                code = stream.ReadByte();
                if (code == values[index])
                {
                    index++;
                }
                else if (index > 0)
                {
                    stream.Position -= index; index = 0;
                }
            }
            while (code != -1 && index < values.Length);
 
            if (index == values.Length) { return stream.Position; }
            else { return -1; }
        }
 
        /// <summary>
        /// Search the first occurance of values from current position in stream within start range
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="values"></param>
        /// <param name="maxlength">maximum bytes to seach</param>
        /// <returns></returns>
        public static long SearchStream(Stream stream, byte[] values, int maxlength)
        {
            int index = 0;
            int code = -1;
            long maxpos = stream.Position + maxlength;
            do
            {
                code = stream.ReadByte();
                if (code == values[index]) { index++; }
                else { stream.Position -= index; index = 0; }
            }
            while (code != -1 && index < values.Length && stream.Position < maxpos);
 
            if (index == values.Length) { return stream.Position; }
            else { return -1; }
        }
    }
}