Change Base64 to Base64Encoder (#492)
This commit is contained in:
parent
b862d35b10
commit
762d52133c
|
|
@ -13,22 +13,11 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
|||
if (inputPayload.Length > 0)
|
||||
{
|
||||
// Determine the output size
|
||||
// Every 4 Base64 characters represents 3 bytes
|
||||
var decodedLength = (inputPayload.Length / 4) * 3;
|
||||
|
||||
// Subtract padding bytes
|
||||
if (inputPayload[inputPayload.Length - 1] == '=')
|
||||
{
|
||||
decodedLength -= 1;
|
||||
}
|
||||
if (inputPayload.Length > 1 && inputPayload[inputPayload.Length - 2] == '=')
|
||||
{
|
||||
decodedLength -= 1;
|
||||
}
|
||||
var decodedLength = Base64Encoder.ComputeDecodedLength(inputPayload);
|
||||
|
||||
// Allocate a new buffer to decode to
|
||||
var decodeBuffer = new byte[decodedLength];
|
||||
if (Base64.Decode(inputPayload, decodeBuffer) != decodedLength)
|
||||
if (!Base64Encoder.TryDecode(inputPayload, decodeBuffer, out _, out var _))
|
||||
{
|
||||
throw new FormatException("Invalid Base64 payload");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,8 +72,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
|||
if (type == MessageType.Binary)
|
||||
{
|
||||
// TODO: Base64 writer that works with IOutput would be amazing!
|
||||
var arr = new byte[Base64.ComputeEncodedLength(payload.Length)];
|
||||
Base64.Encode(payload, arr);
|
||||
var arr = new byte[Base64Encoder.ComputeEncodedLength(payload.Length)];
|
||||
Base64Encoder.TryEncode(payload, arr, out _, out _);
|
||||
return TryWriteLine(arr, output);
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
|||
var length = message.Payload.Length;
|
||||
if (message.Type == MessageType.Binary)
|
||||
{
|
||||
length = Base64.ComputeEncodedLength(length);
|
||||
length = Base64Encoder.ComputeEncodedLength(length);
|
||||
}
|
||||
|
||||
// Get the type indicator
|
||||
|
|
@ -61,8 +61,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
|||
if (message.Type == MessageType.Binary)
|
||||
{
|
||||
// TODO: Base64 writer that works with IOutput would be amazing!
|
||||
var arr = new byte[Base64.ComputeEncodedLength(message.Payload.Length)];
|
||||
Base64.Encode(message.Payload, arr);
|
||||
var arr = new byte[Base64Encoder.ComputeEncodedLength(message.Payload.Length)];
|
||||
Base64Encoder.TryEncode(message.Payload, arr, out _, out _);
|
||||
return output.TryWrite(arr);
|
||||
}
|
||||
else
|
||||
|
|
|
|||
Loading…
Reference in New Issue