private unsafe int InternalReadChars(char[] buffer, int index, int count) { int num = 0; int charCount = count; if (this.m_charBytes == null) { this.m_charBytes = new byte[0x80]; } while (charCount > 0) { int num3 = 0; num = charCount; UTF8Encoding.UTF8Decoder decoder = this.m_decoder as UTF8Encoding.UTF8Decoder; if (((decoder != null) && decoder.HasState) && (num > 1)) { num--; } if (this.m_2BytesPerChar) { num = num << 1; } if (num > 0x80) { num = 0x80; } int position = 0; byte[] charBytes = null; if (this.m_isMemoryStream) { MemoryStream stream = this.m_stream as MemoryStream; position = stream.InternalGetPosition(); num = stream.InternalEmulateRead(num); charBytes = stream.InternalGetBuffer(); } else { num = this.m_stream.Read(this.m_charBytes, 0, num); charBytes = this.m_charBytes; } if (num == 0) { return(count - charCount); } fixed(byte *numRef = charBytes) { fixed(char *chRef = buffer) { num3 = this.m_decoder.GetChars(numRef + position, num, chRef + index, charCount, false); } } charCount -= num3; index += num3; } return(count - charCount); }
private int InternalReadChars(char[] buffer, int index, int count) { int charsRead = 0; int numBytes = 0; int charsRemaining = count; if (m_charBytes == null) { m_charBytes = new byte[MaxCharBytesSize]; } while (charsRemaining > 0) { // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. numBytes = charsRemaining; if (m_2BytesPerChar) { numBytes <<= 1; } if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } if (m_isMemoryStream) { MemoryStream mStream = m_stream as MemoryStream; BCLDebug.Assert(mStream != null, "m_stream as MemoryStream != null"); int position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); if (numBytes == 0) { return(count - charsRemaining); } charsRead = m_decoder.GetChars(mStream.InternalGetBuffer(), position, numBytes, buffer, index); } else { numBytes = m_stream.Read(m_charBytes, 0, numBytes); if (numBytes == 0) { // Console.WriteLine("Found no bytes. We're outta here."); return(count - charsRemaining); } charsRead = m_decoder.GetChars(m_charBytes, 0, numBytes, buffer, index); } charsRemaining -= charsRead; index += charsRead; // Console.WriteLine("That became: " + charsRead + " characters."); } BCLDebug.Assert(charsRemaining == 0, "We didn't read all the chars we thought we would."); return(count); }
private unsafe int InternalReadChars(char[] buffer, int index, int count) { int charCount = count; if (this.m_charBytes == null) { this.m_charBytes = new byte[128]; } while (charCount > 0) { int count1 = charCount; DecoderNLS decoderNls = this.m_decoder as DecoderNLS; if (decoderNls != null && decoderNls.HasState && count1 > 1) { --count1; } if (this.m_2BytesPerChar) { count1 <<= 1; } if (count1 > 128) { count1 = 128; } int num = 0; int byteCount; byte[] numArray; if (this.m_isMemoryStream) { MemoryStream memoryStream = this.m_stream as MemoryStream; num = memoryStream.InternalGetPosition(); int count2 = count1; byteCount = memoryStream.InternalEmulateRead(count2); numArray = memoryStream.InternalGetBuffer(); } else { byteCount = this.m_stream.Read(this.m_charBytes, 0, count1); numArray = this.m_charBytes; } if (byteCount == 0) { return(count - charCount); } int chars; fixed(byte *numPtr = numArray) fixed(char *chPtr = buffer) chars = this.m_decoder.GetChars(numPtr + num, byteCount, chPtr + index, charCount, false); charCount -= chars; index += chars; } return(count - charCount); }
private int InternalReadChars(Span <char> buffer) { Debug.Assert(!_disposed); int numBytes = 0; int index = 0; int charsRemaining = buffer.Length; if (_charBytes == null) { _charBytes = new byte[MaxCharBytesSize]; } while (charsRemaining > 0) { int charsRead = 0; // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. numBytes = charsRemaining; if (_2BytesPerChar) { numBytes <<= 1; } // We do not want to read even a single byte more than necessary. // // Subtract pending bytes that the decoder may be holding onto. This assumes that each // decoded char corresponds to one or more bytes. Note that custom encodings or encodings with // a custom replacement sequence may violate this assumption. if (numBytes > 1) { DecoderNLS?decoder = _decoder as DecoderNLS; // For internal decoders, we can check whether the decoder has any pending state. // For custom decoders, assume that the decoder has pending state. if (decoder == null || decoder.HasState) { numBytes -= 1; // The worst case is charsRemaining = 2 and UTF32Decoder holding onto 3 pending bytes. We need to read just // one byte in this case. if (_2BytesPerChar && numBytes > 2) { numBytes -= 2; } } } if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } int position = 0; byte[]? byteBuffer = null; if (_isMemoryStream) { Debug.Assert(_stream is MemoryStream); MemoryStream mStream = (MemoryStream)_stream; position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); byteBuffer = mStream.InternalGetBuffer(); } else { numBytes = _stream.Read(_charBytes, 0, numBytes); byteBuffer = _charBytes; } if (numBytes == 0) { return(buffer.Length - charsRemaining); } Debug.Assert(byteBuffer != null, "expected byteBuffer to be non-null"); checked { if (position < 0 || numBytes < 0 || position > byteBuffer.Length - numBytes) { throw new ArgumentOutOfRangeException(nameof(numBytes)); } if (index < 0 || charsRemaining < 0 || index > buffer.Length - charsRemaining) { throw new ArgumentOutOfRangeException(nameof(charsRemaining)); } unsafe { fixed(byte *pBytes = byteBuffer) fixed(char *pChars = &MemoryMarshal.GetReference(buffer)) { charsRead = _decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, flush: false); } } } charsRemaining -= charsRead; index += charsRead; } // this should never fail Debug.Assert(charsRemaining >= 0, "We read too many characters."); // we may have read fewer than the number of characters requested if end of stream reached // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) return(buffer.Length - charsRemaining); }
private int InternalReadChars(Span <char> buffer) { Debug.Assert(!_disposed); int totalCharsRead = 0; while (!buffer.IsEmpty) { int numBytes = buffer.Length; // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. if (_2BytesPerChar) { numBytes <<= 1; } // We do not want to read even a single byte more than necessary. // // Subtract pending bytes that the decoder may be holding onto. This assumes that each // decoded char corresponds to one or more bytes. Note that custom encodings or encodings with // a custom replacement sequence may violate this assumption. if (numBytes > 1) { DecoderNLS?decoder = _decoder as DecoderNLS; // For internal decoders, we can check whether the decoder has any pending state. // For custom decoders, assume that the decoder has pending state. if (decoder == null || decoder.HasState) { numBytes--; // The worst case is charsRemaining = 2 and UTF32Decoder holding onto 3 pending bytes. We need to read just // one byte in this case. if (_2BytesPerChar && numBytes > 2) { numBytes -= 2; } } } ReadOnlySpan <byte> byteBuffer; if (_isMemoryStream) { Debug.Assert(_stream is MemoryStream); MemoryStream mStream = (MemoryStream)_stream; int position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); byteBuffer = new ReadOnlySpan <byte>(mStream.InternalGetBuffer(), position, numBytes); } else { _charBytes ??= new byte[MaxCharBytesSize]; if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } numBytes = _stream.Read(_charBytes, 0, numBytes); byteBuffer = new ReadOnlySpan <byte>(_charBytes, 0, numBytes); } if (byteBuffer.IsEmpty) { break; } int charsRead = _decoder.GetChars(byteBuffer, buffer, flush: false); buffer = buffer.Slice(charsRead); totalCharsRead += charsRead; } // we may have read fewer than the number of characters requested if end of stream reached // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) return(totalCharsRead); }
private int InternalReadChars(char[] buffer, int index, int count) { Contract.Requires(buffer != null); Contract.Requires(index >= 0 && count >= 0); Contract.Assert(m_stream != null); int numBytes = 0; int charsRemaining = count; if (m_charBytes == null) { m_charBytes = new byte[MaxCharBytesSize]; } while (charsRemaining > 0) { int charsRead = 0; // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. numBytes = charsRemaining; // special case for DecoderNLS subclasses when there is a hanging byte from the previous loop DecoderNLS decoder = m_decoder as DecoderNLS; if (decoder != null && decoder.HasState && numBytes > 1) { numBytes -= 1; } if (m_2BytesPerChar) { numBytes <<= 1; } if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } int position = 0; byte[] byteBuffer = null; if (m_isMemoryStream) { MemoryStream mStream = m_stream as MemoryStream; Contract.Assert(mStream != null, "m_stream as MemoryStream != null"); position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); byteBuffer = mStream.InternalGetBuffer(); } else { numBytes = m_stream.Read(m_charBytes, 0, numBytes); byteBuffer = m_charBytes; } if (numBytes == 0) { return(count - charsRemaining); } Contract.Assert(byteBuffer != null, "expected byteBuffer to be non-null"); checked { if (position < 0 || numBytes < 0 || position + numBytes > byteBuffer.Length) { throw new ArgumentOutOfRangeException("byteCount"); } if (index < 0 || charsRemaining < 0 || index + charsRemaining > buffer.Length) { throw new ArgumentOutOfRangeException("charsRemaining"); } unsafe { fixed(byte *pBytes = byteBuffer) { fixed(char *pChars = buffer) { charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false); } } } } charsRemaining -= charsRead; index += charsRead; } // this should never fail Contract.Assert(charsRemaining >= 0, "We read too many characters."); // we may have read fewer than the number of characters requested if end of stream reached // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) return(count - charsRemaining); }
private int InternalReadChars(Span <char> buffer) { Debug.Assert(!_disposed); int numBytes = 0; int index = 0; int charsRemaining = buffer.Length; if (_charBytes == null) { _charBytes = new byte[MaxCharBytesSize]; } while (charsRemaining > 0) { int charsRead = 0; // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. numBytes = charsRemaining; if (_2BytesPerChar) { numBytes <<= 1; } if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } int position = 0; byte[]? byteBuffer = null; if (_isMemoryStream) { Debug.Assert(_stream is MemoryStream); MemoryStream mStream = (MemoryStream)_stream; position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); byteBuffer = mStream.InternalGetBuffer(); } else { numBytes = _stream.Read(_charBytes, 0, numBytes); byteBuffer = _charBytes; } if (numBytes == 0) { return(buffer.Length - charsRemaining); } Debug.Assert(byteBuffer != null, "expected byteBuffer to be non-null"); checked { if (position < 0 || numBytes < 0 || position > byteBuffer.Length - numBytes) { throw new ArgumentOutOfRangeException(nameof(numBytes)); } if (index < 0 || charsRemaining < 0 || index > buffer.Length - charsRemaining) { throw new ArgumentOutOfRangeException(nameof(charsRemaining)); } unsafe { fixed(byte *pBytes = byteBuffer) fixed(char *pChars = &MemoryMarshal.GetReference(buffer)) { charsRead = _decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, flush: false); } } } charsRemaining -= charsRead; index += charsRead; } // this should never fail Debug.Assert(charsRemaining >= 0, "We read too many characters."); // we may have read fewer than the number of characters requested if end of stream reached // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) return(buffer.Length - charsRemaining); }
private int InternalReadChars(char[] buffer, int index, int count) { Debug.Assert(buffer != null); Debug.Assert(index >= 0 && count >= 0); Debug.Assert(_stream != null); int numBytes = 0; int charsRemaining = count; if (_charBytes == null) { _charBytes = new byte[MaxCharBytesSize]; } while (charsRemaining > 0) { int charsRead = 0; // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. numBytes = charsRemaining; if (_2BytesPerChar) { numBytes <<= 1; } if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } int position = 0; byte[] byteBuffer = null; if (_isMemoryStream) { MemoryStream mStream = _stream as MemoryStream; Debug.Assert(mStream != null, "_stream as MemoryStream != null"); position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); byteBuffer = mStream.InternalGetBuffer(); } else { numBytes = _stream.Read(_charBytes, 0, numBytes); byteBuffer = _charBytes; } if (numBytes == 0) { return(count - charsRemaining); } Debug.Assert(byteBuffer != null, "expected byteBuffer to be non-null"); unsafe { fixed(byte *pBytes = byteBuffer) fixed(char *pChars = buffer) { charsRead = _decoder.GetChars(byteBuffer, position, numBytes, buffer, index, false); } } charsRemaining -= charsRead; index += charsRead; } // this should never fail Debug.Assert(charsRemaining >= 0, "We read too many characters."); // we may have read fewer than the number of characters requested if end of stream reached // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) return(count - charsRemaining); }
private int InternalReadChars(char[] buffer, int index, int count) { int numBytes = 0; int charsRemaining = count; if (m_charBytes == null) { m_charBytes = new byte[MaxCharBytesSize]; } while (charsRemaining > 0) { int charsRead = 0; // We really want to know what the minimum number of bytes per char // is for our encoding. Otherwise for UnicodeEncoding we'd have to // do ~1+log(n) reads to read n characters. numBytes = charsRemaining; // special case for DecoderNLS subclasses when there is a hanging byte from the previous loop DecoderNLS decoder = m_decoder as DecoderNLS; if (decoder != null && decoder.HasState && numBytes > 1) { numBytes -= 1; } if (m_2BytesPerChar) { numBytes <<= 1; } if (numBytes > MaxCharBytesSize) { numBytes = MaxCharBytesSize; } int position = 0; byte[] byteBuffer = null; if (m_isMemoryStream) { MemoryStream mStream = m_stream as MemoryStream; position = mStream.InternalGetPosition(); numBytes = mStream.InternalEmulateRead(numBytes); byteBuffer = mStream.InternalGetBuffer(); } else { numBytes = m_stream.Read(m_charBytes, 0, numBytes); byteBuffer = m_charBytes; } if (numBytes == 0) { return(count - charsRemaining); } unsafe { fixed(byte *pBytes = byteBuffer) fixed(char *pChars = buffer) { charsRead = m_decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, false); } } charsRemaining -= charsRead; index += charsRead; } // we may have read fewer than the number of characters requested if end of stream reached // or if the encoding makes the char count too big for the buffer (e.g. fallback sequence) return(count - charsRemaining); }
private unsafe int InternalReadChars(char[] buffer, int index, int count) { int i = count; if (this.m_charBytes == null) { this.m_charBytes = new byte[128]; } while (i > 0) { int num = i; DecoderNLS decoderNLS = this.m_decoder as DecoderNLS; if (decoderNLS != null && decoderNLS.HasState && num > 1) { num--; } if (this.m_2BytesPerChar) { num <<= 1; } if (num > 128) { num = 128; } int num2 = 0; byte[] array; if (this.m_isMemoryStream) { MemoryStream memoryStream = this.m_stream as MemoryStream; num2 = memoryStream.InternalGetPosition(); num = memoryStream.InternalEmulateRead(num); array = memoryStream.InternalGetBuffer(); } else { num = this.m_stream.Read(this.m_charBytes, 0, num); array = this.m_charBytes; } if (num == 0) { return(count - i); } int chars; checked { if (num2 < 0 || num < 0 || num2 + num > array.Length) { throw new ArgumentOutOfRangeException("byteCount"); } if (index < 0 || i < 0 || index + i > buffer.Length) { throw new ArgumentOutOfRangeException("charsRemaining"); } fixed(byte *ptr = array) { fixed(char *ptr2 = buffer) { chars = this.m_decoder.GetChars(ptr + num2, num, ptr2 + index, i, false); } } } i -= chars; index += chars; } return(count - i); }