PrefetchedStream.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (C) 2016-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using System.Threading;
  12. namespace Utilities
  13. {
  14. public class PrefetchedStream : Stream
  15. {
  16. public const int CacheSize = 524288; // 512 KB
  17. public const int ReadAheadThershold = 65536; // 64 KB
  18. private long m_cacheOffset;
  19. private byte[] m_cache = new byte[0];
  20. private Stream m_stream;
  21. public PrefetchedStream(Stream stream)
  22. {
  23. m_stream = stream;
  24. if (m_stream.CanRead)
  25. {
  26. ScheduleReadAhead();
  27. }
  28. }
  29. private void ScheduleReadAhead()
  30. {
  31. new Thread(delegate()
  32. {
  33. ReadAhead();
  34. }).Start();
  35. }
  36. private void ReadAhead()
  37. {
  38. lock (m_stream)
  39. {
  40. long position = this.Position;
  41. bool isInCache = (position >= m_cacheOffset) && (position < m_cacheOffset + m_cache.Length);
  42. int bytesAlreadyRead;
  43. if (isInCache)
  44. {
  45. int offsetInCache = (int)(position - m_cacheOffset);
  46. bytesAlreadyRead = m_cache.Length - offsetInCache;
  47. byte[] oldCache = m_cache;
  48. m_cache = new byte[CacheSize];
  49. Array.Copy(oldCache, offsetInCache, m_cache, 0, bytesAlreadyRead);
  50. this.Position = position + bytesAlreadyRead;
  51. }
  52. else
  53. {
  54. bytesAlreadyRead = 0;
  55. m_cache = new byte[CacheSize];
  56. }
  57. m_cacheOffset = position;
  58. int bytesRead = m_stream.Read(m_cache, bytesAlreadyRead, CacheSize - bytesAlreadyRead);
  59. System.Diagnostics.Debug.Print("[{0}] {1} bytes have been read ahead from offset {2}.", DateTime.Now.ToString("HH:mm:ss:ffff"), bytesRead, position);
  60. if (bytesAlreadyRead + bytesRead < CacheSize)
  61. {
  62. // EOF, we must trim the response data array
  63. m_cache = ByteReader.ReadBytes(m_cache, 0, bytesAlreadyRead + bytesRead);
  64. }
  65. this.Position = position;
  66. }
  67. }
  68. public override int Read(byte[] buffer, int offset, int count)
  69. {
  70. int bytesCopied;
  71. lock (m_stream)
  72. {
  73. long position = this.Position;
  74. bool isInCache = (position >= m_cacheOffset) && (position < m_cacheOffset + m_cache.Length);
  75. if (isInCache)
  76. {
  77. int offsetInCache = (int)(position - m_cacheOffset);
  78. int bytesAvailableInCache = m_cache.Length - offsetInCache;
  79. bytesCopied = Math.Min(count, bytesAvailableInCache);
  80. Array.Copy(m_cache, offsetInCache, buffer, offset, bytesCopied);
  81. this.Position = position + bytesCopied;
  82. if (bytesCopied < count)
  83. {
  84. int bytesMissing = count - bytesCopied;
  85. int bytesRead = m_stream.Read(buffer, offset + bytesCopied, bytesMissing);
  86. }
  87. if (offsetInCache + ReadAheadThershold >= m_cache.Length)
  88. {
  89. ScheduleReadAhead();
  90. }
  91. }
  92. else
  93. {
  94. bytesCopied = m_stream.Read(buffer, 0, count);
  95. ScheduleReadAhead();
  96. }
  97. }
  98. return bytesCopied;
  99. }
  100. public override void Write(byte[] buffer, int offset, int count)
  101. {
  102. lock (m_stream)
  103. {
  104. m_cache = new byte[0];
  105. m_stream.Write(buffer, offset, count);
  106. }
  107. }
  108. public override void Close()
  109. {
  110. lock (m_stream)
  111. {
  112. m_stream.Close();
  113. }
  114. base.Close();
  115. }
  116. public override bool CanRead
  117. {
  118. get
  119. {
  120. return m_stream.CanRead;
  121. }
  122. }
  123. public override bool CanSeek
  124. {
  125. get
  126. {
  127. return m_stream.CanSeek;
  128. }
  129. }
  130. public override bool CanWrite
  131. {
  132. get
  133. {
  134. return m_stream.CanWrite;
  135. }
  136. }
  137. public override long Length
  138. {
  139. get
  140. {
  141. lock (m_stream)
  142. {
  143. return m_stream.Length;
  144. }
  145. }
  146. }
  147. public override long Position
  148. {
  149. get
  150. {
  151. lock (m_stream)
  152. {
  153. return m_stream.Position;
  154. }
  155. }
  156. set
  157. {
  158. lock (m_stream)
  159. {
  160. m_stream.Position = value;
  161. }
  162. }
  163. }
  164. public override void Flush()
  165. {
  166. lock (m_stream)
  167. {
  168. m_stream.Flush();
  169. }
  170. }
  171. public override long Seek(long offset, SeekOrigin origin)
  172. {
  173. lock (m_stream)
  174. {
  175. return m_stream.Seek(offset, origin);
  176. }
  177. }
  178. public override void SetLength(long value)
  179. {
  180. lock (m_stream)
  181. {
  182. m_stream.SetLength(value);
  183. }
  184. }
  185. }
  186. }