mirror of https://github.com/SixLabors/ImageSharp
4 changed files with 52 additions and 115 deletions
@ -1,69 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Buffers; |
|
||||
using System.IO; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.IO |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// A memory stream constructed from a pooled buffer of known length.
|
|
||||
/// </summary>
|
|
||||
internal sealed class FixedCapacityPooledMemoryStream : MemoryStream |
|
||||
{ |
|
||||
private readonly IManagedByteBuffer buffer; |
|
||||
private bool isDisposed; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="FixedCapacityPooledMemoryStream"/> class.
|
|
||||
/// </summary>
|
|
||||
/// <param name="length">The length of the stream buffer to rent.</param>
|
|
||||
/// <param name="allocator">The allocator to rent the buffer from.</param>
|
|
||||
public FixedCapacityPooledMemoryStream(long length, MemoryAllocator allocator) |
|
||||
: this(RentBuffer(length, allocator)) => this.Length = length; |
|
||||
|
|
||||
private FixedCapacityPooledMemoryStream(IManagedByteBuffer buffer) |
|
||||
: base(buffer.Array) => this.buffer = buffer; |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override long Length { get; } |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override bool TryGetBuffer(out ArraySegment<byte> buffer) |
|
||||
{ |
|
||||
if (this.isDisposed) |
|
||||
{ |
|
||||
throw new ObjectDisposedException(this.GetType().Name); |
|
||||
} |
|
||||
|
|
||||
buffer = new ArraySegment<byte>(this.buffer.Array, 0, this.buffer.Length()); |
|
||||
return true; |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
protected override void Dispose(bool disposing) |
|
||||
{ |
|
||||
if (!this.isDisposed) |
|
||||
{ |
|
||||
this.isDisposed = true; |
|
||||
|
|
||||
if (disposing) |
|
||||
{ |
|
||||
this.buffer.Dispose(); |
|
||||
} |
|
||||
|
|
||||
base.Dispose(disposing); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// In the extrememly unlikely event someone ever gives us a stream
|
|
||||
// with length longer than int.MaxValue then we'll use something else.
|
|
||||
private static IManagedByteBuffer RentBuffer(long length, MemoryAllocator allocator) |
|
||||
{ |
|
||||
Guard.MustBeBetweenOrEqualTo(length, 0, int.MaxValue, nameof(length)); |
|
||||
return allocator.AllocateManagedByteBuffer((int)length); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,42 +0,0 @@ |
|||||
// Copyright (c) Six Labors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.IO; |
|
||||
using System.Linq; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
using SixLabors.ImageSharp.Tests.Memory; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Tests.IO |
|
||||
{ |
|
||||
public class FixedCapacityPooledMemoryStreamTests |
|
||||
{ |
|
||||
private readonly TestMemoryAllocator memoryAllocator = new TestMemoryAllocator(); |
|
||||
|
|
||||
[Theory] |
|
||||
[InlineData(1)] |
|
||||
[InlineData(512)] |
|
||||
public void RentsManagedBuffer(int length) |
|
||||
{ |
|
||||
MemoryStream ms = this.memoryAllocator.AllocateFixedCapacityMemoryStream(length); |
|
||||
Assert.Equal(length, this.memoryAllocator.AllocationLog.Single().Length); |
|
||||
ms.Dispose(); |
|
||||
Assert.Equal(1, this.memoryAllocator.ReturnLog.Count); |
|
||||
} |
|
||||
|
|
||||
[Theory] |
|
||||
[InlineData(42)] |
|
||||
[InlineData(2999)] |
|
||||
public void UsesRentedBuffer(int length) |
|
||||
{ |
|
||||
using MemoryStream ms = this.memoryAllocator.AllocateFixedCapacityMemoryStream(length); |
|
||||
ms.TryGetBuffer(out ArraySegment<byte> buffer); |
|
||||
byte[] array = buffer.Array; |
|
||||
Assert.Equal(array.GetHashCode(), this.memoryAllocator.AllocationLog.Single().HashCodeOfBuffer); |
|
||||
|
|
||||
ms.Write(new byte[] { 123 }); |
|
||||
Assert.Equal(123, array[0]); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue