Skip to content

Commit

Permalink
Add CappedMemoryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
CrspyAu committed Dec 12, 2024
1 parent 0982e2e commit 88d4a56
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions jose-jwt/compression/CappedMemoryStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.IO;

namespace Jose;

public class CappedMemoryStream : MemoryStream
{
private readonly long maxCapacity;

public CappedMemoryStream(long maxCapacity)
{
this.maxCapacity = maxCapacity;
}

public override void Write(byte[] buffer, int offset, int count)
{
if (Length + Math.Min(count, buffer.Length - offset) > maxCapacity)
{
throw new NotSupportedException("Exceeding maximum memory stream size.");
}

base.Write(buffer, offset, count);
}

public override void WriteByte(byte value)
{
if (Length + 1 > maxCapacity)
{
throw new NotSupportedException("Exceeding maximum memory stream size.");
}

base.WriteByte(value);
}
}

0 comments on commit 88d4a56

Please sign in to comment.