Removing old build files. NAnt doesn't use them.
[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                         
141                         if (Read (val, 0, 1) != 1)
142                                 throw new IOException();
143                         
144                         return val[0];
145                 }
146
147                 public override long Seek (long offset,
148                                            SeekOrigin origin)
149                 {
150                         return _os.SeekFile (fd, offset, origin);
151                 }
152
153                 public override void SetLength (long value)
154                 {
155                         int res;
156
157                         if ((res = _os.SetLengthFile (fd, value)) == -1)
158                                 throw new IOException();
159
160                         
161                 }
162
163                 public unsafe override void Write (byte[] buffer,
164                                                    int offset,
165                                                    int count)
166                 {
167                         int res = _os.WriteFile (fd, buffer, offset, count);
168                         
169                         if (res != count)
170                                 throw new IOException();
171                 }
172
173                 public unsafe override void WriteByte (byte value)
174                 {
175                         byte[] buf = new byte[1];
176
177                         buf[0] = value;
178
179                         Write (buf, 0, 1);
180                 }
181
182         }
183 }