MemoryStreamManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Microsoft.IO;
  2. using System;
  3. namespace Ryujinx.Common.Memory
  4. {
  5. public static class MemoryStreamManager
  6. {
  7. private static readonly RecyclableMemoryStreamManager _shared = new RecyclableMemoryStreamManager();
  8. /// <summary>
  9. /// We don't expose the <c>RecyclableMemoryStreamManager</c> directly because version 2.x
  10. /// returns them as <c>MemoryStream</c>. This Shared class is here to a) offer only the GetStream() versions we use
  11. /// and b) return them as <c>RecyclableMemoryStream</c> so we don't have to cast.
  12. /// </summary>
  13. public static class Shared
  14. {
  15. /// <summary>
  16. /// Retrieve a new <c>MemoryStream</c> object with no tag and a default initial capacity.
  17. /// </summary>
  18. /// <returns>A <c>RecyclableMemoryStream</c></returns>
  19. public static RecyclableMemoryStream GetStream()
  20. => new RecyclableMemoryStream(_shared);
  21. /// <summary>
  22. /// Retrieve a new <c>MemoryStream</c> object with the contents copied from the provided
  23. /// buffer. The provided buffer is not wrapped or used after construction.
  24. /// </summary>
  25. /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
  26. /// <param name="buffer">The byte buffer to copy data from</param>
  27. /// <returns>A <c>RecyclableMemoryStream</c></returns>
  28. public static RecyclableMemoryStream GetStream(byte[] buffer)
  29. => GetStream(Guid.NewGuid(), null, buffer, 0, buffer.Length);
  30. /// <summary>
  31. /// Retrieve a new <c>MemoryStream</c> object with the given tag and with contents copied from the provided
  32. /// buffer. The provided buffer is not wrapped or used after construction.
  33. /// </summary>
  34. /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
  35. /// <param name="buffer">The byte buffer to copy data from</param>
  36. /// <returns>A <c>RecyclableMemoryStream</c></returns>
  37. public static RecyclableMemoryStream GetStream(ReadOnlySpan<byte> buffer)
  38. => GetStream(Guid.NewGuid(), null, buffer);
  39. /// <summary>
  40. /// Retrieve a new <c>RecyclableMemoryStream</c> object with the given tag and with contents copied from the provided
  41. /// buffer. The provided buffer is not wrapped or used after construction.
  42. /// </summary>
  43. /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
  44. /// <param name="id">A unique identifier which can be used to trace usages of the stream</param>
  45. /// <param name="tag">A tag which can be used to track the source of the stream</param>
  46. /// <param name="buffer">The byte buffer to copy data from</param>
  47. /// <returns>A <c>RecyclableMemoryStream</c></returns>
  48. public static RecyclableMemoryStream GetStream(Guid id, string tag, ReadOnlySpan<byte> buffer)
  49. {
  50. RecyclableMemoryStream stream = null;
  51. try
  52. {
  53. stream = new RecyclableMemoryStream(_shared, id, tag, buffer.Length);
  54. stream.Write(buffer);
  55. stream.Position = 0;
  56. return stream;
  57. }
  58. catch
  59. {
  60. stream?.Dispose();
  61. throw;
  62. }
  63. }
  64. /// <summary>
  65. /// Retrieve a new <c>RecyclableMemoryStream</c> object with the given tag and with contents copied from the provided
  66. /// buffer. The provided buffer is not wrapped or used after construction.
  67. /// </summary>
  68. /// <remarks>The new stream's position is set to the beginning of the stream when returned</remarks>
  69. /// <param name="id">A unique identifier which can be used to trace usages of the stream</param>
  70. /// <param name="tag">A tag which can be used to track the source of the stream</param>
  71. /// <param name="buffer">The byte buffer to copy data from</param>
  72. /// <param name="offset">The offset from the start of the buffer to copy from</param>
  73. /// <param name="count">The number of bytes to copy from the buffer</param>
  74. /// <returns>A <c>RecyclableMemoryStream</c></returns>
  75. public static RecyclableMemoryStream GetStream(Guid id, string tag, byte[] buffer, int offset, int count)
  76. {
  77. RecyclableMemoryStream stream = null;
  78. try
  79. {
  80. stream = new RecyclableMemoryStream(_shared, id, tag, count);
  81. stream.Write(buffer, offset, count);
  82. stream.Position = 0;
  83. return stream;
  84. }
  85. catch
  86. {
  87. stream?.Dispose();
  88. throw;
  89. }
  90. }
  91. }
  92. }
  93. }