summary refs log tree commit diff
path: root/crypto/bzip2
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/bzip2')
-rw-r--r--crypto/bzip2/src/CBZip2InputStream.cs84
-rw-r--r--crypto/bzip2/src/CBZip2OutputStream.cs60
2 files changed, 32 insertions, 112 deletions
diff --git a/crypto/bzip2/src/CBZip2InputStream.cs b/crypto/bzip2/src/CBZip2InputStream.cs
index 82f397d38..7144b6d06 100644
--- a/crypto/bzip2/src/CBZip2InputStream.cs
+++ b/crypto/bzip2/src/CBZip2InputStream.cs
@@ -26,6 +26,7 @@ using System;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Apache.Bzip2
 {
@@ -38,7 +39,8 @@ namespace Org.BouncyCastle.Apache.Bzip2
     * <b>NB:</b> note this class has been modified to read the leading BZ from the
     * start of the BZIP2 stream to make it compatible with other PGP programs.
     */
-    public class CBZip2InputStream : Stream 
+    public class CBZip2InputStream
+        : BaseInputStream 
 	{
         private static void Cadvise() {
             //System.out.Println("CRC Error");
@@ -172,6 +174,27 @@ namespace Org.BouncyCastle.Apache.Bzip2
             return a;
         }
 
+        public override int Read(byte[] buffer, int offset, int count)
+        {
+            Streams.ValidateBufferArguments(buffer, offset, count);
+
+            /*
+             * TODO The base class implementation allows to return partial data if/when ReadByte throws. That would be
+             * be preferable here too (so don't override), but it would require that exceptions cause this instance to
+             * permanently fail, and that needs review.
+             */
+            int pos = 0;
+            while (pos < count)
+            {
+                int b = ReadByte();
+                if (b < 0)
+                    break;
+
+                buffer[offset + pos++] = (byte)b;
+            }
+            return pos;
+        }
+
         public override int ReadByte()
         {
             if (streamEnd)
@@ -911,62 +934,5 @@ namespace Org.BouncyCastle.Apache.Bzip2
             ll8 = new char[n];
             tt = new int[n];
         }
-    
-        public override void Flush() {
-        }
-    
-        public override int Read(byte[] buffer, int offset, int count) {
-            int c = -1;
-            int k;
-            for (k = 0; k < count; ++k) {
-                c = ReadByte();
-                if (c == -1)
-                    break;
-                buffer[k + offset] = (byte)c;
-            }
-            return k;
-        }
-    
-        public override long Seek(long offset, SeekOrigin origin) {
-            return 0;
-        }
-    
-        public override void SetLength(long value) {
-        }
-    
-        public override void Write(byte[] buffer, int offset, int count) {
-        }
-    
-        public override bool CanRead {
-            get {
-                return true;
-            }
-        }
-    
-        public override bool CanSeek {
-            get {
-                return false;
-            }
-        }
-    
-        public override bool CanWrite {
-            get {
-                return false;
-            }
-        }
-    
-        public override long Length {
-            get {
-                return 0;
-            }
-        }
-    
-        public override long Position {
-            get {
-                return 0;
-            }
-            set {
-            }
-        }
     }
-}
\ No newline at end of file
+}
diff --git a/crypto/bzip2/src/CBZip2OutputStream.cs b/crypto/bzip2/src/CBZip2OutputStream.cs
index e81f6ffc1..f56ca29b2 100644
--- a/crypto/bzip2/src/CBZip2OutputStream.cs
+++ b/crypto/bzip2/src/CBZip2OutputStream.cs
@@ -27,6 +27,7 @@ using System.Collections;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Apache.Bzip2
 {
@@ -40,7 +41,8 @@ namespace Org.BouncyCastle.Apache.Bzip2
     * <b>NB:</b> note this class has been modified to add a leading BZ to the
     * start of the BZIP2 stream to make it compatible with other PGP programs.
     */
-    public class CBZip2OutputStream : Stream 
+    public class CBZip2OutputStream
+        : BaseOutputStream 
 	{
         protected const int SETMASK = 1 << 21;
         protected const int CLEARMASK = ~SETMASK;
@@ -314,9 +316,9 @@ namespace Org.BouncyCastle.Apache.Bzip2
         * modified by Oliver Merkel, 010128
         *
         */
-        public override void WriteByte(byte b)
+        public override void WriteByte(byte value)
         {
-            if (currentByte == b)
+            if (currentByte == value)
             {
                 runLength++;
                 if (runLength > 254)
@@ -328,14 +330,14 @@ namespace Org.BouncyCastle.Apache.Bzip2
             }
             else if (currentByte == -1)
             {
-                currentByte = b;
+                currentByte = value;
                 runLength++;
             }
             else
             {
                 WriteRun();
                 runLength = 1;
-                currentByte = b;
+                currentByte = value;
             }
         }
 
@@ -1694,53 +1696,5 @@ namespace Org.BouncyCastle.Apache.Bzip2
 
             nMTF = wr;
         }
-
-        public override int Read(byte[] buffer, int offset, int count)
-        {
-            return 0;
-        }
-
-        public override long Seek(long offset, SeekOrigin origin)
-        {
-            return 0;
-        }
-
-        public override void SetLength(long value)
-        {
-        }
-
-        public override void Write(byte[] buffer, int offset, int count)
-        {
-            for (int k = 0; k < count; ++k)
-            {
-                WriteByte(buffer[k + offset]);
-            }
-        }
-
-        public override bool CanRead
-        {
-            get { return false; }
-        }
-
-        public override bool CanSeek
-        {
-            get { return false; }
-        }
-
-        public override bool CanWrite
-        {
-            get { return true; }
-        }
-
-        public override long Length
-        {
-            get { return 0; }
-        }
-
-        public override long Position
-        {
-            get { return 0; }
-            set {}
-        }
     }
 }