1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Runtime
.Remoting
;
25 public abstract class AbstractStreamResource
: AbstractResource
27 public AbstractStreamResource()
31 ~
AbstractStreamResource()
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()
57 protected void Dispose(bool disposing
)
61 GC
.SuppressFinalize(this);
70 #region StreamHideCloseDelegate nested class
73 /// Do not allow closing and disposal of the
74 /// underlying <see cref="Stream"/>.
76 public class StreamHideCloseDelegate
: Stream
, IDisposable
78 private readonly Stream inner
;
80 public StreamHideCloseDelegate(Stream 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()
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()