diff options
author | Larry Ewing <lewing@gmail.com> | 2012-09-14 22:27:54 +0400 |
---|---|---|
committer | Larry Ewing <lewing@gmail.com> | 2012-09-14 22:27:54 +0400 |
commit | 137a5c40cfecff099f3b5e97c425663ed2e8505d (patch) | |
tree | afc7fee50227ab8a1884cc3b967e8ee418514a75 /mcs/class/corlib/System.IO/MemoryStream.cs | |
parent | 7bd0ccde07c6d4c7e9e684cade2f481bbb55cb06 (diff) |
Rework exception handling in MemoryStream async methods
Return faulted tasks for runtime errors when using the MemoryStream
Async methods.
Diffstat (limited to 'mcs/class/corlib/System.IO/MemoryStream.cs')
-rw-r--r-- | mcs/class/corlib/System.IO/MemoryStream.cs | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/mcs/class/corlib/System.IO/MemoryStream.cs b/mcs/class/corlib/System.IO/MemoryStream.cs index fa874887d6f..4ce86df30b6 100644 --- a/mcs/class/corlib/System.IO/MemoryStream.cs +++ b/mcs/class/corlib/System.IO/MemoryStream.cs @@ -376,9 +376,6 @@ namespace System.IO public override void Write (byte [] buffer, int offset, int count) { - if (!canWrite) - throw new NotSupportedException ("Cannot write to this stream."); - if (buffer == null) throw new ArgumentNullException ("buffer"); @@ -391,6 +388,9 @@ namespace System.IO CheckIfClosedThrowDisposed (); + if (!CanWrite) + throw new NotSupportedException ("Cannot write to this stream."); + // reordered to avoid possible integer overflow if (position > length - count) Expand (position + count); @@ -436,33 +436,64 @@ namespace System.IO public override Task FlushAsync (CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) - return TaskConstants<int>.Canceled; + return TaskConstants.Canceled; - Flush (); - return TaskConstants.Finished; + try { + Flush (); + return TaskConstants.Finished; + } catch (Exception ex) { + return Task<object>.FromException (ex); + } } public override Task<int> ReadAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken) { + if (buffer == null) + throw new ArgumentNullException ("buffer"); + + if (offset < 0 || count < 0) + throw new ArgumentOutOfRangeException ("offset or count less than zero."); + + if (buffer.Length - offset < count ) + throw new ArgumentException ("offset+count", + "The size of the buffer is less than offset + count."); if (cancellationToken.IsCancellationRequested) return TaskConstants<int>.Canceled; - count = Read (buffer, offset, count); + try { + count = Read (buffer, offset, count); - // Try not to allocate a new task for every buffer read - if (read_task == null || read_task.Result != count) - read_task = Task<int>.FromResult (count); + // Try not to allocate a new task for every buffer read + if (read_task == null || read_task.Result != count) + read_task = Task<int>.FromResult (count); - return read_task; + return read_task; + } catch (Exception ex) { + return Task<int>.FromException (ex); + } } public override Task WriteAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken) { + if (buffer == null) + throw new ArgumentNullException ("buffer"); + + if (offset < 0 || count < 0) + throw new ArgumentOutOfRangeException (); + + if (buffer.Length - offset < count) + throw new ArgumentException ("offset+count", + "The size of the buffer is less than offset + count."); + if (cancellationToken.IsCancellationRequested) - return TaskConstants<int>.Canceled; + return TaskConstants.Canceled; - Write (buffer, offset, count); - return TaskConstants.Finished; + try { + Write (buffer, offset, count); + return TaskConstants.Finished; + } catch (Exception ex) { + return Task<object>.FromException (ex); + } } #endif } |