using Microsoft.IO;
using System;
namespace Ryujinx.Common.Memory
{
public static class MemoryStreamManager
{
private static readonly RecyclableMemoryStreamManager _shared = new();
///
/// We don't expose the RecyclableMemoryStreamManager directly because version 2.x
/// returns them as MemoryStream. This Shared class is here to a) offer only the GetStream() versions we use
/// and b) return them as RecyclableMemoryStream so we don't have to cast.
///
public static class Shared
{
///
/// Retrieve a new MemoryStream object with no tag and a default initial capacity.
///
/// A RecyclableMemoryStream
public static RecyclableMemoryStream GetStream()
=> new(_shared);
///
/// Retrieve a new MemoryStream object with the contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
///
/// The new stream's position is set to the beginning of the stream when returned.
/// The byte buffer to copy data from
/// A RecyclableMemoryStream
public static RecyclableMemoryStream GetStream(byte[] buffer)
=> GetStream(Guid.NewGuid(), null, buffer, 0, buffer.Length);
///
/// Retrieve a new MemoryStream object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
///
/// The new stream's position is set to the beginning of the stream when returned.
/// The byte buffer to copy data from
/// A RecyclableMemoryStream
public static RecyclableMemoryStream GetStream(ReadOnlySpan buffer)
=> GetStream(Guid.NewGuid(), null, buffer);
///
/// Retrieve a new RecyclableMemoryStream object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
///
/// The new stream's position is set to the beginning of the stream when returned.
/// A unique identifier which can be used to trace usages of the stream
/// A tag which can be used to track the source of the stream
/// The byte buffer to copy data from
/// A RecyclableMemoryStream
public static RecyclableMemoryStream GetStream(Guid id, string tag, ReadOnlySpan buffer)
{
RecyclableMemoryStream stream = null;
try
{
stream = new RecyclableMemoryStream(_shared, id, tag, buffer.Length);
stream.Write(buffer);
stream.Position = 0;
return stream;
}
catch
{
stream?.Dispose();
throw;
}
}
///
/// Retrieve a new RecyclableMemoryStream object with the given tag and with contents copied from the provided
/// buffer. The provided buffer is not wrapped or used after construction.
///
/// The new stream's position is set to the beginning of the stream when returned
/// A unique identifier which can be used to trace usages of the stream
/// A tag which can be used to track the source of the stream
/// The byte buffer to copy data from
/// The offset from the start of the buffer to copy from
/// The number of bytes to copy from the buffer
/// A RecyclableMemoryStream
public static RecyclableMemoryStream GetStream(Guid id, string tag, byte[] buffer, int offset, int count)
{
RecyclableMemoryStream stream = null;
try
{
stream = new RecyclableMemoryStream(_shared, id, tag, count);
stream.Write(buffer, offset, count);
stream.Position = 0;
return stream;
}
catch
{
stream?.Dispose();
throw;
}
}
}
}
}