From a18842b9d8b70958afadc1a9d48771a8ce52c4fb Mon Sep 17 00:00:00 2001 From: Sedat Kapanoglu Date: Sat, 17 Nov 2018 00:12:52 -0800 Subject: [PATCH] add stream encoding/decoding support to Base85 --- src/Base85.cs | 44 +++++++++++++++++++++++++++++++++++++- test/Base85/Ascii85Test.cs | 27 ++++++++++++++++++++++- test/Base85/Z85Test.cs | 2 +- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/Base85.cs b/src/Base85.cs index 69626b9..93edeb8 100644 --- a/src/Base85.cs +++ b/src/Base85.cs @@ -15,6 +15,7 @@ limitations under the License. */ using System; +using System.IO; using System.Runtime.CompilerServices; namespace SimpleBase @@ -122,9 +123,50 @@ private unsafe void writeOutput(ref char* pOutput, string table, long input, int pOutput += stringLength; } - public unsafe Span Decode(string text) + public void Encode(Stream input, TextWriter output) + { + Require.NotNull(input, nameof(input)); + Require.NotNull(output, nameof(output)); + const int bufferSize = 4096; + var buffer = new byte[bufferSize]; + while (true) + { + int bytesRead = input.Read(buffer, 0, bufferSize); + if (bytesRead < 1) + { + break; + } + var result = Encode(buffer.AsSpan(0, bytesRead)); + output.Write(result); + } + } + + public void Decode(TextReader input, Stream output) + { + Require.NotNull(input, nameof(input)); + Require.NotNull(output, nameof(output)); + const int bufferSize = 5120; + var buffer = new char[bufferSize]; + while (true) + { + int charsRead = input.Read(buffer, 0, bufferSize); + if (charsRead < 1) + { + break; + } + var result = Decode(buffer.AsSpan(0, charsRead)); + output.Write(result.ToArray(), 0, result.Length); + } + } + + public Span Decode(string text) { Require.NotNull(text, nameof(text)); + return Decode(text.AsSpan()); + } + + public unsafe Span Decode(ReadOnlySpan text) + { int textLen = text.Length; if (textLen == 0) { diff --git a/test/Base85/Ascii85Test.cs b/test/Base85/Ascii85Test.cs index 274293d..e05fd2f 100644 --- a/test/Base85/Ascii85Test.cs +++ b/test/Base85/Ascii85Test.cs @@ -1,6 +1,7 @@ using NUnit.Framework; using SimpleBase; using System; +using System.IO; namespace SimpleBaseTest.Base85Test { @@ -30,6 +31,18 @@ public void Decode_Whitespace_IsIgnored(byte[] expectedOutput, string input) CollectionAssert.AreEqual(expectedOutput, result.ToArray()); } + [Test] + [TestCaseSource(nameof(testVectors))] + public void Encode_TestVectorsOnStream_ShouldEncodeCorrectly(byte[] input, string expectedOutput) + { + using (var inputStream = new MemoryStream(input)) + using (var writer = new StringWriter()) + { + Base85.Ascii85.Encode(inputStream, writer); + Assert.AreEqual(expectedOutput, writer.ToString()); + } + } + [Test] [TestCaseSource(nameof(testVectors))] public void Encode_TestVectors_ShouldEncodeCorrectly(byte[] input, string expectedOutput) @@ -59,7 +72,19 @@ public void Decode_UnevenText_DoesNotThrowArgumentException() [Test] public void Decode_NullText_ThrowsArgumentNullException() { - Assert.Throws(() => Base85.Ascii85.Decode(null)); + Assert.Throws(() => Base85.Ascii85.Decode((string)null)); + } + + [Test] + [TestCaseSource(nameof(testVectors))] + public void Decode_TestVectorsWithStream_ShouldDecodeCorrectly(byte[] expectedOutput, string input) + { + using (var inputStream = new StringReader(input)) + using (var writer = new MemoryStream()) + { + Base85.Ascii85.Decode(inputStream, writer); + CollectionAssert.AreEqual(expectedOutput, writer.ToArray()); + } } [Test] diff --git a/test/Base85/Z85Test.cs b/test/Base85/Z85Test.cs index e22021d..6c3e7b7 100644 --- a/test/Base85/Z85Test.cs +++ b/test/Base85/Z85Test.cs @@ -36,7 +36,7 @@ public void Encode_NullBuffer_ReturnsEmptyString() [Test] public void Decode_NullText_ThrowsArgumentNullException() { - Assert.Throws(() => Base85.Z85.Decode(null)); + Assert.Throws(() => Base85.Z85.Decode((string)null)); } [Test]