Skip to content

Commit

Permalink
add stream encoding/decoding support to Base85
Browse files Browse the repository at this point in the history
  • Loading branch information
ssg committed Nov 17, 2018
1 parent ea8dd86 commit a18842b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
44 changes: 43 additions & 1 deletion src/Base85.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace SimpleBase
Expand Down Expand Up @@ -122,9 +123,50 @@ private unsafe void writeOutput(ref char* pOutput, string table, long input, int
pOutput += stringLength;
}

public unsafe Span<byte> 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<byte> Decode(string text)
{
Require.NotNull(text, nameof(text));
return Decode(text.AsSpan());
}

public unsafe Span<byte> Decode(ReadOnlySpan<char> text)
{
int textLen = text.Length;
if (textLen == 0)
{
Expand Down
27 changes: 26 additions & 1 deletion test/Base85/Ascii85Test.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using SimpleBase;
using System;
using System.IO;

namespace SimpleBaseTest.Base85Test
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -59,7 +72,19 @@ public void Decode_UnevenText_DoesNotThrowArgumentException()
[Test]
public void Decode_NullText_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => Base85.Ascii85.Decode(null));
Assert.Throws<ArgumentNullException>(() => 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]
Expand Down
2 changes: 1 addition & 1 deletion test/Base85/Z85Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Encode_NullBuffer_ReturnsEmptyString()
[Test]
public void Decode_NullText_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => Base85.Z85.Decode(null));
Assert.Throws<ArgumentNullException>(() => Base85.Z85.Decode((string)null));
}

[Test]
Expand Down

0 comments on commit a18842b

Please sign in to comment.