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)
|
if (inputPayload.Length > 0)
|
||||||
{
|
{
|
||||||
// Determine the output size
|
// Determine the output size
|
||||||
// Every 4 Base64 characters represents 3 bytes
|
var decodedLength = Base64Encoder.ComputeDecodedLength(inputPayload);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allocate a new buffer to decode to
|
// Allocate a new buffer to decode to
|
||||||
var decodeBuffer = new byte[decodedLength];
|
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");
|
throw new FormatException("Invalid Base64 payload");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,8 +72,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
||||||
if (type == MessageType.Binary)
|
if (type == MessageType.Binary)
|
||||||
{
|
{
|
||||||
// TODO: Base64 writer that works with IOutput would be amazing!
|
// TODO: Base64 writer that works with IOutput would be amazing!
|
||||||
var arr = new byte[Base64.ComputeEncodedLength(payload.Length)];
|
var arr = new byte[Base64Encoder.ComputeEncodedLength(payload.Length)];
|
||||||
Base64.Encode(payload, arr);
|
Base64Encoder.TryEncode(payload, arr, out _, out _);
|
||||||
return TryWriteLine(arr, output);
|
return TryWriteLine(arr, output);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
||||||
var length = message.Payload.Length;
|
var length = message.Payload.Length;
|
||||||
if (message.Type == MessageType.Binary)
|
if (message.Type == MessageType.Binary)
|
||||||
{
|
{
|
||||||
length = Base64.ComputeEncodedLength(length);
|
length = Base64Encoder.ComputeEncodedLength(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the type indicator
|
// Get the type indicator
|
||||||
|
|
@ -61,8 +61,8 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Formatters
|
||||||
if (message.Type == MessageType.Binary)
|
if (message.Type == MessageType.Binary)
|
||||||
{
|
{
|
||||||
// TODO: Base64 writer that works with IOutput would be amazing!
|
// TODO: Base64 writer that works with IOutput would be amazing!
|
||||||
var arr = new byte[Base64.ComputeEncodedLength(message.Payload.Length)];
|
var arr = new byte[Base64Encoder.ComputeEncodedLength(message.Payload.Length)];
|
||||||
Base64.Encode(message.Payload, arr);
|
Base64Encoder.TryEncode(message.Payload, arr, out _, out _);
|
||||||
return output.TryWrite(arr);
|
return output.TryWrite(arr);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue