2008-09-05 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.IO.IsolatedStorage / MoonIsolatedStorageFileStream.cs
1 //
2 // System.IO.IsolatedStorage.MoonIsolatedStorageFileStream
3 //
4 // Moonlight's implementation for the IsolatedStorageFileStream
5 // 
6 // Authors
7 //      Miguel de Icaza (miguel@novell.com)
8 //
9 // Copyright (C) 2007, 2008 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_1
31 using System;
32 using System.IO;
33
34 namespace System.IO.IsolatedStorage {
35
36         // NOTES: 
37         // * Silverlight allows extending to more than AvailableFreeSpace (by up to 1024 bytes).
38         //   This looks like a safety buffer.
39
40         [MonoTODO ("this needs to be quota-enabled")]
41         public class IsolatedStorageFileStream : FileStream {
42
43                 IsolatedStorageFile container;
44
45                 internal static string Verify (IsolatedStorageFile isf, string path)
46                 {
47                         if (path == null)
48                                 throw new ArgumentNullException ("path");
49                         if (path.Length == 0)
50                                 throw new ArgumentException ("path");
51                         if (isf == null)
52                                 throw new ArgumentNullException ("isf");
53
54                         isf.PreCheck ();
55                         return isf.Verify (path);
56                 }
57
58                 public IsolatedStorageFileStream (string path, FileMode mode, IsolatedStorageFile isf)
59                         : base (Verify (isf, path), mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), 
60                                 FileShare.Read, DefaultBufferSize, false, true)
61                 {
62                         container = isf;
63                 }
64
65                 public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
66                         : base (Verify (isf, path), mode, access, FileShare.Read, DefaultBufferSize, false, true)
67                 {
68                         container = isf;
69                 }
70
71                 public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
72                         : base (Verify (isf, path), mode, access, share, DefaultBufferSize, false, true)
73                 {
74                         container = isf;
75                 }
76
77                 protected override void Dispose (bool disposing)
78                 {
79                         // no PreCheck required
80                         base.Dispose (disposing);
81                 }
82
83                 public override void Flush ()
84                 {
85                         container.PreCheck ();
86                         base.Flush ();
87                 }
88
89                 public override int Read (byte [] buffer, int offset, int count)
90                 {
91                         container.PreCheck ();
92                         return base.Read (buffer, offset, count);
93                 }
94
95                 public override int ReadByte ()
96                 {
97                         container.PreCheck ();
98                         return base.ReadByte ();
99                 }
100
101                 public override long Seek (long offset, SeekOrigin origin)
102                 {
103                         container.PreCheck ();
104                         return base.Seek (offset, origin);
105                 }
106
107                 public override void SetLength (long value)
108                 {
109                         container.PreCheck ();
110                         // if we're getting bigger then we must ensure we fit in the available free space of our container
111                         if (!container.CanExtend (value - Length))
112                                 throw new IsolatedStorageException ();
113
114                         base.SetLength (value);
115                 }
116
117                 public override void Write (byte [] buffer, int offset, int count)
118                 {
119                         container.PreCheck ();
120                         // FIXME: check quota, if we grow the file
121                         base.Write (buffer, offset, count);
122                 }
123
124                 public override void WriteByte (byte value)
125                 {
126                         container.PreCheck ();
127                         // if we are in position to grow the file make sure we can extend it by one byte
128                         if ((Position == Length) && !container.CanExtend (1))
129                                 throw new IsolatedStorageException ();
130                         base.WriteByte (value);
131                 }
132
133                 public override bool CanRead {
134                         get {
135                                 // no PreCheck required
136                                 return base.CanRead;
137                         }
138                 }
139
140                 public override bool CanSeek {
141                         get {
142                                 // no PreCheck required
143                                 return base.CanSeek;
144                         }
145                 }
146
147                 public override bool CanWrite {
148                         get {
149                                 // no PreCheck required
150                                 return base.CanWrite;
151                         }
152                 }
153
154                 public override long Length {
155                         get {
156                                 container.PreCheck ();
157                                 return base.Length;
158                         }
159                 }
160
161                 public override long Position {
162                         get {
163                                 container.PreCheck ();
164                                 return base.Position;
165                         }
166                         set {
167                                 container.PreCheck ();
168                                 base.Position = value;
169                         }
170                 }
171
172                 public override IAsyncResult BeginRead (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
173                 {
174                         container.PreCheck ();
175                         return base.BeginRead (buffer, offset, numBytes, userCallback, stateObject);
176                 }
177
178                 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
179                 {
180                         container.PreCheck ();
181                         // FIXME: check quota, if we grow the file
182                         return base.BeginWrite (buffer, offset, numBytes, userCallback, stateObject);
183                 }
184
185                 public override int EndRead (IAsyncResult asyncResult)
186                 {
187                         container.PreCheck ();
188                         return base.EndRead (asyncResult);
189                 }
190
191                 public override void EndWrite (IAsyncResult asyncResult)
192                 {
193                         container.PreCheck ();
194                         base.EndWrite (asyncResult);
195                 }
196         }
197 }
198 #endif