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();
145 this.checkSize =
false;
150 ExceptionDispatchInfo.Capture(ex).Throw();
154 private void SwitchToFile()
156 TemporaryFile File =
new TemporaryFile();
159 long Pos = this.stream.Position;
160 this.stream.Position = 0;
161 this.stream.CopyTo(File);
164 this.stream.Dispose();
168 this.checkSize =
false;
173 ExceptionDispatchInfo.Capture(ex).Throw();
193 public override Task
FlushAsync(CancellationToken cancellationToken)
195 return this.stream.FlushAsync(cancellationToken);
211 public override int Read(
byte[] buffer,
int offset,
int count)
213 return this.stream.Read(buffer, offset, count);
230 public override Task<int>
ReadAsync(
byte[] buffer,
int offset,
int count, CancellationToken cancellationToken)
232 return this.stream.ReadAsync(buffer, offset, count, cancellationToken);
242 return this.stream.ReadByte();
252 public override long Seek(
long offset, SeekOrigin origin)
254 return this.stream.Seek(offset, origin);
263 if (this.checkSize && value > this.thresholdBytes)
266 this.stream.SetLength(value);
279 public override void Write(
byte[] buffer,
int offset,
int count)
281 if (this.checkSize && this.stream.Position + count >
this.thresholdBytes)
284 this.stream.Write(buffer, offset, count);
298 public override async Task
WriteAsync(
byte[] buffer,
int offset,
int count, CancellationToken cancellationToken)
300 if (this.checkSize && this.stream.Position + count >
this.thresholdBytes)
301 await this.SwitchToFileAsync(cancellationToken);
303 await this.stream.WriteAsync(buffer, offset, count, cancellationToken);
313 if (this.checkSize && this.stream.Position + 1 >
this.thresholdBytes)
316 this.stream.WriteByte(value);
325 protected override void Dispose(
bool disposing)
327 this.stream?.Dispose();
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...