mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
Add extension method to get Memory<byte> from MemoryStream, attempting to do it without allocation and update CMapParser
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
public static class MemoryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a read-only <see cref="MemoryStream"/> from a ReadOnlyMemory<byte>.
|
||||
/// Gets a read-only <see cref="MemoryStream"/> from a ReadOnlyMemory<byte>, attempting without memory allocation.
|
||||
/// </summary>
|
||||
public static MemoryStream AsReadOnlyMemoryStream(this ReadOnlyMemory<byte> memory)
|
||||
{
|
||||
@@ -21,5 +21,18 @@
|
||||
|
||||
return new MemoryStream(memory.ToArray(), false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Memory<byte> from a <see cref="MemoryStream"/>, attempting without memory allocation.
|
||||
/// </summary>
|
||||
public static Memory<byte> AsMemory(this MemoryStream stream)
|
||||
{
|
||||
if (stream.TryGetBuffer(out ArraySegment<byte> segment))
|
||||
{
|
||||
return segment.AsMemory();
|
||||
}
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,12 +70,7 @@
|
||||
deflate.CopyTo(f);
|
||||
f.Flush();
|
||||
|
||||
if (output.TryGetBuffer(out var segment))
|
||||
{
|
||||
return segment.AsMemory();
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
return output.AsMemory();
|
||||
}
|
||||
}
|
||||
catch (InvalidDataException ex)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace UglyToad.PdfPig.Filters
|
||||
{
|
||||
using Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Lzw;
|
||||
@@ -131,12 +132,7 @@ namespace UglyToad.PdfPig.Filters
|
||||
|
||||
result.Flush();
|
||||
|
||||
if (output.TryGetBuffer(out var segment))
|
||||
{
|
||||
return segment.AsMemory();
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
return output.AsMemory();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
using Tokenization.Scanner;
|
||||
using Tokens;
|
||||
|
||||
internal class CMapParser
|
||||
internal sealed class CMapParser
|
||||
{
|
||||
private static readonly BaseFontRangeParser BaseFontRangeParser = new BaseFontRangeParser();
|
||||
private static readonly BaseFontCharacterParser BaseFontCharacterParser = new BaseFontCharacterParser();
|
||||
@@ -139,7 +139,7 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
ReadOnlyMemory<byte> bytes;
|
||||
using (var stream = typeof(CMapParser).Assembly.GetManifestResourceStream(resource))
|
||||
{
|
||||
if (stream is null)
|
||||
@@ -150,8 +150,7 @@
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
stream.CopyTo(memoryStream);
|
||||
|
||||
bytes = memoryStream.ToArray();
|
||||
bytes = memoryStream.AsMemory();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user