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); }
public override unsafe int GetByteCount(char[] chars, int index, int count, bool flush) { // Validate input parameters if ((chars == null) || (index < 0) || (count < 0) || (chars.Length - index < count)) { EncodingForwarder.ThrowValidationFailedException(chars, index, count); } Contract.EndContractBlock(); // Avoid empty input problem if (chars.Length == 0) { chars = new char[1]; } // Just call the pointer version int result = -1; fixed(char *pChars = &chars[0]) { result = GetByteCountValidated(pChars + index, count, flush); } return(result); }
public override byte[] GetBytes(char[] chars, int index, int count) { if ((chars == null) || (index < 0) || (count < 0) || (chars.Length - index < count)) { EncodingForwarder.ThrowValidationFailedException(chars, index, count); } Contract.EndContractBlock(); return(GetBytesValidated(chars, index, count)); }
public unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, bool flush) { // Validate parameters if ((bytes == null) || (chars == null) || (charCount < 0) || (byteCount < 0)) { EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes); } Contract.EndContractBlock(); return(GetBytesValidated(chars, charCount, bytes, byteCount, flush)); }
// 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); }
// This is the version that uses pointers. We call the base encoding worker function // after setting our appropriate internal variables. This is getting bytes public override unsafe void Convert(char *chars, int charCount, byte *bytes, int byteCount, bool flush, out int charsUsed, out int bytesUsed, out bool completed) { // Validate input parameters if ((bytes == null) || (chars == null) || (charCount < 0) || (byteCount < 0)) { EncodingForwarder.ThrowValidationFailedException(chars, charCount, bytes); } Contract.EndContractBlock(); StartConversion(flush); // Do conversion bytesUsed = this.m_encoding.GetBytes(chars, charCount, bytes, byteCount, this); FinishConversion(charCount, flush, out charsUsed, out completed); }
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)); } }