summary refs log tree commit diff
path: root/crypto/src/asn1/BEROctetStringGenerator.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/BEROctetStringGenerator.cs')
-rw-r--r--crypto/src/asn1/BEROctetStringGenerator.cs56
1 files changed, 28 insertions, 28 deletions
diff --git a/crypto/src/asn1/BEROctetStringGenerator.cs b/crypto/src/asn1/BEROctetStringGenerator.cs
index f34538f38..0bef886f2 100644
--- a/crypto/src/asn1/BEROctetStringGenerator.cs
+++ b/crypto/src/asn1/BEROctetStringGenerator.cs
@@ -72,35 +72,35 @@ namespace Org.BouncyCastle.Asn1
 				}
 			}
 
-			public override void Write(
-				byte[] buf,
-				int    offset,
-				int    len)
+			public override void Write(byte[] b, int off, int len)
 			{
-				while (len > 0)
-				{
-					int numToCopy = System.Math.Min(len, _buf.Length - _off);
-
-					if (numToCopy == _buf.Length)
-					{
-						DerOctetString.Encode(_derOut, buf, offset, numToCopy);
-					}
-					else
-					{
-						Array.Copy(buf, offset, _buf, _off, numToCopy);
-
-						_off += numToCopy;
-						if (_off < _buf.Length)
-							break;
-
-						DerOctetString.Encode(_derOut, _buf, 0, _off);
-						_off = 0;
-					}
-
-					offset += numToCopy;
-					len -= numToCopy;
-				}
-			}
+                int bufLen = _buf.Length;
+                int available = bufLen - _off;
+                if (len < available)
+                {
+                    Array.Copy(b, off, _buf, _off, len);
+                    _off += len;
+                    return;
+                }
+
+                int count = 0;
+                if (_off > 0)
+                {
+                    Array.Copy(b, off, _buf, _off, available);
+                    count += available;
+                    DerOctetString.Encode(_derOut, _buf, 0, bufLen);
+                }
+
+                int remaining;
+                while ((remaining = len - count) >= bufLen)
+                {
+                    DerOctetString.Encode(_derOut, b, off + count, bufLen);
+                    count += bufLen;
+                }
+
+                Array.Copy(b, off + count, _buf, 0, remaining);
+                this._off = remaining;
+            }
 
 #if PORTABLE
             protected override void Dispose(bool disposing)