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
144 this.checkSize = false;
145 }
146 catch (Exception ex)
147 {
148 ExceptionDispatchInfo.Capture(ex).Throw();
149 }
150 }
151
152 private void SwitchToFile()
153 {
154 TemporaryFile File = new TemporaryFile();
155 try
156 {
157 long Pos = this.stream.Position;
158 this.stream.Position = 0;
159 this.stream.CopyTo(File);
160
161 File.Position = Pos;
162 this.stream.Dispose();
163 this.stream = File;
164
165 this.checkSize = false;
166 }
167 catch (Exception ex)
168 {
169 ExceptionDispatchInfo.Capture(ex).Throw();
170 }
171 }
172
177 public override void Flush()
178 {
179 this.stream.Flush();
180 }
181
189 public override Task FlushAsync(CancellationToken cancellationToken)
190 {
191 return this.stream.FlushAsync(cancellationToken);
192 }
193
207 public override int Read(byte[] buffer, int offset, int count)
208 {
209 return this.stream.Read(buffer, offset, count);
210 }
211
226 public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
227 {
228 return this.stream.ReadAsync(buffer, offset, count, cancellationToken);
229 }
230
236 public override int ReadByte()
237 {
238 return this.stream.ReadByte();
239 }
240
248 public override long Seek(long offset, SeekOrigin origin)
249 {
250 return this.stream.Seek(offset, origin);
251 }
252
257 public override void SetLength(long value)
258 {
259 if (this.checkSize && value > this.thresholdBytes)
260 this.SwitchToFile();
261
262 this.stream.SetLength(value);
263 }
264
275 public override void Write(byte[] buffer, int offset, int count)
276 {
277 if (this.checkSize && this.stream.Position + count > this.thresholdBytes)
278 this.SwitchToFile();
279
280 this.stream.Write(buffer, offset, count);
281 }
282
294 public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
295 {
296 if (this.checkSize && this.stream.Position + count > this.thresholdBytes)
297 await this.SwitchToFileAsync(cancellationToken);
298
299 await this.stream.WriteAsync(buffer, offset, count, cancellationToken);
300 }
301
307 public override void WriteByte(byte value)
308 {
309 if (this.checkSize && this.stream.Position + 1 > this.thresholdBytes)
310 this.SwitchToFile();
311
312 this.stream.WriteByte(value);
313 }
314
321 protected override void Dispose(bool disposing)
322 {
323 this.stream?.Dispose();
324 this.stream = null;
325 }
326 }
327}
Class managing the contents of a temporary file. When the class is disposed, the temporary file is de...
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...