int position;
int dirty_bytes;
#if NET_4_5
+ [NonSerialized]
Task<int> read_task;
#endif
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");
CheckIfClosedThrowDisposed ();
+ if (!CanWrite)
+ throw new NotSupportedException ("Cannot write to this stream.");
+
// reordered to avoid possible integer overflow
if (position > length - count)
Expand (position + count);
#if NET_4_5
+ public override Task CopyToAsync (Stream destination, int bufferSize, CancellationToken cancellationToken)
+ {
+ // TODO: Specialization but what for?
+ return base.CopyToAsync (destination, bufferSize, cancellationToken);
+ }
+
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
}