2001-12-11 Dick Porter <dick@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                         fdhandle = _os.OpenFile (name, mode, access, share);
63                         
64                         /* Implement error checking, with some sort of access
65                            to the errno error reason
66                         if(fdhandle == error) {
67                                 throw new IOException();
68                         }
69                         */
70
71                         acc = access;
72                         owner = true;
73                 }
74                 
75                 public override bool CanRead
76                 {
77                         get {
78                                 switch (acc) {
79                                 case FileAccess.Read:
80                                 case FileAccess.ReadWrite:
81                                         return true;
82                                 case FileAccess.Write:
83                                 default:
84                                         return false;
85                                 }
86                         }
87                 }
88
89                 public override bool CanSeek
90                 {
91                         get {
92                                 // fixme: not alway true
93                                 return true;
94                         }
95                 }
96
97                 public override bool CanWrite
98                 {
99                         get {
100                                 switch (acc) {
101                                 case FileAccess.Write:
102                                 case FileAccess.ReadWrite:
103                                         return true;
104                                 default:
105                                         return false;
106                                 }
107                         }
108                 }
109
110                 unsafe public override long Length
111                 {
112                         get {
113                                 return _os.FileLength (fdhandle);
114                         }
115                 }
116
117                 public override long Position
118                 {
119                         get {
120                                 return _os.SeekFile (fdhandle, 0,  SeekOrigin.Current);
121                         }
122                         set {
123                                 _os.SeekFile (fdhandle, value, SeekOrigin.Begin);
124                         }
125                 }
126
127                 public override void Flush ()
128                 {
129                 }
130
131                 public override void Close ()
132                 {
133                         if (owner) {
134                                 _os.CloseFile (fdhandle);
135                         }
136                 }
137
138                 public unsafe override int Read (byte[] buffer,
139                                           int offset,
140                                           int count)
141                 {
142                         return _os.ReadFile (fdhandle, buffer, offset, count);
143                 }
144
145                 public unsafe override int ReadByte ()
146                 {
147                         byte[] val = new byte[1];
148                         int res = Read (val, 0, 1);
149                         
150                         if (res == -1)
151                                 throw new IOException();
152                         if (res == 0)
153                                 return -1;
154
155                         return val[0];
156                 }
157
158                 public override long Seek (long offset,
159                                            SeekOrigin origin)
160                 {
161                         return _os.SeekFile (fdhandle, offset, origin);
162                 }
163
164                 public override void SetLength (long value)
165                 {
166                         _os.SetLengthFile (fdhandle, value);
167                 }
168
169                 public unsafe override void Write (byte[] buffer,
170                                                    int offset,
171                                                    int count)
172                 {
173                         int res = _os.WriteFile (fdhandle, buffer, offset, count);
174                         
175                         if (res != count)
176                                 throw new IOException();
177                 }
178
179                 public unsafe override void WriteByte (byte value)
180                 {
181                         byte[] buf = new byte[1];
182
183                         buf[0] = value;
184
185                         Write (buf, 0, 1);
186                 }
187
188                 public virtual IntPtr Handle
189                 {
190                         get {
191                                 return(fdhandle);
192                         }
193                 }
194         }
195 }