public override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount) { if ((bytes == null) || (chars == null) || (charCount < 0) || (byteCount < 0)) { EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes); } Contract.EndContractBlock(); int bytesWritten; if (charCount > 0) { if (byteCount == 0) { // Definitely not enough space, early bail EncodingForwarder.ThrowBytesOverflow(this); } int charactersConsumed; if (!EncodingForwarder.TryEncode(chars, charCount, bytes, byteCount, out charactersConsumed, out bytesWritten)) { // Not all ASCII, GetBytesFallback for remaining conversion bytesWritten += GetBytesFallback(chars + charactersConsumed, charCount - charactersConsumed, bytes + bytesWritten, byteCount - bytesWritten, null); } } else { // Nothing to encode bytesWritten = 0; } return(bytesWritten); }
// Encodes a range of characters in a character array into a range of bytes // in a byte array. An exception occurs if the byte array is not large // enough to hold the complete encoding of the characters. The // GetByteCount method can be used to determine the exact number of // bytes that will be produced for a given range of characters. // Alternatively, the GetMaxByteCount method can be used to // determine the maximum number of bytes that will be produced for a given // number of characters, regardless of the actual character values. public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { if ((chars == null) || (bytes == null) || (charIndex < 0) || (charCount < 0) || (chars.Length - charIndex < charCount) || (byteIndex < 0 || byteIndex > bytes.Length)) { EncodingForwarder.ThrowValidationFailedException(chars, charIndex, charCount, bytes); } Contract.EndContractBlock(); // Note that byteCount is the # of bytes to decode, not the size of the array int byteCount = bytes.Length - byteIndex; int bytesWritten; if (charCount > 0) { if (byteCount == 0) { // Definitely not enough space, early bail EncodingForwarder.ThrowBytesOverflow(this); } fixed(char *pInput = &chars[0]) fixed(byte *pOutput = &bytes[0]) { char *input = pInput + charIndex; byte *output = pOutput + byteIndex; int charactersConsumed; if (!EncodingForwarder.TryEncode(input, charCount, output, byteCount, out charactersConsumed, out bytesWritten)) { // Not all ASCII, GetBytesFallback for remaining conversion bytesWritten += GetBytesFallback(input + charactersConsumed, charCount - charactersConsumed, output + bytesWritten, byteCount - bytesWritten, null); } } } else { // Nothing to encode bytesWritten = 0; } return(bytesWritten); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { // Just need to Assert, this is called by internal EncoderNLS and parameters should already be checked Debug.Assert(this != null); Debug.Assert(bytes != null); Debug.Assert(chars != null); Debug.Assert(charCount >= 0); Debug.Assert(byteCount >= 0); int bytesWritten; int charactersConsumed = 0; if (((encoder?.InternalHasFallbackBuffer ?? false) && (encoder.FallbackBuffer.Remaining > 0)) || (charCount > byteCount)) { // Data already in Fallback buffer, so straight to GetBytesFallback bytesWritten = GetBytesFallback(chars, charCount, bytes, byteCount, encoder); } else if (charCount > 0) { if (byteCount == 0) { // Definitely not enough space, early bail EncodingForwarder.ThrowBytesOverflow(this); } if (!EncodingForwarder.TryEncode(chars, charCount, bytes, byteCount, out charactersConsumed, out bytesWritten)) { // Not all ASCII, use GetBytesFallback for remaining conversion bytesWritten += GetBytesFallback(chars + charactersConsumed, charCount - charactersConsumed, bytes + bytesWritten, byteCount - bytesWritten, encoder); } } else { // Nothing to encode bytesWritten = 0; } if (encoder != null) { encoder.m_charsUsed += charactersConsumed; } return(bytesWritten); }
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) { // Validate parameters if ((chars == null) || (bytes == null) || (charIndex < 0) || (charCount < 0) || (chars.Length - charIndex < charCount) || (byteIndex < 0 || byteIndex > bytes.Length)) { EncodingForwarder.ThrowValidationFailedException(chars, charIndex, charCount, bytes); } Contract.EndContractBlock(); int byteCount = bytes.Length - byteIndex; if (charCount > 0 && byteCount == 0) { // Definitely not enough space, early bail EncodingForwarder.ThrowBytesOverflow(m_encoding); } if (chars.Length == 0) { chars = new char[1]; } if (bytes.Length == 0) { bytes = new byte[1]; // Just call pointer version fixed(char *pChars = &chars[0]) fixed(byte *pBytes = &bytes[0]) { return(GetBytesValidated(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, flush)); } }