chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.IO;
 
namespace OpenTap
{
    internal class HybridStream : Stream
    {
        private class MemViewStream : Stream
        {
            private byte[] data;
            private long length, position;
 
            public MemViewStream(byte[] data, long length)
            {
                this.data = data;
                this.length = length;
            }
 
            public override bool CanRead { get { return true; } }
            public override bool CanSeek { get { return true; } }
            public override bool CanWrite { get { return false; } }
            public override long Length { get { return length; } }
 
            public override long Position
            {
                get { return position; }
                set
                {
                    if (value != position)
                    {
                        if (value < 0) throw new InvalidOperationException("Invalid position.");
                        if (value > length) throw new InvalidOperationException("Invalid position.");
 
                        position = value;
                    }
                }
            }
 
            public override void Flush()
            {
            }
 
            public override int Read(byte[] buffer, int offset, int count)
            {
                if (count < 0) throw new ArgumentOutOfRangeException("count");
                if (offset < 0) throw new ArgumentOutOfRangeException("count");
 
                long rest = length - position;
                if (count > rest) count = (int)rest;
 
                if (count > 0)
                {
                    Array.Copy(data, position, buffer, offset, count);
                    position += count;
                }
 
                return count;
            }
 
            public override long Seek(long offset, SeekOrigin origin)
            {
                long newpos = position;
 
                switch (origin)
                {
                    case SeekOrigin.Begin:
                        newpos = offset;
                        break;
                    case SeekOrigin.Current:
                        newpos = position + offset;
                        break;
                    case SeekOrigin.End:
                        newpos = length + offset;
                        break;
                }
 
                if (newpos < 0) newpos = 0;
                else if (newpos > length) newpos = length;
 
                position = newpos;
                return position;
            }
 
            public override void SetLength(long value)
            {
                throw new NotSupportedException();
            }
 
            public override void Write(byte[] buffer, int offset, int count)
            {
                throw new NotSupportedException();
            }
        }
 
        #region Private
        private object lockObject = new object();
 
        private Stream stream = new MemoryStream();
 
        /// <summary> Create a new instance with default parameters. </summary>
        public HybridStream() : this(FileSystemHelper.CreateTempFile(".opentap-log.txt"), 1024 * 1024)
        {
 
        }
 
        public HybridStream(string filename, int threshold)
        {
            this.Filename = filename;
            this.MemoryThreshold = threshold;
        }
        #endregion
 
        public long MemoryThreshold { get; set; }
        public string Filename { get; set; }
 
        #region Overrides
        public override bool CanRead { get { return false; } }
        public override bool CanSeek { get { return false; } }
        public override bool CanWrite { get { return true; } }
 
        public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
        public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
        public override void SetLength(long value) { throw new NotSupportedException(); }
 
        public override long Length { get { return GetLength(); } }
        public override long Position { get { return GetPosition(); } set { throw new NotSupportedException(); } }
        #endregion
 
        private long GetPosition()
        {
            lock (lockObject)
                return stream.Length;
        }
 
        private long GetLength()
        {
            lock (lockObject)
                return stream.Length;
        }
 
        public override void Flush()
        {
            lock (lockObject)
                stream.Flush();
        }
 
        public override void Write(byte[] buffer, int offset, int count)
        {
            lock (lockObject)
            {
                stream.Write(buffer, offset, count);
                if (stream is MemoryStream)
                    checkTrigger();
            }
        }
 
        private void checkTrigger()
        {
            if (stream.Length >= MemoryThreshold)
                ConvertToFile();
        }
 
        public void ConvertToFile()
        {
            lock (lockObject)
            {
                var fileStream = new FileStream(Filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 1024 * 16, FileOptions.DeleteOnClose);
                fileStream.Write(((MemoryStream)stream).GetBuffer(), 0, (int)stream.Length);
                stream.Dispose();
                stream = fileStream;
            }
        }
 
        public Stream GetViewStream()
        {
            lock (lockObject)
            {
                if (stream is FileStream)
                    return new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
                return new MemViewStream(((MemoryStream)stream).GetBuffer(), stream.Length);
            }
        }
 
        public override void Close()
        {
            lock (lockObject)
            {
                if (stream != null)
                {
                    stream.Close();
                    stream = null;
                }
            }
 
            base.Close();
        }
    }
 
}