2008-08-22 Sebastien Pouliot <sebastien@ximian.com>
[mono-project.git] / mcs / class / corlib / System.IO.IsolatedStorage / MoonIsolatedStorageFileStream.cs
blob3ac4f5d9c673e21f5041f1b1fd77ae8538b0adfd
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)
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.
30 #if NET_2_1
31 using System;
32 using System.IO;
34 namespace System.IO.IsolatedStorage {
36 // NOTES:
37 // * Silverlight allows extending to more than AvailableFreeSpace (by up to 1024 bytes).
38 // This looks like a safety buffer.
40 [MonoTODO ("this needs to be quota-enabled")]
41 public class IsolatedStorageFileStream : FileStream {
43 IsolatedStorageFile container;
45 internal static string Verify (IsolatedStorageFile isf, string path)
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");
54 isf.PreCheck ();
55 return isf.Verify (path);
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)
62 container = isf;
65 public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
66 : base (Verify (isf, path), mode, access, FileShare.Read, DefaultBufferSize, false, true)
68 container = isf;
71 public IsolatedStorageFileStream (string path, FileMode mode, FileAccess access, FileShare share, IsolatedStorageFile isf)
72 : base (Verify (isf, path), mode, access, share, DefaultBufferSize, false, true)
74 container = isf;
77 protected override void Dispose (bool disposing)
79 // no PreCheck required
80 base.Dispose (disposing);
83 public override void Flush ()
85 container.PreCheck ();
86 base.Flush ();
89 public override int Read (byte [] buffer, int offset, int count)
91 container.PreCheck ();
92 return base.Read (buffer, offset, count);
95 public override int ReadByte ()
97 container.PreCheck ();
98 return base.ReadByte ();
101 public override long Seek (long offset, SeekOrigin origin)
103 container.PreCheck ();
104 return base.Seek (offset, origin);
107 public override void SetLength (long value)
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 ();
114 base.SetLength (value);
117 public override void Write (byte [] buffer, int offset, int count)
119 container.PreCheck ();
120 // FIXME: check quota, if we grow the file
121 base.Write (buffer, offset, count);
124 public override void WriteByte (byte value)
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);
133 public override bool CanRead {
134 get {
135 // no PreCheck required
136 return base.CanRead;
140 public override bool CanSeek {
141 get {
142 // no PreCheck required
143 return base.CanSeek;
147 public override bool CanWrite {
148 get {
149 // no PreCheck required
150 return base.CanWrite;
154 public override long Length {
155 get {
156 container.PreCheck ();
157 return base.Length;
161 public override long Position {
162 get {
163 container.PreCheck ();
164 return base.Position;
166 set {
167 container.PreCheck ();
168 base.Position = value;
172 public override IAsyncResult BeginRead (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
174 container.PreCheck ();
175 return base.BeginRead (buffer, offset, numBytes, userCallback, stateObject);
178 public override IAsyncResult BeginWrite (byte[] buffer, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
180 container.PreCheck ();
181 // FIXME: check quota, if we grow the file
182 return base.BeginWrite (buffer, offset, numBytes, userCallback, stateObject);
185 public override int EndRead (IAsyncResult asyncResult)
187 return base.EndRead (asyncResult);
190 public override void EndWrite (IAsyncResult asyncResult)
192 base.EndWrite (asyncResult);
196 #endif