summary refs log tree commit diff
path: root/crypto/src/bcpg/ArmoredOutputStream.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/bcpg/ArmoredOutputStream.cs')
-rw-r--r--crypto/src/bcpg/ArmoredOutputStream.cs75
1 files changed, 32 insertions, 43 deletions
diff --git a/crypto/src/bcpg/ArmoredOutputStream.cs b/crypto/src/bcpg/ArmoredOutputStream.cs
index 919325a73..a3d86429c 100644
--- a/crypto/src/bcpg/ArmoredOutputStream.cs
+++ b/crypto/src/bcpg/ArmoredOutputStream.cs
@@ -1,15 +1,9 @@
-using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
-using System.Reflection;
-using System.Text;
-
-#if PORTABLE
-using System.Linq;
-#endif
 
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Collections;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Bcpg
@@ -105,24 +99,24 @@ namespace Org.BouncyCastle.Bcpg
 
         private static readonly string Version = "BCPG C# v" + typeof(ArmoredOutputStream).Assembly.GetName().Version;
 
-        private readonly IDictionary headers;
+        private readonly IDictionary<string, IList<string>> m_headers;
 
         public ArmoredOutputStream(Stream outStream)
         {
             this.outStream = outStream;
-            this.headers = Platform.CreateHashtable(1);
+            this.m_headers = new Dictionary<string, IList<string>>(1);
             SetHeader(HeaderVersion, Version);
         }
 
-        public ArmoredOutputStream(Stream outStream, IDictionary headers)
+        public ArmoredOutputStream(Stream outStream, IDictionary<string, string> headers)
             : this(outStream)
         {
-            foreach (string header in headers.Keys)
+            foreach (var header in headers)
             {
-                IList headerList = Platform.CreateArrayList(1);
-                headerList.Add(headers[header]);
+                var headerList = new List<string>(1);
+                headerList.Add(header.Value);
 
-                this.headers[header] = headerList;
+                m_headers[header.Key] = headerList;
             }
         }
 
@@ -136,22 +130,21 @@ namespace Org.BouncyCastle.Bcpg
         {
             if (val == null)
             {
-                this.headers.Remove(name);
+                this.m_headers.Remove(name);
+                return;
+            }
+
+            if (m_headers.TryGetValue(name, out var valueList))
+            {
+                valueList.Clear();
             }
             else
             {
-                IList valueList = (IList)headers[name];
-                if (valueList == null)
-                {
-                    valueList = Platform.CreateArrayList(1);
-                    this.headers[name] = valueList;
-                }
-                else
-                {
-                    valueList.Clear();
-                }
-                valueList.Add(val);
+                valueList = new List<string>(1);
+                m_headers[name] = valueList;
             }
+
+            valueList.Add(val);
         }
 
         /**
@@ -166,12 +159,12 @@ namespace Org.BouncyCastle.Bcpg
             if (val == null || name == null)
                 return;
 
-            IList valueList = (IList)headers[name];
-            if (valueList == null)
+            if (!m_headers.TryGetValue(name, out var valueList))
             {
-                valueList = Platform.CreateArrayList(1);
-                this.headers[name] = valueList;
+                valueList = new List<string>(1);
+                m_headers[name] = valueList;
             }
+
             valueList.Add(val);
         }
 
@@ -180,13 +173,13 @@ namespace Org.BouncyCastle.Bcpg
          */
         public void ResetHeaders()
         {
-            IList versions = (IList)headers[HeaderVersion];
+            var versions = CollectionUtilities.GetValueOrNull(m_headers, HeaderVersion);
 
-            headers.Clear();
+            m_headers.Clear();
 
             if (versions != null)
             {
-                headers[HeaderVersion] = versions;
+                m_headers[HeaderVersion] = versions;
             }
         }
 
@@ -297,21 +290,17 @@ namespace Org.BouncyCastle.Bcpg
 
                 DoWrite(headerStart + type + headerTail + nl);
 
+                if (m_headers.TryGetValue(HeaderVersion, out var versionHeaders))
                 {
-                    IList versionHeaders = (IList)headers[HeaderVersion];
-                    if (versionHeaders != null)
-                    {
-                        WriteHeaderEntry(HeaderVersion, versionHeaders[0].ToString());
-                    }
+                    WriteHeaderEntry(HeaderVersion, versionHeaders[0]);
                 }
 
-                foreach (DictionaryEntry de in headers)
+                foreach (var de in m_headers)
                 {
-                    string k = (string)de.Key;
+                    string k = de.Key;
                     if (k != HeaderVersion)
                     {
-                        IList values = (IList)de.Value;
-                        foreach (string v in values)
+                        foreach (string v in de.Value)
                         {
                             WriteHeaderEntry(k, v);
                         }