Wed Nov 14 16:30:27 CET 2001 Paolo Molaro <lupus@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
13 // fixme: I do not know how to handle errno when calling PInvoke functions
14 // fixme: emit the correct exceptions everywhere
15
16 namespace System.IO
17 {
18
19         public class FileStream : Stream
20         {
21                 private OpSys _os = Platform.OS;
22                 private IntPtr fd;
23                 private FileAccess acc;
24                 private bool owner;
25                 
26                 public FileStream (IntPtr fd, FileAccess access)
27                         : this (fd, access, true, 0, false) {}
28
29                 public FileStream (IntPtr fd, FileAccess access, bool ownsHandle)
30                         : this (fd, access, ownsHandle, 0, false) {}
31                 
32                 public FileStream (IntPtr fd, FileAccess access, bool ownsHandle, int bufferSize)
33                         : this (fd, access, ownsHandle, bufferSize, false) {}
34                 
35                 public FileStream (IntPtr fd, FileAccess access, bool ownsHandle,
36                                    int bufferSize, bool isAsync)
37                 {
38                         fd = fd;
39                         //acc = access;
40                         //owner = ownsHandle;
41                 }
42                 
43                 public FileStream (string name, FileMode mode)
44                         : this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, 0, false) {}
45
46                 public FileStream (string name, FileMode mode, FileAccess access)
47                         : this (name, mode, access, FileShare.ReadWrite, 0, false) {}
48
49                 public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
50                         : this (name, mode, access, share, 0, false) {}
51                 
52                 public FileStream (string name, FileMode mode, FileAccess access,
53                                    FileShare share, int buferSize)
54                         : this (name, mode, access, share, 0, false) {}
55
56                 // fixme: implement all share, buffer, async
57                 public FileStream (string name, FileMode mode, FileAccess access, FileShare share,
58                                    int buferSize, bool useAsync)
59                 {
60                         if ((int)(fd = _os.OpenFile (name, mode, access, share)) == -1)
61                                 throw new IOException();
62
63                         acc = access;
64                         owner = true;
65                 }
66                 
67                 public override bool CanRead
68                 {
69                         get {
70                                 switch (acc) {
71                                 case FileAccess.Read:
72                                 case FileAccess.ReadWrite:
73                                         return true;
74                                 case FileAccess.Write:
75                                 default:
76                                         return false;
77                                 }
78                         }
79                 }
80
81                 public override bool CanSeek
82                 {
83                         get {
84                                 // fixme: not alway true
85                                 return true;
86                         }
87                 }
88
89                 public override bool CanWrite
90                 {
91                         get {
92                                 switch (acc) {
93                                 case FileAccess.Write:
94                                 case FileAccess.ReadWrite:
95                                         return true;
96                                 default:
97                                         return false;
98                                 }
99                         }
100                 }
101
102                 unsafe public override long Length
103                 {
104                         get {
105                                 return _os.FileLength (fd);
106                         }
107                 }
108
109                 public override long Position
110                 {
111                         get {
112                                 return _os.SeekFile (fd, 0,  SeekOrigin.Current);
113                         }
114                         set {
115                                 _os.SeekFile (fd, value, SeekOrigin.Begin);
116                         }
117                 }
118
119                 public override void Flush ()
120                 {
121                 }
122
123                 public override void Close ()
124                 {
125                         if (owner) {
126                                 _os.CloseFile (fd);
127                         }
128                 }
129
130                 public unsafe override int Read (byte[] buffer,
131                                           int offset,
132                                           int count)
133                 {
134                         return _os.ReadFile (fd, buffer, offset, count);
135                 }
136
137                 public unsafe override int ReadByte ()
138                 {
139                         byte[] val = new byte[1];
140                         int res = Read (val, 0, 1);
141                         
142                         if (res == -1)
143                                 throw new IOException();
144                         if (res == 0)
145                                 return -1;
146                         
147                         return val[0];
148                 }
149
150                 public override long Seek (long offset,
151                                            SeekOrigin origin)
152                 {
153                         return _os.SeekFile (fd, offset, origin);
154                 }
155
156                 public override void SetLength (long value)
157                 {
158                         int res;
159
160                         if ((res = _os.SetLengthFile (fd, value)) == -1)
161                                 throw new IOException();
162
163                         
164                 }
165
166                 public unsafe override void Write (byte[] buffer,
167                                                    int offset,
168                                                    int count)
169                 {
170                         int res = _os.WriteFile (fd, buffer, offset, count);
171                         
172                         if (res != count)
173                                 throw new IOException();
174                 }
175
176                 public unsafe override void WriteByte (byte value)
177                 {
178                         byte[] buf = new byte[1];
179
180                         buf[0] = value;
181
182                         Write (buf, 0, 1);
183                 }
184
185         }
186 }