Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TemporaryStream.cs
1using System;
2using System.IO;
3using System.Runtime.ExceptionServices;
4using System.Threading;
5using System.Threading.Tasks;
6
8{
13 public class TemporaryStream : Stream
14 {
15 private static int defaultThreasholdBytes = 1024 * 1024;
16
20 public static int DefaultThreasholdBytes
21 {
22 get => defaultThreasholdBytes;
23 set
24 {
25 if (value <= 0)
26 throw new ArgumentOutOfRangeException("Must be positive.", nameof(DefaultThreasholdBytes));
27
28 defaultThreasholdBytes = value;
29 }
30 }
31
32 private readonly int thresholdBytes;
33 private Stream stream;
34 private bool checkSize;
35
42 {
43 }
44
50 public TemporaryStream(int ThresholdBytes)
51 {
52 this.thresholdBytes = ThresholdBytes;
53 this.stream = new MemoryStream();
54 this.checkSize = true;
55 }
56
60 public override long Position
61 {
62 get => this.stream.Position;
63 set => this.stream.Position = value;
64 }
65
69 public override long Length => this.stream.Length;
70
75 public override bool CanWrite => this.stream.CanWrite;
76
80 public override bool CanTimeout => this.stream.CanTimeout;
81
86 public override bool CanSeek => this.stream.CanSeek;
87
92 public override bool CanRead => this.stream.CanRead;
93
98 public override int ReadTimeout
99 {
100 get => this.stream.ReadTimeout;
101 set => this.stream.ReadTimeout = value;
102 }
103
108 public override int WriteTimeout
109 {
110 get => this.stream.WriteTimeout;
111 set => this.stream.WriteTimeout = value;
112 }
113
123 public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
124 {
125 if (this.checkSize && this.stream.Position + bufferSize > this.thresholdBytes)
126 await this.SwitchToFileAsync(cancellationToken);
127
128 await this.stream.CopyToAsync(destination, bufferSize, cancellationToken);
129 }
130
131 private async Task SwitchToFileAsync(CancellationToken cancellationToken)
132 {
133 TemporaryFile File = new TemporaryFile();
134 try
135 {
136 long Pos = this.stream.Position;
137 this.stream.Position = 0;
138 await this.stream.CopyToAsync(File, 81920, cancellationToken);
139
140 File.Position = Pos;
141 this.stream.Dispose();
142 this.stream = File;
143 File = null;
144
145 this.checkSize = false;
146 }
147 catch (Exception ex)
148 {
149 File?.Dispose();
150 ExceptionDispatchInfo.Capture(ex).Throw();
151 }
152 }
153
154 private void SwitchToFile()
155 {
156 TemporaryFile File = new TemporaryFile();
157 try
158 {
159 long Pos = this.stream.Position;
160 this.stream.Position = 0;
161 this.stream.CopyTo(File);
162
163 File.Position = Pos;
164 this.stream.Dispose();
165 this.stream = File;
166 File = null;
167
168 this.checkSize = false;
169 }
170 catch (Exception ex)
171 {
172 File?.Dispose();
173 ExceptionDispatchInfo.Capture(ex).Throw();
174 }
175 }
176
181 public override void Flush()
182 {
183 this.stream.Flush();
184 }
185
193 public override Task FlushAsync(CancellationToken cancellationToken)
194 {
195 return this.stream.FlushAsync(cancellationToken);
196 }
197
211 public override int Read(byte[] buffer, int offset, int count)
212 {
213 return this.stream.Read(buffer, offset, count);
214 }
215
230 public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
231 {
232 return this.stream.ReadAsync(buffer, offset, count, cancellationToken);
233 }
234
240 public override int ReadByte()
241 {
242 return this.stream.ReadByte();
243 }
244
252 public override long Seek(long offset, SeekOrigin origin)
253 {
254 return this.stream.Seek(offset, origin);
255 }
256
261 public override void SetLength(long value)
262 {
263 if (this.checkSize && value > this.thresholdBytes)
264 this.SwitchToFile();
265
266 this.stream.SetLength(value);
267 }
268
279 public override void Write(byte[] buffer, int offset, int count)
280 {
281 if (this.checkSize && this.stream.Position + count > this.thresholdBytes)
282 this.SwitchToFile();
283
284 this.stream.Write(buffer, offset, count);
285 }
286
298 public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
299 {
300 if (this.checkSize && this.stream.Position + count > this.thresholdBytes)
301 await this.SwitchToFileAsync(cancellationToken);
302
303 await this.stream.WriteAsync(buffer, offset, count, cancellationToken);
304 }
305
311 public override void WriteByte(byte value)
312 {
313 if (this.checkSize && this.stream.Position + 1 > this.thresholdBytes)
314 this.SwitchToFile();
315
316 this.stream.WriteByte(value);
317 }
318
325 protected override void Dispose(bool disposing)
326 {
327 this.stream?.Dispose();
328 this.stream = null;
329 }
330 }
331}
Class managing the contents of a temporary file. When the class is disposed, the temporary file is de...
override void Dispose(bool disposing)
Disposes of the object, and deletes the temporary file.
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Asynchronously reads the bytes from the current stream and writes them to another stream,...
override bool CanTimeout
Gets a value that determines whether the current stream can time out.
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
override bool CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writi...
TemporaryStream()
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Flush()
When overridden in a derived class, clears all buffers for this stream and causes any buffered data t...
override int WriteTimeout
Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write befor...
override int Read(byte[] buffer, int offset, int count)
When overridden in a derived class, reads a sequence of bytes from the current stream and advances th...
override bool CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeki...
override long Position
When overridden in a derived class, gets or sets the position within the current stream.
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the st...
override void Write(byte[] buffer, int offset, int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the...
override void WriteByte(byte value)
Writes a byte to the current position in the stream and advances the position within the stream by on...
static int DefaultThreasholdBytes
Default threashold before switching to temporary files from in-memory streams.
override int ReadByte()
Reads a byte from the stream and advances the position within the stream by one byte,...
override bool CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports readi...
TemporaryStream(int ThresholdBytes)
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override Task FlushAsync(CancellationToken cancellationToken)
Asynchronously clears all buffers for this stream, causes any buffered data to be written to the unde...
override void SetLength(long value)
When overridden in a derived class, sets the length of the current stream.
override long Seek(long offset, SeekOrigin origin)
When overridden in a derived class, sets the position within the current stream.
override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
override long Length
When overridden in a derived class, gets the length in bytes of the stream.
override int ReadTimeout
Gets or sets a value, in miliseconds, that determines how long the stream will attempt to read before...