2 // System.IO.MonoSyncFileStream.cs: Synchronous FileStream with
3 // asynchronous BeginRead/Write methods.
6 // Robert Jordan (robertj@gmx.net)
8 // Copyright (C) 2007 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.Remoting.Messaging;
37 internal class MonoSyncFileStream : FileStream
39 public MonoSyncFileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
40 : base (handle, access, ownsHandle, bufferSize, false)
44 delegate void WriteDelegate (byte [] buffer, int offset, int count);
46 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
47 AsyncCallback cback, object state)
50 throw new NotSupportedException ("This stream does not support writing");
53 throw new ArgumentNullException ("buffer");
56 throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
59 throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
61 WriteDelegate d = new WriteDelegate (this.Write);
62 return d.BeginInvoke (buffer, offset, count, cback, state);
65 public override void EndWrite (IAsyncResult asyncResult)
67 if (asyncResult == null)
68 throw new ArgumentNullException ("asyncResult");
70 AsyncResult ar = asyncResult as AsyncResult;
72 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
74 WriteDelegate d = ar.AsyncDelegate as WriteDelegate;
76 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
78 d.EndInvoke (asyncResult);
81 delegate int ReadDelegate (byte [] buffer, int offset, int count);
83 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
84 AsyncCallback cback, object state)
87 throw new NotSupportedException ("This stream does not support reading");
90 throw new ArgumentNullException ("buffer");
93 throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
96 throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
98 ReadDelegate d = new ReadDelegate (this.Read);
99 return d.BeginInvoke (buffer, offset, count, cback, state);
102 public override int EndRead (IAsyncResult asyncResult)
104 if (asyncResult == null)
105 throw new ArgumentNullException ("asyncResult");
107 AsyncResult ar = asyncResult as AsyncResult;
109 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
111 ReadDelegate d = ar.AsyncDelegate as ReadDelegate;
113 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
115 return d.EndInvoke (asyncResult);