[runtime] Fix test_op_il_seq_point in amd64.
[mono.git] / mcs / class / System / System.IO / MonoSyncFileStream.cs
1 // 
2 // System.IO.MonoSyncFileStream.cs: Synchronous FileStream with
3 //     asynchronous BeginRead/Write methods.
4 //
5 // Authors:
6 //     Robert Jordan (robertj@gmx.net)
7 //
8 // Copyright (C) 2007 Novell, Inc. (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.Runtime.Remoting.Messaging;
34
35 namespace System.IO
36 {
37         internal class MonoSyncFileStream : FileStream
38         {
39                 public MonoSyncFileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
40                         : base (handle, access, ownsHandle, bufferSize, false)
41                 {
42                 }
43
44                 delegate void WriteDelegate (byte [] buffer, int offset, int count);
45
46                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
47                                                         AsyncCallback cback, object state)
48                 {
49                         if (!CanWrite)
50                                 throw new NotSupportedException ("This stream does not support writing");
51
52                         if (buffer == null)
53                                 throw new ArgumentNullException ("buffer");
54
55                         if (count < 0)
56                                 throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
57
58                         if (offset < 0)
59                                 throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
60
61                         WriteDelegate d = new WriteDelegate (this.Write);
62                         return d.BeginInvoke (buffer, offset, count, cback, state);
63                 }
64
65                 public override void EndWrite (IAsyncResult asyncResult)
66                 {
67                         if (asyncResult == null)
68                                 throw new ArgumentNullException ("asyncResult");
69
70                         AsyncResult ar = asyncResult as AsyncResult;
71                         if (ar == null)
72                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
73
74                         WriteDelegate d = ar.AsyncDelegate as WriteDelegate;
75                         if (d == null)
76                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
77
78                         d.EndInvoke (asyncResult);
79                 }
80
81                 delegate int ReadDelegate (byte [] buffer, int offset, int count);
82
83                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
84                                                         AsyncCallback cback, object state)
85                 {
86                         if (!CanRead)
87                                 throw new NotSupportedException ("This stream does not support reading");
88
89                         if (buffer == null)
90                                 throw new ArgumentNullException ("buffer");
91
92                         if (count < 0)
93                                 throw new ArgumentOutOfRangeException ("count", "Must be >= 0");
94
95                         if (offset < 0)
96                                 throw new ArgumentOutOfRangeException ("offset", "Must be >= 0");
97
98                         ReadDelegate d = new ReadDelegate (this.Read);
99                         return d.BeginInvoke (buffer, offset, count, cback, state);
100                 }
101
102                 public override int EndRead (IAsyncResult asyncResult)
103                 {
104                         if (asyncResult == null)
105                                 throw new ArgumentNullException ("asyncResult");
106
107                         AsyncResult ar = asyncResult as AsyncResult;
108                         if (ar == null)
109                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
110
111                         ReadDelegate d = ar.AsyncDelegate as ReadDelegate;
112                         if (d == null)
113                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
114
115                         return d.EndInvoke (asyncResult);
116                 }
117
118         }
119 }