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, 26 insertions, 30 deletions
diff --git a/crypto/src/asn1/BEROctetStringGenerator.cs b/crypto/src/asn1/BEROctetStringGenerator.cs
index b07576e7d..5472802a2 100644
--- a/crypto/src/asn1/BEROctetStringGenerator.cs
+++ b/crypto/src/asn1/BEROctetStringGenerator.cs
@@ -8,38 +8,39 @@ namespace Org.BouncyCastle.Asn1
 	public class BerOctetStringGenerator
 		: BerGenerator
 	{
-		public BerOctetStringGenerator(Stream outStream)
-			: base(outStream)
-		{
-			WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
-		}
+        public BerOctetStringGenerator(Stream outStream)
+            : base(outStream)
+        {
+            WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
+        }
 
-		public BerOctetStringGenerator(
-			Stream	outStream,
-			int		tagNo,
-			bool	isExplicit)
-			: base(outStream, tagNo, isExplicit)
-		{
-			WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
-		}
+        public BerOctetStringGenerator(Stream outStream, int tagNo, bool isExplicit)
+            : base(outStream, tagNo, isExplicit)
+        {
+            WriteBerHeader(Asn1Tags.Constructed | Asn1Tags.OctetString);
+        }
 
+        /// <remarks>The caller is responsible for disposing the returned <see cref="Stream"/> before disposing
+        /// this generator.</remarks>
 		public Stream GetOctetOutputStream()
 		{
 			return GetOctetOutputStream(new byte[1000]); // limit for CER encoding.
 		}
 
-		public Stream GetOctetOutputStream(
-			int bufSize)
+        /// <remarks>The caller is responsible for disposing the returned <see cref="Stream"/> before disposing
+        /// this generator.</remarks>
+		public Stream GetOctetOutputStream(int bufSize)
 		{
-			return bufSize < 1
-				?	GetOctetOutputStream()
-				:	GetOctetOutputStream(new byte[bufSize]);
-		}
+            return bufSize < 1
+                ? GetOctetOutputStream()
+                : GetOctetOutputStream(new byte[bufSize]);
+        }
 
-		public Stream GetOctetOutputStream(
-			byte[] buf)
+        /// <remarks>The caller is responsible for disposing the returned <see cref="Stream"/> before disposing
+        /// this generator.</remarks>
+		public Stream GetOctetOutputStream(byte[] buf)
 		{
-			return new BufferedBerOctetStream(this, buf);
+			return new BufferedBerOctetStream(GetRawOutputStream(), buf);
 		}
 
 		private class BufferedBerOctetStream
@@ -47,17 +48,13 @@ namespace Org.BouncyCastle.Asn1
 		{
 			private byte[] _buf;
 			private int    _off;
-			private readonly BerOctetStringGenerator _gen;
 			private readonly Asn1OutputStream _derOut;
 
-			internal BufferedBerOctetStream(
-				BerOctetStringGenerator	gen,
-				byte[]					buf)
+			internal BufferedBerOctetStream(Stream outStream, byte[] buf)
 			{
-				_gen = gen;
 				_buf = buf;
 				_off = 0;
-				_derOut = Asn1OutputStream.Create(_gen.Out, Asn1Encodable.Der);
+				_derOut = Asn1OutputStream.Create(outStream, Asn1Encodable.Der);
 			}
 
 			public override void Write(byte[] buffer, int offset, int count)
@@ -145,11 +142,10 @@ namespace Org.BouncyCastle.Asn1
                     if (_off != 0)
                     {
                         DerOctetString.Encode(_derOut, _buf, 0, _off);
+                        _off = 0;
                     }
 
                     _derOut.FlushInternal();
-
-                    _gen.WriteBerEnd();
                 }
                 base.Dispose(disposing);
             }