2002-01-23 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / FileStream.cs
1 //
2 // System.IO/FileStream.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9 using System;
10 using System.PAL;
11 using System.Runtime.InteropServices;
12 using System.Threading;
13
14 // fixme: I do not know how to handle errno when calling PInvoke functions
15 // fixme: emit the correct exceptions everywhere
16
17 namespace System.IO
18 {
19
20         public class FileStream : Stream
21         {
22                 private OpSys _os = Platform.OS;
23                 private IntPtr fdhandle;
24                 private FileAccess acc;
25                 private bool owner;
26                 
27                 public FileStream (IntPtr handle, FileAccess access)
28                         : this (handle, access, true, 0, false) {}
29
30                 public FileStream (IntPtr handle, FileAccess access, bool ownsHandle)
31                         : this (handle, access, ownsHandle, 0, false) {}
32                 
33                 public FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
34                         : this (handle, access, ownsHandle, bufferSize, false) {}
35                 
36                 public FileStream (IntPtr handle, FileAccess access, bool ownsHandle,
37                                    int bufferSize, bool isAsync)
38                 {
39                         fdhandle = handle;
40                         
41                         //acc = access;
42                         //owner = ownsHandle;
43                 }
44                 
45                 public FileStream (string name, FileMode mode)
46                         : this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, 0, false) {}
47
48                 public FileStream (string name, FileMode mode, FileAccess access)
49                         : this (name, mode, access, FileShare.ReadWrite, 0, false) {}
50
51                 public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
52                         : this (name, mode, access, share, 0, false) {}
53                 
54                 public FileStream (string name, FileMode mode, FileAccess access,
55                                    FileShare share, int buferSize)
56                         : this (name, mode, access, share, 0, false) {}
57
58                 // fixme: implement all share, buffer, async
59                 public FileStream (string name, FileMode mode, FileAccess access, FileShare share,
60                                    int buferSize, bool useAsync)
61                 {
62                         if (name == null)
63                                 throw new ArgumentNullException ();
64                         if (name == "" || name.IndexOfAny (Path.InvalidPathChars) != -1)
65                                 throw new ArgumentException ();
66
67                         fdhandle = _os.OpenFile (name, mode, access, share);
68                         
69                         /* Implement error checking, with some sort of access
70                            to the errno error reason
71                         if(fdhandle == error) {
72                                 throw new IOException();
73                         }
74                         */
75
76                         acc = access;
77                         owner = true;
78                 }
79                 
80                 public override bool CanRead
81                 {
82                         get {
83                                 switch (acc) {
84                                 case FileAccess.Read:
85                                 case FileAccess.ReadWrite:
86                                         return true;
87                                 case FileAccess.Write:
88                                 default:
89                                         return false;
90                                 }
91                         }
92                 }
93
94                 public override bool CanSeek
95                 {
96                         get {
97                                 // fixme: not alway true
98                                 return true;
99                         }
100                 }
101
102                 public override bool CanWrite
103                 {
104                         get {
105                                 switch (acc) {
106                                 case FileAccess.Write:
107                                 case FileAccess.ReadWrite:
108                                         return true;
109                                 default:
110                                         return false;
111                                 }
112                         }
113                 }
114
115                 unsafe public override long Length
116                 {
117                         get {
118                                 return _os.FileLength (fdhandle);
119                         }
120                 }
121
122                 public override long Position
123                 {
124                         get {
125                                 return _os.SeekFile (fdhandle, 0,  SeekOrigin.Current);
126                         }
127                         set {
128                                 _os.SeekFile (fdhandle, value, SeekOrigin.Begin);
129                         }
130                 }
131
132                 public override void Flush ()
133                 {
134                 }
135
136                 public override void Close ()
137                 {
138                         if (owner) {
139                                 _os.CloseFile (fdhandle);
140                         }
141                 }
142
143                 public unsafe override int Read (byte[] buffer,
144                                           int offset,
145                                           int count)
146                 {
147                         return _os.ReadFile (fdhandle, buffer, offset, count);
148                 }
149
150                 public unsafe override int ReadByte ()
151                 {
152                         byte[] val = new byte[1];
153                         int res = Read (val, 0, 1);
154                         
155                         if (res == -1)
156                                 throw new IOException();
157                         if (res == 0)
158                                 return -1;
159
160                         return val[0];
161                 }
162
163                 public override long Seek (long offset,
164                                            SeekOrigin origin)
165                 {
166                         return _os.SeekFile (fdhandle, offset, origin);
167                 }
168
169                 public override void SetLength (long value)
170                 {
171                         _os.SetLengthFile (fdhandle, value);
172                 }
173
174                 public unsafe override void Write (byte[] buffer,
175                                                    int offset,
176                                                    int count)
177                 {
178                         int res = _os.WriteFile (fdhandle, buffer, offset, count);
179                         
180                         if (res != count)
181                                 throw new IOException();
182                 }
183
184                 public unsafe override void WriteByte (byte value)
185                 {
186                         byte[] buf = new byte[1];
187
188                         buf[0] = value;
189
190                         Write (buf, 0, 1);
191                 }
192
193                 public virtual IntPtr Handle
194                 {
195                         get {
196                                 return(fdhandle);
197                         }
198                 }
199         }
200 }