Skip to content

Commit

Permalink
added CappedMemoryStream.cs to all project files
Browse files Browse the repository at this point in the history
  • Loading branch information
dvsekhvalnov committed Dec 26, 2024
1 parent 8cf59d9 commit 1c530fb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
61 changes: 31 additions & 30 deletions jose-jwt/compression/CappedMemoryStream.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
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 CapacityExceededException("Exceeding maximum memory stream size.");
}

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

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

base.WriteByte(value);
}
namespace Jose

This comment has been minimized.

Copy link
@CrspyAu

CrspyAu Dec 26, 2024

Contributor

Ah yes, of course - haven't worked with the older frameworks in a while

{
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 CapacityExceededException("Exceeding maximum memory stream size.");
}

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

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

base.WriteByte(value);
}
}
}
1 change: 1 addition & 0 deletions jose-jwt/jose-jwt.net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="compression\CappedMemoryStream.cs" />
<Compile Include="compression\DeflateCompression.cs" />
<Compile Include="compression\ICompression.cs" />
<Compile Include="crypto\AesGcm.cs" />
Expand Down
1 change: 1 addition & 0 deletions jose-jwt/jose-jwt.net46.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="compression\CappedMemoryStream.cs" />
<Compile Include="compression\DeflateCompression.cs" />
<Compile Include="compression\ICompression.cs" />
<Compile Include="crypto\AesGcm.cs" />
Expand Down
1 change: 1 addition & 0 deletions jose-jwt/jose-jwt.net47.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="compression\CappedMemoryStream.cs" />
<Compile Include="compression\DeflateCompression.cs" />
<Compile Include="compression\ICompression.cs" />
<Compile Include="crypto\AesGcm.cs" />
Expand Down

0 comments on commit 1c530fb

Please sign in to comment.