From 44844b4c0e0df30a72f1659b9904749f456f2545 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 6 Mar 2023 22:44:32 +0700 Subject: Support leaveOpen in decompressors --- crypto/src/util/zlib/ZInputStream.cs | 57 +++++++++++++++++++++++++++++++++-- crypto/src/util/zlib/ZOutputStream.cs | 57 ++++++++++++++--------------------- 2 files changed, 77 insertions(+), 37 deletions(-) (limited to 'crypto/src/util/zlib') diff --git a/crypto/src/util/zlib/ZInputStream.cs b/crypto/src/util/zlib/ZInputStream.cs index de1c27202..67b30a3ec 100644 --- a/crypto/src/util/zlib/ZInputStream.cs +++ b/crypto/src/util/zlib/ZInputStream.cs @@ -116,17 +116,34 @@ namespace Org.BouncyCastle.Utilities.Zlib this.z.avail_in = 0; } + protected void Detach(bool disposing) + { + if (disposing) + { + ImplDisposing(disposeInput: false); + } + base.Dispose(disposing); + } + protected override void Dispose(bool disposing) { if (disposing) { - if (!closed) + ImplDisposing(disposeInput: true); + } + base.Dispose(disposing); + } + + private void ImplDisposing(bool disposeInput) + { + if (!closed) + { + closed = true; + if (disposeInput) { - closed = true; input.Dispose(); } } - base.Dispose(disposing); } public virtual int FlushMode @@ -205,4 +222,38 @@ namespace Org.BouncyCastle.Utilities.Zlib get { return z.total_out; } } } + + public class ZInputStreamLeaveOpen + : ZInputStream + { + public ZInputStreamLeaveOpen(Stream input) + : base(input) + { + } + + public ZInputStreamLeaveOpen(Stream input, bool nowrap) + : base(input, nowrap) + { + } + + public ZInputStreamLeaveOpen(Stream input, ZStream z) + : base(input, z) + { + } + + public ZInputStreamLeaveOpen(Stream input, int level) + : base(input, level) + { + } + + public ZInputStreamLeaveOpen(Stream input, int level, bool nowrap) + : base(input, level, nowrap) + { + } + + protected override void Dispose(bool disposing) + { + Detach(disposing); + } + } } diff --git a/crypto/src/util/zlib/ZOutputStream.cs b/crypto/src/util/zlib/ZOutputStream.cs index ecf33cddf..04b7bb8e5 100644 --- a/crypto/src/util/zlib/ZOutputStream.cs +++ b/crypto/src/util/zlib/ZOutputStream.cs @@ -113,26 +113,7 @@ namespace Org.BouncyCastle.Utilities.Zlib { if (disposing) { - if (!closed) - { - try - { - try - { - Finish(); - } - catch (IOException) - { - // Ignore - } - } - finally - { - this.closed = true; - End(); - output = null; - } - } + ImplDisposing(disposeOutput: false); } base.Dispose(disposing); } @@ -141,29 +122,37 @@ namespace Org.BouncyCastle.Utilities.Zlib { if (disposing) { - if (!closed) + ImplDisposing(disposeOutput: true); + } + base.Dispose(disposing); + } + + private void ImplDisposing(bool disposeOutput) + { + if (!closed) + { + try { try { - try - { - Finish(); - } - catch (IOException) - { - // Ignore - } + Finish(); } - finally + catch (IOException) + { + // Ignore + } + } + finally + { + this.closed = true; + End(); + if (disposeOutput) { - this.closed = true; - End(); output.Dispose(); - output = null; } + output = null; } } - base.Dispose(disposing); } public virtual void End() -- cgit 1.4.1