mirror of
https://github.com/ppy/osu
synced 2025-01-11 00:29:30 +00:00
e57d7d1205
`MemoryStreamArchiveReader` introduced in 0657b551964986fc5504a202eaa5e699d1c72f00 would previously use `MemoryStream.GetBuffer()` to retrieve the underlying byte buffer with stream data. However, this is not generally the method you would want, for two reasons: 1. It can fail if the stream wasn't created in the way that supports it. 2. As per https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.getbuffer?view=net-7.0#system-io-memorystream-getbuffer, it will return the _raw_ contents of the buffer, including potentially unused bytes. To fix, use `MemoryStream.ToArray()` instead, which avoids both pitfalls. Notably, `ToArray()` always returns the full contents of the buffer, regardless of `Position`, as documented in: https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.toarray?view=net-7.0#system-io-memorystream-toarray
31 lines
875 B
C#
31 lines
875 B
C#
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace osu.Game.IO.Archives
|
|
{
|
|
/// <summary>
|
|
/// Allows reading a single file from the provided memory stream.
|
|
/// </summary>
|
|
public class MemoryStreamArchiveReader : ArchiveReader
|
|
{
|
|
private readonly MemoryStream stream;
|
|
|
|
public MemoryStreamArchiveReader(MemoryStream stream, string filename)
|
|
: base(filename)
|
|
{
|
|
this.stream = stream;
|
|
}
|
|
|
|
public override Stream GetStream(string name) => new MemoryStream(stream.ToArray(), 0, (int)stream.Length);
|
|
|
|
public override void Dispose()
|
|
{
|
|
}
|
|
|
|
public override IEnumerable<string> Filenames => new[] { Name };
|
|
}
|
|
}
|