Skip to content

Commit

Permalink
refactor(stream-handling): optimize stream management
Browse files Browse the repository at this point in the history
- Add CanSeek check before calling stream.Seek(0, SeekOrigin.Begin)
- Add condition to return byte array directly if stream is MemoryStream
- Use 'using' statement to ensure resource disposal with MemoryStream
  • Loading branch information
Mourad SADIQ authored and Mourad SADIQ committed Oct 4, 2024
1 parent 0e03f33 commit 87c2bd1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/PdfLibCore/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,17 @@ protected override void Dispose(FPDF_DOCUMENT handle)

private static byte[] GetBytes(Stream stream)
{
stream.Seek(0, SeekOrigin.Begin);
var ms = new MemoryStream();
if (stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}

if (stream is MemoryStream memoryStream)
{
return memoryStream.ToArray();
}

using var ms = new MemoryStream();
stream.CopyTo(ms);
return ms.ToArray();
}
Expand Down

0 comments on commit 87c2bd1

Please sign in to comment.