public byte[] ToByteArray()
{
if (_type == ValueHandleType.Base64)
{
byte[] buffer = new byte[_length];
GetBase64(buffer, 0, _length);
return(buffer);
}
if (_type == ValueHandleType.UTF8 && (_length % 4) == 0)
{
try
{
int expectedLength = _length / 4 * 3;
if (_length > 0)
{
if (_bufferReader.Buffer[_offset + _length - 1] == '=')
{
expectedLength--;
if (_bufferReader.Buffer[_offset + _length - 2] == '=')
{
expectedLength--;
}
}
}
byte[] buffer = new byte[expectedLength];
int actualLength = Base64Encoding.GetBytes(_bufferReader.Buffer, _offset, _length, buffer, 0);
if (actualLength != buffer.Length)
{
byte[] newBuffer = new byte[actualLength];
Buffer.BlockCopy(buffer, 0, newBuffer, 0, actualLength);
buffer = newBuffer;
}
return(buffer);
}
catch (FormatException)
{
// Something unhappy with the characters, fall back to the hard way
}
}
try
{
return(Base64Encoding.GetBytes(XmlConverter.StripWhitespace(GetString())));
}
catch (FormatException exception)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(exception.Message, exception.InnerException));
}
}