Introducing IController - just a bare bone extract interface at this point
[castle.git] / Core / Castle.Core / Resource / AbstractStreamResource.cs
blobddffc743ceaafc1c4a21723c5d0e5881dca923fd
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core.Resource
17 using System;
18 using System.IO;
19 using System.Runtime.Remoting;
20 using System.Text;
22 /// <summary>
23 ///
24 /// </summary>
25 public abstract class AbstractStreamResource : AbstractResource
27 public AbstractStreamResource()
31 ~AbstractStreamResource()
33 Dispose(false);
36 protected abstract Stream Stream { get; }
38 public override TextReader GetStreamReader()
40 // The StreamReader can't take ownership of the stream
41 // that's why we use StreamHideCloseDelegate
42 return new StreamReader(new StreamHideCloseDelegate(Stream));
45 public override TextReader GetStreamReader(Encoding encoding)
47 // The StreamReader can't take ownership of the stream
48 // that's why we use StreamHideCloseDelegate
49 return new StreamReader(new StreamHideCloseDelegate(Stream), encoding);
52 public override void Dispose()
54 Dispose(true);
57 protected void Dispose(bool disposing)
59 if (disposing)
61 GC.SuppressFinalize(this);
64 if (Stream != null)
66 Stream.Close();
70 #region StreamHideCloseDelegate nested class
72 /// <summary>
73 /// Do not allow closing and disposal of the
74 /// underlying <see cref="Stream"/>.
75 /// </summary>
76 public class StreamHideCloseDelegate : Stream, IDisposable
78 private readonly Stream inner;
80 public StreamHideCloseDelegate(Stream inner)
82 this.inner = inner;
85 #region Stream implementation
87 public override bool CanRead
89 get { return inner.CanRead; }
92 public override bool CanSeek
94 get { return inner.CanSeek; }
97 public override bool CanWrite
99 get { return inner.CanWrite; }
102 public override long Length
104 get { return inner.Length; }
107 public override long Position
109 get { return inner.Position; }
110 set { inner.Position = value; }
113 public override void Flush()
115 inner.Flush();
118 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
120 return inner.BeginRead(buffer, offset, count, callback, state);
123 public override int EndRead(IAsyncResult asyncResult)
125 return inner.EndRead(asyncResult);
128 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
130 return inner.BeginWrite(buffer, offset, count, callback, state);
133 public override void EndWrite(IAsyncResult asyncResult)
135 inner.EndWrite(asyncResult);
138 public override long Seek(long offset, SeekOrigin origin)
140 return inner.Seek(offset, origin);
143 public override void SetLength(long value)
145 inner.SetLength(value);
148 public override int Read(byte[] buffer, int offset, int count)
150 return inner.Read(buffer, offset, count);
153 public override int ReadByte()
155 return inner.ReadByte();
158 public override void Write(byte[] buffer, int offset, int count)
160 inner.Write(buffer, offset, count);
163 public override void WriteByte(byte value)
165 inner.WriteByte(value);
168 public override object InitializeLifetimeService()
170 return inner.InitializeLifetimeService();
173 public override ObjRef CreateObjRef(Type requestedType)
175 return inner.CreateObjRef(requestedType);
178 public override void Close()
182 #endregion
185 #endregion