mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-14 19:05:01 +08:00
Add new flag to control, weather we want to dispose the base stream.
Let the base stream throw in case of using unsupported method (Ex. Seek, Read, etc..)
This commit is contained in:
@@ -20,11 +20,14 @@
|
|||||||
|
|
||||||
public Stream Stream { get; private set; }
|
public Stream Stream { get; private set; }
|
||||||
|
|
||||||
|
public bool DisposeStream { get; set; }
|
||||||
|
|
||||||
public PdfStreamWriter() : this(new MemoryStream()) { }
|
public PdfStreamWriter() : this(new MemoryStream()) { }
|
||||||
|
|
||||||
public PdfStreamWriter(Stream baseStream)
|
public PdfStreamWriter(Stream baseStream, bool disposeStream = true)
|
||||||
{
|
{
|
||||||
Stream = baseStream ?? throw new ArgumentNullException(nameof(baseStream));
|
Stream = baseStream ?? throw new ArgumentNullException(nameof(baseStream));
|
||||||
|
DisposeStream = disposeStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Flush(decimal version, IndirectReferenceToken catalogReference)
|
public void Flush(decimal version, IndirectReferenceToken catalogReference)
|
||||||
@@ -110,15 +113,11 @@
|
|||||||
|
|
||||||
public byte[] ToArray()
|
public byte[] ToArray()
|
||||||
{
|
{
|
||||||
if (!Stream.CanSeek)
|
|
||||||
throw new NotSupportedException($"{Stream.GetType()} can't seek");
|
|
||||||
|
|
||||||
var currentPosition = Stream.Position;
|
var currentPosition = Stream.Position;
|
||||||
Stream.Seek(0, SeekOrigin.Begin);
|
Stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
|
||||||
var bytes = new byte[Stream.Length];
|
var bytes = new byte[Stream.Length];
|
||||||
|
|
||||||
// Should we slice the reading into smaller chunks?
|
|
||||||
if (Stream.Read(bytes, 0, bytes.Length) != bytes.Length)
|
if (Stream.Read(bytes, 0, bytes.Length) != bytes.Length)
|
||||||
throw new Exception("Unable to read all the bytes from stream");
|
throw new Exception("Unable to read all the bytes from stream");
|
||||||
|
|
||||||
@@ -129,6 +128,12 @@
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
if (!DisposeStream)
|
||||||
|
{
|
||||||
|
Stream = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Stream?.Dispose();
|
Stream?.Dispose();
|
||||||
Stream = null;
|
Stream = null;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user