|
|
@@ -59,6 +59,11 @@
|
|
|
/// </summary>
|
|
|
private bool _canBlockLastRead;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Indicates whether the current <see cref="PipeStream"/> is disposed.
|
|
|
+ /// </summary>
|
|
|
+ private bool _isDisposed;
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
#region Public properties
|
|
|
@@ -84,11 +89,21 @@
|
|
|
/// <value>
|
|
|
/// <c>true</c> if block last read method before the buffer is empty; otherwise, <c>false</c>.
|
|
|
/// </value>
|
|
|
+ /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
|
|
|
public bool BlockLastReadBuffer
|
|
|
{
|
|
|
- get { return this._canBlockLastRead; }
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (_isDisposed)
|
|
|
+ throw CreateObjectDisposedException();
|
|
|
+
|
|
|
+ return this._canBlockLastRead;
|
|
|
+ }
|
|
|
set
|
|
|
{
|
|
|
+ if (_isDisposed)
|
|
|
+ throw CreateObjectDisposedException();
|
|
|
+
|
|
|
this._canBlockLastRead = value;
|
|
|
|
|
|
// when turning off the block last read, signal Read() that it may now read the rest of the buffer.
|
|
|
@@ -102,41 +117,40 @@
|
|
|
|
|
|
#region Stream overide methods
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
|
|
|
- ///</summary>
|
|
|
- ///
|
|
|
- ///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception><filterpriority>2</filterpriority>
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
|
|
|
+ /// </summary>
|
|
|
+ /// <exception cref="IOException">An I/O error occurs.</exception>
|
|
|
+ /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
|
|
|
public override void Flush()
|
|
|
{
|
|
|
+ if (_isDisposed)
|
|
|
+ throw CreateObjectDisposedException();
|
|
|
+
|
|
|
this._isFlushed = true;
|
|
|
lock (this._buffer)
|
|
|
Monitor.Pulse(this._buffer);
|
|
|
}
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, sets the position within the current stream.
|
|
|
- ///</summary>
|
|
|
- ///<returns>
|
|
|
- ///The new position within the current stream.
|
|
|
- ///</returns>
|
|
|
- ///<param name="offset">A byte offset relative to the origin parameter. </param>
|
|
|
- ///<param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position. </param>
|
|
|
- ///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
|
|
|
- ///<exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
|
|
|
- ///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, sets the position within the current stream.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>
|
|
|
+ /// The new position within the current stream.
|
|
|
+ /// </returns>
|
|
|
+ /// <param name="offset">A byte offset relative to the origin parameter.</param>
|
|
|
+ /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
|
|
|
+ /// <exception cref="NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output.</exception>
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
{
|
|
|
throw new NotSupportedException();
|
|
|
}
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, sets the length of the current stream.
|
|
|
- ///</summary>
|
|
|
- ///<param name="value">The desired length of the current stream in bytes. </param>
|
|
|
- ///<exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
|
|
|
- ///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
|
|
|
- ///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>2</filterpriority>
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, sets the length of the current stream.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="value">The desired length of the current stream in bytes.</param>
|
|
|
+ /// <exception cref="NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.</exception>
|
|
|
public override void SetLength(long value)
|
|
|
{
|
|
|
throw new NotSupportedException();
|
|
|
@@ -146,17 +160,17 @@
|
|
|
///When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
|
|
///</summary>
|
|
|
///<returns>
|
|
|
- ///The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
|
|
|
+ ///The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the end of the stream has been reached.
|
|
|
///</returns>
|
|
|
- ///<param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream. </param>
|
|
|
- ///<param name="count">The maximum number of bytes to be read from the current stream. </param>
|
|
|
- ///<param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source. </param>
|
|
|
- ///<exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
|
|
|
- ///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
|
|
|
- ///<exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
|
|
|
- ///<exception cref="T:System.ArgumentNullException">buffer is null. </exception>
|
|
|
- ///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
|
|
|
- ///<exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception><filterpriority>1</filterpriority>
|
|
|
+ ///<param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
|
|
|
+ ///<param name="count">The maximum number of bytes to be read from the current stream.</param>
|
|
|
+ ///<param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
|
|
|
+ ///<exception cref="ArgumentException">The sum of offset and count is larger than the buffer length.</exception>
|
|
|
+ ///<exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
|
|
|
+ ///<exception cref="NotSupportedException">The stream does not support reading.</exception>
|
|
|
+ ///<exception cref="ArgumentNullException">buffer is null.</exception>
|
|
|
+ ///<exception cref="IOException">An I/O error occurs.</exception>
|
|
|
+ ///<exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
{
|
|
|
if (offset != 0)
|
|
|
@@ -164,12 +178,13 @@
|
|
|
if (buffer == null)
|
|
|
throw new ArgumentNullException("buffer");
|
|
|
if (offset + count > buffer.Length)
|
|
|
- throw new ArgumentException("The sum of offset and count is greater than the buffer length. ");
|
|
|
+ throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
|
|
|
if (offset < 0 || count < 0)
|
|
|
throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
|
|
|
if (BlockLastReadBuffer && count >= _maxBufferLength)
|
|
|
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "count({0}) > mMaxBufferLength({1})", count, _maxBufferLength));
|
|
|
-
|
|
|
+ if (_isDisposed)
|
|
|
+ throw CreateObjectDisposedException();
|
|
|
if (count == 0)
|
|
|
return 0;
|
|
|
|
|
|
@@ -205,23 +220,25 @@
|
|
|
///<summary>
|
|
|
///When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
|
|
///</summary>
|
|
|
- ///<param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream. </param>
|
|
|
- ///<param name="count">The number of bytes to be written to the current stream. </param>
|
|
|
- ///<param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream. </param>
|
|
|
- ///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
|
|
|
- ///<exception cref="T:System.NotSupportedException">The stream does not support writing. </exception>
|
|
|
- ///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
|
|
|
- ///<exception cref="T:System.ArgumentNullException">buffer is null. </exception>
|
|
|
- ///<exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
|
|
|
- ///<exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception><filterpriority>1</filterpriority>
|
|
|
+ ///<param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
|
|
|
+ ///<param name="count">The number of bytes to be written to the current stream.</param>
|
|
|
+ ///<param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
|
|
|
+ ///<exception cref="IOException">An I/O error occurs.</exception>
|
|
|
+ ///<exception cref="NotSupportedException">The stream does not support writing.</exception>
|
|
|
+ ///<exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
|
|
|
+ ///<exception cref="ArgumentNullException">buffer is null.</exception>
|
|
|
+ ///<exception cref="ArgumentException">The sum of offset and count is greater than the buffer length.</exception>
|
|
|
+ ///<exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
{
|
|
|
if (buffer == null)
|
|
|
throw new ArgumentNullException("buffer");
|
|
|
if (offset + count > buffer.Length)
|
|
|
- throw new ArgumentException("The sum of offset and count is greater than the buffer length. ");
|
|
|
+ throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
|
|
|
if (offset < 0 || count < 0)
|
|
|
throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
|
|
|
+ if (_isDisposed)
|
|
|
+ throw CreateObjectDisposedException();
|
|
|
if (count == 0)
|
|
|
return;
|
|
|
|
|
|
@@ -243,65 +260,76 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
+ protected override void Dispose(bool disposing)
|
|
|
+ {
|
|
|
+ base.Dispose(disposing);
|
|
|
+
|
|
|
+ _isDisposed = true;
|
|
|
+ }
|
|
|
+
|
|
|
///<summary>
|
|
|
///When overridden in a derived class, gets a value indicating whether the current stream supports reading.
|
|
|
///</summary>
|
|
|
///<returns>
|
|
|
///true if the stream supports reading; otherwise, false.
|
|
|
///</returns>
|
|
|
- ///<filterpriority>1</filterpriority>
|
|
|
public override bool CanRead
|
|
|
{
|
|
|
- get { return true; }
|
|
|
+ get { return !_isDisposed; }
|
|
|
}
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
|
|
|
- ///</summary>
|
|
|
- ///<returns>
|
|
|
- ///true if the stream supports seeking; otherwise, false.
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>
|
|
|
+ /// <c>true</c> if the stream supports seeking; otherwise, <c>false</c>.
|
|
|
///</returns>
|
|
|
- ///<filterpriority>1</filterpriority>
|
|
|
public override bool CanSeek
|
|
|
{
|
|
|
get { return false; }
|
|
|
}
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, gets a value indicating whether the current stream supports writing.
|
|
|
- ///</summary>
|
|
|
- ///<returns>
|
|
|
- ///true if the stream supports writing; otherwise, false.
|
|
|
- ///</returns>
|
|
|
- ///<filterpriority>1</filterpriority>
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>
|
|
|
+ /// <c>true</c> if the stream supports writing; otherwise, <c>false</c>.
|
|
|
+ /// </returns>
|
|
|
public override bool CanWrite
|
|
|
{
|
|
|
- get { return true; }
|
|
|
+ get { return !_isDisposed; }
|
|
|
}
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, gets the length in bytes of the stream.
|
|
|
- ///</summary>
|
|
|
- ///<returns>
|
|
|
- ///A long value representing the length of the stream in bytes.
|
|
|
- ///</returns>
|
|
|
- ///
|
|
|
- ///<exception cref="T:System.NotSupportedException">A class derived from Stream does not support seeking. </exception>
|
|
|
- ///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, gets the length in bytes of the stream.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>
|
|
|
+ /// A long value representing the length of the stream in bytes.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="NotSupportedException">A class derived from Stream does not support seeking.</exception>
|
|
|
+ /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
|
|
|
public override long Length
|
|
|
{
|
|
|
- get { return this._buffer.Count; }
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (_isDisposed)
|
|
|
+ throw CreateObjectDisposedException();
|
|
|
+
|
|
|
+ return this._buffer.Count;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- ///<summary>
|
|
|
- ///When overridden in a derived class, gets or sets the position within the current stream.
|
|
|
- ///</summary>
|
|
|
- ///<returns>
|
|
|
- ///The current position within the stream.
|
|
|
- ///</returns>
|
|
|
- ///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
|
|
|
- ///<exception cref="T:System.NotSupportedException">The stream does not support seeking. </exception>
|
|
|
- ///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
|
|
|
+ /// <summary>
|
|
|
+ /// When overridden in a derived class, gets or sets the position within the current stream.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>
|
|
|
+ /// The current position within the stream.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="NotSupportedException">The stream does not support seeking.</exception>
|
|
|
public override long Position
|
|
|
{
|
|
|
get { return 0; }
|
|
|
@@ -309,5 +337,10 @@
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
+
|
|
|
+ private ObjectDisposedException CreateObjectDisposedException()
|
|
|
+ {
|
|
|
+ return new ObjectDisposedException(GetType().FullName);
|
|
|
+ }
|
|
|
}
|
|
|
}
|