From 4b9f92f40910434d9227d6cfae3bc003614c0ff0 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 20 May 2024 19:14:46 +0700 Subject: Add constructors allowing optional version header - see https://github.com/bcgit/bc-csharp/issues/533 --- crypto/src/bcpg/ArmoredOutputStream.cs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/crypto/src/bcpg/ArmoredOutputStream.cs b/crypto/src/bcpg/ArmoredOutputStream.cs index 03d4a1a91..0f4d410b3 100644 --- a/crypto/src/bcpg/ArmoredOutputStream.cs +++ b/crypto/src/bcpg/ArmoredOutputStream.cs @@ -6,7 +6,6 @@ using System.Reflection; using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Bcpg @@ -145,24 +144,35 @@ namespace Org.BouncyCastle.Bcpg private static readonly string Version = CreateVersion(); - private readonly IDictionary> m_headers; + private readonly Dictionary> m_headers; public ArmoredOutputStream(Stream outStream) + : this(outStream, true) + { + } + + public ArmoredOutputStream(Stream outStream, bool addVersionHeader) { this.outStream = outStream; - this.m_headers = new Dictionary>(1); - SetHeader(HeaderVersion, Version); + this.m_headers = new Dictionary>(); + + if (addVersionHeader) + { + SetHeader(HeaderVersion, Version); + } } public ArmoredOutputStream(Stream outStream, IDictionary headers) - : this(outStream) + : this(outStream, headers, true) + { + } + + public ArmoredOutputStream(Stream outStream, IDictionary headers, bool addVersionHeader) + : this(outStream, addVersionHeader && !headers.ContainsKey(HeaderVersion)) { foreach (var header in headers) { - var headerList = new List(1); - headerList.Add(header.Value); - - m_headers[header.Key] = headerList; + m_headers.Add(header.Key, new List { header.Value }); } } @@ -219,13 +229,13 @@ namespace Org.BouncyCastle.Bcpg */ public void ResetHeaders() { - var versions = CollectionUtilities.GetValueOrNull(m_headers, HeaderVersion); + bool hadVersion = m_headers.TryGetValue(HeaderVersion, out var version); m_headers.Clear(); - if (versions != null) + if (hadVersion) { - m_headers[HeaderVersion] = versions; + m_headers.Add(HeaderVersion, version); } } -- cgit 1.4.1