Skip to content

Commit

Permalink
Update Base64StringAttribute.cs (#86221)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed May 14, 2023
1 parent f107b63 commit 9dd59af
Showing 1 changed file with 2 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Buffers.Text;

namespace System.ComponentModel.DataAnnotations
{
Expand Down Expand Up @@ -45,20 +45,7 @@ public override bool IsValid(object? value)
return false;
}

byte[]? rentedBuffer = null;
Span<byte> destinationBuffer = valueAsString.Length < 256
? stackalloc byte[256]
: rentedBuffer = ArrayPool<byte>.Shared.Rent(valueAsString.Length);

bool result = Convert.TryFromBase64String(valueAsString, destinationBuffer, out int bytesWritten);

if (rentedBuffer != null)
{
destinationBuffer.Slice(0, bytesWritten).Clear();
ArrayPool<byte>.Shared.Return(rentedBuffer);
}

return result;
return Base64.IsValid(valueAsString);
}
}
}

0 comments on commit 9dd59af

Please sign in to comment.