Do not try to prefetch write-only streams

This commit is contained in:
Tal Aloni 2016-12-27 00:25:19 +02:00
parent 8c7b17426c
commit af936b2217

View file

@ -25,22 +25,25 @@ namespace Utilities
public PrefetchedStream(Stream stream) public PrefetchedStream(Stream stream)
{ {
m_stream = stream; m_stream = stream;
new Thread(delegate() if (m_stream.CanRead)
{ {
lock (m_syncLock) new Thread(delegate()
{ {
m_cacheOffset = 0; lock (m_syncLock)
m_cache = new byte[CacheSize];
int bytesRead = m_stream.Read(m_cache, 0, CacheSize);
System.Diagnostics.Debug.Print("[{0}] bytes read {1}", DateTime.Now.ToString("HH:mm:ss:ffff"), bytesRead);
this.Position = 0;
if (bytesRead < CacheSize)
{ {
// EOF, we must trim the response data array m_cacheOffset = 0;
m_cache = ByteReader.ReadBytes(m_cache, 0, bytesRead); m_cache = new byte[CacheSize];
int bytesRead = m_stream.Read(m_cache, 0, CacheSize);
System.Diagnostics.Debug.Print("[{0}] bytes read {1}", DateTime.Now.ToString("HH:mm:ss:ffff"), bytesRead);
this.Position = 0;
if (bytesRead < CacheSize)
{
// EOF, we must trim the response data array
m_cache = ByteReader.ReadBytes(m_cache, 0, bytesRead);
}
} }
} }).Start();
}).Start(); }
} }
public override int Read(byte[] buffer, int offset, int count) public override int Read(byte[] buffer, int offset, int count)