summary refs log tree commit diff
path: root/crypto/src/util/bzip2
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/util/bzip2')
-rw-r--r--crypto/src/util/bzip2/CBZip2InputStream.cs46
-rw-r--r--crypto/src/util/bzip2/CBZip2OutputStream.cs22
2 files changed, 50 insertions, 18 deletions
diff --git a/crypto/src/util/bzip2/CBZip2InputStream.cs b/crypto/src/util/bzip2/CBZip2InputStream.cs
index 08760f547..be085c517 100644
--- a/crypto/src/util/bzip2/CBZip2InputStream.cs
+++ b/crypto/src/util/bzip2/CBZip2InputStream.cs
@@ -193,7 +193,7 @@ namespace Org.BouncyCastle.Utilities.Bzip2
                 if (m_expectedStreamCrc != m_streamCrc)
                     throw new IOException("Stream CRC error");
 
-                BsFinishedWithStream();
+                // TODO If not a LeaveOpen stream, should we check that we are at the end of stream here?
                 streamEnd = true;
                 return;
             }
@@ -252,19 +252,33 @@ namespace Org.BouncyCastle.Utilities.Bzip2
             m_streamCrc = Integers.RotateLeft(m_streamCrc, 1) ^ blockFinalCrc;
         }
 
-        private void BsFinishedWithStream()
+        protected void Detach(bool disposing)
         {
-            try
+            if (disposing)
             {
-                if (this.bsStream != null)
+                ImplDisposing(disposeInput: false);
+            }
+            base.Dispose(disposing);
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                ImplDisposing(disposeInput: true);
+            }
+            base.Dispose(disposing);
+        }
+
+        private void ImplDisposing(bool disposeInput)
+        {
+            if (this.bsStream != null)
+            {
+                if (disposeInput)
                 {
                     this.bsStream.Dispose();
-                    this.bsStream = null;
                 }
-            }
-            catch
-            {
-                //ignore
+                this.bsStream = null;
             }
         }
 
@@ -806,4 +820,18 @@ namespace Org.BouncyCastle.Utilities.Bzip2
             return a;
         }
     }
+
+    public class CBZip2InputStreamLeaveOpen
+        : CBZip2InputStream
+    {
+        public CBZip2InputStreamLeaveOpen(Stream outStream)
+            : base(outStream)
+        {
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            Detach(disposing);
+        }
+    }
 }
diff --git a/crypto/src/util/bzip2/CBZip2OutputStream.cs b/crypto/src/util/bzip2/CBZip2OutputStream.cs
index d1400c7c4..10b76c677 100644
--- a/crypto/src/util/bzip2/CBZip2OutputStream.cs
+++ b/crypto/src/util/bzip2/CBZip2OutputStream.cs
@@ -424,11 +424,7 @@ namespace Org.BouncyCastle.Utilities.Bzip2
         {
             if (disposing)
             {
-                if (!closed)
-                {
-                    Finish();
-                    closed = true;
-                }
+                ImplDisposing(disposeOutput: false);
             }
             base.Dispose(disposing);
         }
@@ -437,14 +433,22 @@ namespace Org.BouncyCastle.Utilities.Bzip2
         {
             if (disposing)
             {
-                if (!closed)
+                ImplDisposing(disposeOutput: true);
+            }
+            base.Dispose(disposing);
+        }
+
+        private void ImplDisposing(bool disposeOutput)
+        {
+            if (!closed)
+            {
+                Finish();
+                closed = true;
+                if (disposeOutput)
                 {
-                    Finish();
-                    closed = true;
                     this.bsStream.Dispose();
                 }
             }
-            base.Dispose(disposing);
         }
 
         public void Finish()