BufferPoolMemoryStream.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // System.IO.MemoryStream.cs
  3. //
  4. // Authors: Marcin Szczepanski (marcins@zipworld.com.au)
  5. // Patrik Torstensson
  6. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  7. //
  8. // (c) 2001,2002 Marcin Szczepanski, Patrik Torstensson
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright (C) 2004 Novell (http://www.novell.com)
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.IO;
  36. using Best.HTTP.Shared.Logger;
  37. using Best.HTTP.Shared.PlatformSupport.Memory;
  38. namespace Best.HTTP.Shared.Streams
  39. {
  40. /// <summary>
  41. /// This is a modified MemoryStream class to use VariableSizedBufferPool
  42. /// </summary>
  43. public sealed class BufferPoolMemoryStream : System.IO.Stream
  44. {
  45. bool canWrite;
  46. bool allowGetBuffer;
  47. int capacity;
  48. int length;
  49. byte[] internalBuffer;
  50. int initialIndex;
  51. bool expandable;
  52. bool streamClosed;
  53. int position;
  54. int dirty_bytes;
  55. bool releaseInternalBuffer;
  56. public BufferPoolMemoryStream() : this(0)
  57. {
  58. }
  59. public BufferPoolMemoryStream(int capacity)
  60. {
  61. if (capacity < 0)
  62. throw new ArgumentOutOfRangeException("capacity");
  63. canWrite = true;
  64. //internalBuffer = capacity > 0 ? BufferPool.Get(capacity, true) : BufferPool.NoData;
  65. //this.capacity = internalBuffer.Length;
  66. //
  67. //expandable = true;
  68. //allowGetBuffer = true;
  69. var buffer = capacity > 0 ? BufferPool.Get(capacity, true) : BufferPool.NoData;
  70. InternalConstructor(buffer, 0, buffer.Length, true, true, true, true);
  71. }
  72. public BufferPoolMemoryStream(byte[] buffer)
  73. {
  74. if (buffer == null)
  75. throw new ArgumentNullException("buffer");
  76. InternalConstructor(buffer, 0, buffer.Length, true, false, true, false);
  77. }
  78. public BufferPoolMemoryStream(byte[] buffer, bool writable)
  79. {
  80. if (buffer == null)
  81. throw new ArgumentNullException("buffer");
  82. InternalConstructor(buffer, 0, buffer.Length, writable, false, true, false);
  83. }
  84. public BufferPoolMemoryStream(byte[] buffer, int index, int count)
  85. {
  86. InternalConstructor(buffer, index, count, true, false, true, false);
  87. }
  88. public BufferPoolMemoryStream(byte[] buffer, int index, int count, bool writable)
  89. {
  90. InternalConstructor(buffer, index, count, writable, false, true, false);
  91. }
  92. public BufferPoolMemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
  93. {
  94. InternalConstructor(buffer, index, count, writable, publiclyVisible, true, false);
  95. }
  96. public BufferPoolMemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible, bool releaseBuffer)
  97. {
  98. InternalConstructor(buffer, index, count, writable, publiclyVisible, releaseBuffer, false);
  99. }
  100. public BufferPoolMemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible, bool releaseBuffer, bool canExpand)
  101. {
  102. InternalConstructor(buffer, index, count, writable, publiclyVisible, releaseBuffer, canExpand);
  103. }
  104. void InternalConstructor(byte[] buffer, int index, int count, bool writable, bool publicallyVisible, bool releaseBuffer, bool canExpand)
  105. {
  106. if (buffer == null)
  107. throw new ArgumentNullException("buffer");
  108. if (index < 0 || count < 0)
  109. throw new ArgumentOutOfRangeException("index or count is less than 0.");
  110. if (buffer.Length - index < count)
  111. throw new ArgumentException("index+count",
  112. "The size of the buffer is less than index + count.");
  113. canWrite = writable;
  114. internalBuffer = buffer;
  115. capacity = count + index;
  116. //length = capacity;
  117. length = 0;
  118. position = index;
  119. initialIndex = index;
  120. allowGetBuffer = publicallyVisible;
  121. releaseInternalBuffer = releaseBuffer;
  122. expandable = canExpand;
  123. }
  124. void CheckIfClosedThrowDisposed()
  125. {
  126. if (streamClosed)
  127. throw new ObjectDisposedException("MemoryStream");
  128. }
  129. public override bool CanRead
  130. {
  131. get { return !streamClosed; }
  132. }
  133. public override bool CanSeek
  134. {
  135. get { return !streamClosed; }
  136. }
  137. public override bool CanWrite
  138. {
  139. get { return (!streamClosed && canWrite); }
  140. }
  141. public int Capacity
  142. {
  143. get
  144. {
  145. CheckIfClosedThrowDisposed();
  146. return capacity - initialIndex;
  147. }
  148. set
  149. {
  150. CheckIfClosedThrowDisposed();
  151. if (value == capacity)
  152. return; // LAMENESS: see MemoryStreamTest.ConstructorFive
  153. if (!expandable)
  154. throw new NotSupportedException("Cannot expand this MemoryStream");
  155. if (value < 0 || value < length)
  156. throw new ArgumentOutOfRangeException("value",
  157. "New capacity cannot be negative or less than the current capacity " + value + " " + capacity);
  158. byte[] newBuffer = null;
  159. if (value != 0)
  160. {
  161. newBuffer = BufferPool.Get(value, true);
  162. Buffer.BlockCopy(internalBuffer, 0, newBuffer, 0, length);
  163. }
  164. dirty_bytes = 0; // discard any dirty area beyond previous length
  165. BufferPool.Release(internalBuffer);
  166. internalBuffer = newBuffer; // It's null when capacity is set to 0
  167. capacity = internalBuffer != null ? internalBuffer.Length : 0;
  168. }
  169. }
  170. public override long Length
  171. {
  172. get
  173. {
  174. // LAMESPEC: The spec says to throw an IOException if the
  175. // stream is closed and an ObjectDisposedException if
  176. // "methods were called after the stream was closed". What
  177. // is the difference?
  178. CheckIfClosedThrowDisposed();
  179. // This is ok for MemoryStreamTest.ConstructorFive
  180. return length - initialIndex;
  181. }
  182. }
  183. public override long Position
  184. {
  185. get
  186. {
  187. CheckIfClosedThrowDisposed();
  188. return position - initialIndex;
  189. }
  190. set
  191. {
  192. CheckIfClosedThrowDisposed();
  193. if (value < 0)
  194. throw new ArgumentOutOfRangeException("value",
  195. "Position cannot be negative");
  196. if (value > Int32.MaxValue)
  197. throw new ArgumentOutOfRangeException("value",
  198. "Position must be non-negative and less than 2^31 - 1 - origin");
  199. position = initialIndex + (int)value;
  200. }
  201. }
  202. protected override void Dispose (bool disposing)
  203. {
  204. streamClosed = true;
  205. expandable = false;
  206. if (disposing && internalBuffer != null && this.releaseInternalBuffer)
  207. BufferPool.Release(internalBuffer);
  208. internalBuffer = null;
  209. }
  210. public override void Flush()
  211. {
  212. // Do nothing
  213. }
  214. public byte[] GetBuffer()
  215. {
  216. if (!allowGetBuffer)
  217. throw new UnauthorizedAccessException();
  218. return internalBuffer;
  219. }
  220. public override int Read(byte[] buffer, int offset, int count)
  221. {
  222. CheckIfClosedThrowDisposed();
  223. if (buffer == null)
  224. throw new ArgumentNullException("buffer");
  225. if (offset < 0 || count < 0)
  226. throw new ArgumentOutOfRangeException("offset or count less than zero.");
  227. if (buffer.Length - offset < count)
  228. throw new ArgumentException("offset+count",
  229. "The size of the buffer is less than offset + count.");
  230. if (position >= length || count == 0)
  231. return 0;
  232. if (position > length - count)
  233. count = length - position;
  234. Buffer.BlockCopy(internalBuffer, position, buffer, offset, count);
  235. position += count;
  236. return count;
  237. }
  238. public override int ReadByte()
  239. {
  240. CheckIfClosedThrowDisposed();
  241. if (position >= length)
  242. return -1;
  243. return internalBuffer[position++];
  244. }
  245. public override long Seek(long offset, SeekOrigin loc)
  246. {
  247. CheckIfClosedThrowDisposed();
  248. // It's funny that they don't throw this exception for < Int32.MinValue
  249. if (offset > (long)Int32.MaxValue)
  250. throw new ArgumentOutOfRangeException("Offset out of range. " + offset);
  251. int refPoint;
  252. switch (loc)
  253. {
  254. case SeekOrigin.Begin:
  255. if (offset < 0)
  256. throw new IOException("Attempted to seek before start of MemoryStream.");
  257. refPoint = initialIndex;
  258. break;
  259. case SeekOrigin.Current:
  260. refPoint = position;
  261. break;
  262. case SeekOrigin.End:
  263. refPoint = length;
  264. break;
  265. default:
  266. throw new ArgumentException("loc", "Invalid SeekOrigin");
  267. }
  268. // LAMESPEC: My goodness, how may LAMESPECs are there in this
  269. // class! :) In the spec for the Position property it's stated
  270. // "The position must not be more than one byte beyond the end of the stream."
  271. // In the spec for seek it says "Seeking to any location beyond the length of the
  272. // stream is supported." That's a contradiction i'd say.
  273. // I guess seek can go anywhere but if you use position it may get moved back.
  274. refPoint += (int)offset;
  275. if (refPoint < initialIndex)
  276. throw new IOException("Attempted to seek before start of MemoryStream.");
  277. position = refPoint;
  278. return position;
  279. }
  280. int CalculateNewCapacity(int minimum)
  281. {
  282. if (minimum < 256)
  283. minimum = 256; // See GetBufferTwo test
  284. if (minimum < capacity * 2)
  285. minimum = capacity * 2;
  286. if (!UnityEngine.Mathf.IsPowerOfTwo(minimum))
  287. minimum = UnityEngine.Mathf.NextPowerOfTwo(minimum);
  288. return minimum;
  289. }
  290. void Expand(int newSize)
  291. {
  292. // We don't need to take into account the dirty bytes when incrementing the
  293. // Capacity, as changing it will only preserve the valid clear region.
  294. if (newSize > capacity)
  295. Capacity = CalculateNewCapacity(newSize);
  296. else if (dirty_bytes > 0)
  297. {
  298. Array.Clear(internalBuffer, length, dirty_bytes);
  299. dirty_bytes = 0;
  300. }
  301. }
  302. public override void SetLength(long value)
  303. {
  304. if (!expandable && value > capacity)
  305. throw new NotSupportedException("Expanding this MemoryStream is not supported");
  306. CheckIfClosedThrowDisposed();
  307. if (!canWrite)
  308. {
  309. throw new NotSupportedException("Cannot write to this MemoryStream");
  310. }
  311. // LAMESPEC: AGAIN! It says to throw this exception if value is
  312. // greater than "the maximum length of the MemoryStream". I haven't
  313. // seen anywhere mention what the maximum length of a MemoryStream is and
  314. // since we're this far this memory stream is expandable.
  315. if (value < 0 || (value + initialIndex) > (long)Int32.MaxValue)
  316. throw new ArgumentOutOfRangeException();
  317. int newSize = (int)value + initialIndex;
  318. if (newSize > length)
  319. Expand(newSize);
  320. else if (newSize < length) // Postpone the call to Array.Clear till expand time
  321. dirty_bytes += length - newSize;
  322. length = newSize;
  323. if (position > length)
  324. position = length;
  325. }
  326. public byte[] ToArray()
  327. {
  328. return ToArray(false, null);
  329. }
  330. public byte[] ToArray(bool canBeLarger, LoggingContext context)
  331. {
  332. int l = length - initialIndex;
  333. byte[] outBuffer = null;
  334. if (l > 0)
  335. {
  336. if (canBeLarger)
  337. outBuffer = BufferPool.Get(l, true, context);
  338. else
  339. outBuffer = new byte[l];
  340. }
  341. else
  342. {
  343. outBuffer = BufferPool.NoData;
  344. }
  345. if (internalBuffer != null)
  346. Buffer.BlockCopy(internalBuffer, initialIndex, outBuffer, 0, l);
  347. return outBuffer;
  348. }
  349. public BufferSegment ToBufferSegment()
  350. {
  351. int l = length - initialIndex;
  352. byte[] outBuffer = l > 0 ? BufferPool.Get(l, true) : BufferPool.NoData;
  353. if (internalBuffer != null)
  354. Buffer.BlockCopy(internalBuffer, initialIndex, outBuffer, 0, l);
  355. return new BufferSegment(outBuffer, 0, l);
  356. }
  357. public override void Write(byte[] buffer, int offset, int count)
  358. {
  359. CheckIfClosedThrowDisposed();
  360. if (!canWrite)
  361. throw new NotSupportedException("Cannot write to this stream.");
  362. if (buffer == null)
  363. throw new ArgumentNullException("buffer");
  364. if (offset < 0 || count < 0)
  365. throw new ArgumentOutOfRangeException();
  366. if (buffer.Length - offset < count)
  367. throw new ArgumentException("offset+count",
  368. "The size of the buffer is less than offset + count.");
  369. // reordered to avoid possible integer overflow
  370. if (position > length - count)
  371. Expand(position + count);
  372. Buffer.BlockCopy(buffer, offset, internalBuffer, position, count);
  373. position += count;
  374. if (position >= length)
  375. length = position;
  376. }
  377. public override void WriteByte(byte value)
  378. {
  379. CheckIfClosedThrowDisposed();
  380. if (!canWrite)
  381. throw new NotSupportedException("Cannot write to this stream.");
  382. if (position >= length)
  383. {
  384. Expand(position + 1);
  385. length = position + 1;
  386. }
  387. internalBuffer[position++] = value;
  388. }
  389. public void WriteTo(Stream stream)
  390. {
  391. CheckIfClosedThrowDisposed();
  392. if (stream == null)
  393. throw new ArgumentNullException("stream");
  394. stream.Write(internalBuffer, initialIndex, length - initialIndex);
  395. }
  396. }
  397. }