My favorites
▼
|
Sign in
csharptest-net
CSharpTest.Net's Code Library
Project Home
Downloads
Issues
Source
Repository:
default
wiki
Checkout
Browse
Changes
Clones
Source path:
hg
/
src
/
Library
/
IO
/
StreamCache.cs
Branch:
default
Tag:
<none>
v1.10.1024.336
v1.10.1124.358
v1.10.420.164
v1.10.607.213
v1.10.913.269
v1.11.426.305
v1.11.924.348
v1.9.1004.144
‹59104bd26e63
4e0bd400d42a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#region Copyright 2011 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.IO;
using System.Threading;
using CSharpTest.Net.Bases;
using CSharpTest.Net.Interfaces;
namespace CSharpTest.Net.IO
{
/// <summary>
/// Provides a simple means of caching several streams on a single file and for a thread
/// to quickly exclusive access to one of those streams. This class provides the base
/// implementation used by FileStreamCache and FragmentedFile.
/// </summary>
public class StreamCache : Disposable, IFactory<Stream>
{
readonly IFactory<Stream> _streamFactory;
readonly Stream[] _streams;
readonly Mutex[] _handles;
/// <summary>
/// Constructs the stream cache allowing one stream per thread
/// </summary>
public StreamCache(IFactory<Stream> streamFactory)
: this(streamFactory, Environment.ProcessorCount)
{ }
/// <summary>
/// Constructs the stream cache with the maximum allowed stream items
/// </summary>
public StreamCache(IFactory<Stream> streamFactory, int maxItem)
{
_streamFactory = streamFactory;
_streams = new Stream[maxItem];
_handles = new Mutex[maxItem];
for (int i = 0; i < maxItem; i++)
_handles[i] = new Mutex();
}
/// <summary></summary>
protected override void Dispose(bool disposing)
{
for (int i = 0; i < _handles.Length; i++)
_handles[i].Close();
for (int i = 0; i < _streams.Length; i++)
if (_streams[i] != null)
_streams[i].Dispose();
}
private void InvalidHandle(Mutex ownerHandle)
{
for (int i = 0; i < _handles.Length; i++)
{
if(ReferenceEquals(_handles[i], ownerHandle))
_handles[i] = new Mutex();
}
}
/// <summary>
/// Waits for a stream to become available and returns a wrapper on that stream. Just dispose like
/// any other stream to return the resource to the stream pool.
/// </summary>
public Stream Open() { return Open(FileAccess.ReadWrite); }
Stream IFactory<Stream>.Create() { return Open(FileAccess.ReadWrite); }
/// <summary>
/// Waits for a stream to become available and returns a wrapper on that stream. Just dispose like
/// any other stream to return the resource to the stream pool.
/// </summary>
public Stream Open(FileAccess access)
{
int handle;
try { handle = WaitHandle.WaitAny(_handles); }
catch (AbandonedMutexException ae)
{ handle = ae.MutexIndex; }
Stream stream = _streams[handle];
if (stream == null || !stream.CanRead)
{
try { }
finally
{
_streams[handle] = stream = _streamFactory.Create();
}
}
if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
return new CachedStream(this, stream, access, _handles[handle]);
}
private class CachedStream : AggregateStream
{
private const FileAccess NoAccess = 0;
private readonly StreamCache _cache;
private readonly Mutex _ownerHandle;
private FileAccess _fileAccess;
public CachedStream(StreamCache cache, Stream rawStream, FileAccess access, Mutex ownerHandle) : base(rawStream)
{
_cache = cache;
_ownerHandle = ownerHandle;
_fileAccess = access;
}
~CachedStream() { GC.SuppressFinalize(this); Dispose(false); }
public override void Close() { Dispose(true); }
protected override void Dispose(bool disposing)
{
if (_fileAccess != NoAccess)
{
if (disposing && Stream.CanWrite)
Stream.Flush();
_fileAccess = NoAccess;
if (disposing)
try { _ownerHandle.ReleaseMutex(); } catch (ObjectDisposedException) { }
else
_cache.InvalidHandle(_ownerHandle);
base.Stream = null;
}
}
private void IsNotDisposed()
{
if (_fileAccess == NoAccess)
throw new ObjectDisposedException(GetType().FullName);
}
public override bool CanRead { get { return (_fileAccess & FileAccess.Read) == FileAccess.Read && Stream.CanRead; } }
public override bool CanSeek { get { return _fileAccess != NoAccess && Stream.CanSeek; } }
public override bool CanWrite { get { return (_fileAccess & FileAccess.Write) == FileAccess.Write && Stream.CanWrite; } }
public override void SetLength(long value)
{
IsNotDisposed();
Check.Assert<InvalidOperationException>(CanWrite);
base.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
IsNotDisposed();
Check.Assert<InvalidOperationException>(CanRead);
return base.Read(buffer, offset, count);
}
public override int ReadByte()
{
IsNotDisposed();
Check.Assert<InvalidOperationException>(CanRead);
return base.ReadByte();
}
public override void Write(byte[] buffer, int offset, int count)
{
IsNotDisposed();
Check.Assert<InvalidOperationException>(CanWrite);
base.Write(buffer, offset, count);
}
public override void WriteByte(byte value)
{
IsNotDisposed();
Check.Assert<InvalidOperationException>(CanWrite);
base.WriteByte(value);
}
}
}
}
Show details
Hide details
Change log
2902ff73dbaf
by rogerk on Sep 24, 2011
Diff
Release version 1.11.924.348
Go to:
...sTree.Test/BPlusTree.Test.csproj
...ree.Test/TestBPlusTreeOptions.cs
...Collections/BPlusTree.Options.cs
...sTree/Properties/AssemblyInfo.cs
...PlusTree/Storage/Storage.Disk.cs
/src/CSBuild.exe
/src/CSBuild4.exe
/src/CSBuild4.exe.config
/src/Library/Bases/Comparable.cs
...y/Commands/CommandInterpreter.cs
.../Library/Commands/HelpDisplay.cs
/src/Library/Crypto/AESCryptoKey.cs
/src/Library/Crypto/Hash.cs
...brary/Crypto/HashDerivedBytes.cs
/src/Library/Crypto/HashStream.cs
...brary/Crypto/ModifiedRijndael.cs
/src/Library/Crypto/PBKDF2.cs
/src/Library/Crypto/PasswordKey.cs
...y/Crypto/SecureTransferClient.cs
.../Crypto/SecureTransferMessage.cs
...y/Crypto/SecureTransferServer.cs
/src/Library/Data/CsvReader.cs
.../Library/Html/XhtmlValidation.cs
...brary/Html/XmlLightAttributes.cs
...Library/Html/XmlLightDocument.cs
.../Library/Html/XmlLightElement.cs
...brary/Html/XmlLightInterfaces.cs
/src/Library/IO/AggregateStream.cs
/src/Library/IO/BaseStream.cs
/src/Library/IO/ClampedStream.cs
/src/Library/IO/CombinedStream.cs
.../Library/IO/MarshallingStream.cs
/src/Library/IO/NonClosingStream.cs
/src/Library/IO/ReplaceFile.cs
...rary/IO/SegmentedMemoryStream.cs
/src/Library/IO/StreamCache.cs
/src/Library/IO/TransactFile.cs
...y/Interfaces/DefaultFactories.cs
...ry/IpcChannel/IpcEventMessage.cs
...Library.Test/Library.Test.csproj
...brary.Test/TestCmdInterpreter.cs
...ry/Library.Test/TestCsvReader.cs
...y/Library.Test/TestEncryption.cs
...ry/Library.Test/TestFactories.cs
...brary.Test/TestFragmentedFile.cs
...Library/Library.Test/TestHash.cs
...y/Library.Test/TestHtmlParser.cs
...ary/Library.Test/TestPassword.cs
.../Library.Test/TestReplaceFile.cs
...brary.Test/TestSecureTransfer.cs
Project members,
sign in
to write a code review
Older revisions
59104bd26e63
by rogerk on Apr 26, 2011
Diff
Release version 1.11.426.305
All revisions of this file
File info
Size: 7042 bytes, 190 lines
View raw file
Powered by
Google Project Hosting