3using System.Runtime.ExceptionServices;
5using System.Threading.Tasks;
15 private static int defaultThreasholdBytes = 1024 * 1024;
22 get => defaultThreasholdBytes;
28 defaultThreasholdBytes = value;
32 private readonly
int thresholdBytes;
33 private Stream stream;
34 private bool checkSize;
52 this.thresholdBytes = ThresholdBytes;
53 this.stream =
new MemoryStream();
54 this.checkSize =
true;
62 get => this.stream.Position;
63 set => this.stream.Position = value;
69 public override long Length => this.stream.Length;
75 public override bool CanWrite => this.stream.CanWrite;
80 public override bool CanTimeout => this.stream.CanTimeout;
86 public override bool CanSeek => this.stream.CanSeek;
92 public override bool CanRead => this.stream.CanRead;
100 get => this.stream.ReadTimeout;
101 set => this.stream.ReadTimeout = value;
110 get => this.stream.WriteTimeout;
111 set => this.stream.WriteTimeout = value;
123 public override async Task
CopyToAsync(Stream destination,
int bufferSize, CancellationToken cancellationToken)
125 if (this.checkSize && this.stream.Position + bufferSize >
this.thresholdBytes)
126 await this.SwitchToFileAsync(cancellationToken);
128 await this.stream.CopyToAsync(destination, bufferSize, cancellationToken);
131 private async Task SwitchToFileAsync(CancellationToken cancellationToken)
136 long Pos = this.stream.Position;
137 this.stream.Position = 0;
138 await this.stream.CopyToAsync(File, 81920, cancellationToken);
141 this.stream.Dispose();
144 this.checkSize =
false;
148 ExceptionDispatchInfo.Capture(ex).Throw();
152 private void SwitchToFile()
154 TemporaryFile File =
new TemporaryFile();
157 long Pos = this.stream.Position;
158 this.stream.Position = 0;
159 this.stream.CopyTo(File);
162 this.stream.Dispose();
165 this.checkSize =
false;
169 ExceptionDispatchInfo.Capture(ex).Throw();
189 public override Task
FlushAsync(CancellationToken cancellationToken)
191 return this.stream.FlushAsync(cancellationToken);
207 public override int Read(
byte[] buffer,
int offset,
int count)
209 return this.stream.Read(buffer, offset, count);
226 public override Task<int>
ReadAsync(
byte[] buffer,
int offset,
int count, CancellationToken cancellationToken)
228 return this.stream.ReadAsync(buffer, offset, count, cancellationToken);
238 return this.stream.ReadByte();
248 public override long Seek(
long offset, SeekOrigin origin)
250 return this.stream.Seek(offset, origin);
259 if (this.checkSize && value > this.thresholdBytes)
262 this.stream.SetLength(value);
275 public override void Write(
byte[] buffer,
int offset,
int count)
277 if (this.checkSize && this.stream.Position + count >
this.thresholdBytes)
280 this.stream.Write(buffer, offset, count);
294 public override async Task
WriteAsync(
byte[] buffer,
int offset,
int count, CancellationToken cancellationToken)
296 if (this.checkSize && this.stream.Position + count >
this.thresholdBytes)
297 await this.SwitchToFileAsync(cancellationToken);
299 await this.stream.WriteAsync(buffer, offset, count, cancellationToken);
309 if (this.checkSize && this.stream.Position + 1 >
this.thresholdBytes)
312 this.stream.WriteByte(value);
321 protected override void Dispose(
bool disposing)
323 this.stream?.Dispose();
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...