private readonly StringBuilder _readLineSB; // SB that holds readLine output internal StdInStreamReader(Stream stream, Encoding encoding, int bufferSize) : base(stream: stream, encoding: encoding, detectEncodingFromByteOrderMarks: false, bufferSize: bufferSize, leaveOpen: true) { _unprocessedBufferToBeRead = new char[encoding.GetMaxCharCount(BytesToBeRead)]; _startIndex = 0; _endIndex = 0; _readLineSB = new StringBuilder(); }
public EndianReader(Stream input, Endianness endianess, Encoding encoding, bool leaveOpen) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (!input.CanRead) throw new ArgumentException("Can't read from the output stream", nameof(input)); Contract.EndContractBlock(); BaseStream = input; decoder = encoding.GetDecoder(); maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize); var minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) minBufferSize = 16; buffer = new byte[minBufferSize]; // m_charBuffer and m_charBytes will be left null. // For Encodings that always use 2 bytes per char (or more), // special case them here to make Read() & Peek() faster. use2BytesPerChar = encoding is UnicodeEncoding; this.leaveOpen = leaveOpen; Endianness = endianess; resolvedEndianess = EndianessHelper.Resolve(endianess); Contract.Assert(decoder != null, "[EndianReader.ctor]m_decoder!=null"); }
private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen) { _stream = stream; _encoding = encoding; _decoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } _byteBuffer = new byte[bufferSize]; _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); _charBuffer = new char[_maxCharsPerBuffer]; _byteLen = 0; _bytePos = 0; _detectEncoding = detectEncodingFromByteOrderMarks; // Encoding.GetPreamble() always allocates and returns a new byte[] array for // encodings that have a preamble. // We can avoid repeated allocations for the default and commonly used Encoding.UTF8 // encoding by using our own private cached instance of the UTF8 preamble. // We specifically look for Encoding.UTF8 because we know it has a preamble, // whereas other instances of UTF8Encoding may not have a preamble enabled, and // there's no public way to tell if the preamble is enabled for an instance other // than calling GetPreamble(), which we're trying to avoid. // This means that other instances of UTF8Encoding are excluded from this optimization. _preamble = object.ReferenceEquals(encoding, Encoding.UTF8) ? (s_utf8Preamble ?? (s_utf8Preamble = encoding.GetPreamble())) : encoding.GetPreamble(); _checkPreamble = (_preamble.Length > 0); _isBlocked = false; _closable = !leaveOpen; }
public HttpRequestStreamReader(Stream stream, Encoding encoding, int bufferSize) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException(Resources.HttpRequestStreamReader_StreamNotReadable, nameof(stream)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } _stream = stream; _encoding = encoding; _decoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } _byteBufferSize = bufferSize; _byteBuffer = new byte[bufferSize]; var maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); _charBuffer = new char[maxCharsPerBuffer]; }
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) { if (input==null) { throw new ArgumentNullException(nameof(input)); } if (encoding==null) { throw new ArgumentNullException(nameof(encoding)); } if (!input.CanRead) throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable")); Contract.EndContractBlock(); m_stream = input; m_decoder = encoding.GetDecoder(); m_maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize); int minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) minBufferSize = 16; m_buffer = new byte[minBufferSize]; // m_charBuffer and m_charBytes will be left null. // For Encodings that always use 2 bytes per char (or more), // special case them here to make Read() & Peek() faster. m_2BytesPerChar = encoding is UnicodeEncoding; // check if BinaryReader is based on MemoryStream, and keep this for it's life // we cannot use "as" operator, since derived classes are not allowed m_isMemoryStream = (m_stream.GetType() == typeof(MemoryStream)); m_leaveOpen = leaveOpen; Contract.Assert(m_decoder!=null, "[BinaryReader.ctor]m_decoder!=null"); }
public BinaryReader(Stream input, Encoding encoding) { if (input == null) { throw new ArgumentNullException("input"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (!input.CanRead) { throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotReadable")); } this.m_stream = input; this.m_decoder = encoding.GetDecoder(); this.m_maxCharsSize = encoding.GetMaxCharCount(0x80); int maxByteCount = encoding.GetMaxByteCount(1); if (maxByteCount < 0x10) { maxByteCount = 0x10; } this.m_buffer = new byte[maxByteCount]; this.m_charBuffer = null; this.m_charBytes = null; this.m_2BytesPerChar = encoding is UnicodeEncoding; this.m_isMemoryStream = this.m_stream.GetType() == typeof(MemoryStream); }
private int _endIndex; // Index after last unprocessed index in the buffer; internal StdInReader(Encoding encoding, int bufferSize) { _encoding = encoding; _unprocessedBufferToBeRead = new char[encoding.GetMaxCharCount(BytesToBeRead)]; _startIndex = 0; _endIndex = 0; _readLineSB = new StringBuilder(); }
public CancellableStreamReader(Stream stream, Encoding encoding) { _stream = stream; _decoder = encoding.GetDecoder(); _byteBuffer = new byte[BufferLength]; _buffer = new char[encoding.GetMaxCharCount(BufferLength)]; _bufferedLength = -1; _bufferCursor = -1; }
internal static SourceText Decode(Stream stream, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, bool throwIfBinaryDetected) { stream.Seek(0, SeekOrigin.Begin); int length = (int)stream.Length; if (length == 0) { return SourceText.From(string.Empty, encoding, checksumAlgorithm); } var maxCharRemainingGuess = encoding.GetMaxCharCount(length); using (var reader = new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: Math.Min(length, 4096), leaveOpen: true)) { ArrayBuilder<char[]> chunks = ArrayBuilder<char[]>.GetInstance(1 + maxCharRemainingGuess / ChunkSize); while (!reader.EndOfStream) { var nextChunkSize = ChunkSize; if (maxCharRemainingGuess < ChunkSize) { // maxCharRemainingGuess typically overestimates a little // so we will first fill a slightly smaller (maxCharRemainingGuess - 64) chunk // and then use 64 char tail, which is likley to be resized. nextChunkSize = Math.Max(maxCharRemainingGuess - 64, 64); } char[] chunk = new char[nextChunkSize]; int charsRead = reader.ReadBlock(chunk, 0, chunk.Length); if (charsRead == 0) { break; } maxCharRemainingGuess -= charsRead; if (charsRead < chunk.Length) { Array.Resize(ref chunk, charsRead); } // Check for binary files if (throwIfBinaryDetected && IsBinary(chunk)) { throw new InvalidDataException(); } chunks.Add(chunk); } var checksum = CalculateChecksum(stream, checksumAlgorithm); return new LargeText(chunks.ToImmutableAndFree(), reader.CurrentEncoding, checksum, checksumAlgorithm); } }
public MyBinaryReader(Stream stream, Encoding encoding) : base(stream, encoding) { this.m_decoder = encoding.GetDecoder(); this.m_maxCharsSize = encoding.GetMaxCharCount(0x80); int maxByteCount = encoding.GetMaxByteCount(1); if (maxByteCount < 0x10) { maxByteCount = 0x10; } }
/// <summary>Initializes a new reusable reader.</summary> /// <param name="encoding">The Encoding to use. Defaults to UTF8.</param> /// <param name="bufferSize">The size of the buffer to use when reading from the stream.</param> public ReusableTextReader(Encoding encoding = null, int bufferSize = 1024) { if (encoding == null) { encoding = Encoding.UTF8; } _decoder = encoding.GetDecoder(); _bytes = new byte[bufferSize]; _chars = new char[encoding.GetMaxCharCount(_bytes.Length)]; }
void Initialize(Stream stream, System.Action <string> callback, Encoding encoding, int bufferSize) { if (bufferSize < MIN_BUFFSIZE) { bufferSize = MIN_BUFFSIZE; } this.stream = stream; this.callback = callback; this.encoding = encoding; this.bbuff = new byte[bufferSize]; this.cbuff = new char[encoding.GetMaxCharCount(bufferSize)]; this.decoder = this.encoding.GetDecoder(); this.mtxEof = new Thr::ManualResetEvent(false); }
static public int GetMaxCharCount(IntPtr l) { try { System.Text.Encoding self = (System.Text.Encoding)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetMaxCharCount(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
private void Init(Process process, Stream stream, UserCallBack callback, Encoding encoding, int bufferSize) { this.process = process; this.stream = stream; this.encoding = encoding; this.userCallBack = callback; decoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) bufferSize = MinBufferSize; byteBuffer = new byte[bufferSize]; _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); charBuffer = new char[_maxCharsPerBuffer]; cancelOperation = false; eofEvent = new ManualResetEvent(false); sb = null; this.bLastCarriageReturn = false; }
public MultipartFormDataReader(string contentType, Encoding contentEncoding, Stream inputStream, int bufferSize) { this.contentType = contentType; this.contentEncoding = contentEncoding; this.inputStream = inputStream; mode = ParsingMode.Headers; ParseContentType(); mem = new MemoryStream(bufferSize); text = new char[ contentEncoding.GetMaxCharCount(mem.Capacity) ]; }
public TcpBinaryReader(Stream input, Encoding encoding) : base(input, encoding) { m_stream = input; this.m_decoder = encoding.GetDecoder(); this.m_maxCharsSize = encoding.GetMaxCharCount(0x80); int maxByteCount = encoding.GetMaxByteCount(1); if (maxByteCount < 0x10) { maxByteCount = 0x10; } this.m_buffer = new byte[maxByteCount]; this.m_2BytesPerChar = encoding is UnicodeEncoding; this.m_isMemoryStream = this.m_stream.GetType() == typeof(MemoryStream); }
// Creates a new AsyncStreamReader for the given stream. The // character encoding is set by encoding and the buffer size, // in number of 16-bit characters, is set by bufferSize. internal AsyncStreamReader(Stream stream, Action<string> callback, Encoding encoding) { Debug.Assert(stream != null && encoding != null && callback != null, "Invalid arguments!"); Debug.Assert(stream.CanRead, "Stream must be readable!"); _stream = stream; _userCallBack = callback; _decoder = encoding.GetDecoder(); _byteBuffer = new byte[DefaultBufferSize]; // This is the maximum number of chars we can get from one iteration in loop inside ReadBuffer. // Used so ReadBuffer can tell when to copy data into a user's char[] directly, instead of our internal char[]. int maxCharsPerBuffer = encoding.GetMaxCharCount(DefaultBufferSize); _charBuffer = new char[maxCharsPerBuffer]; _cts = new CancellationTokenSource(); _messageQueue = new Queue<string>(); }
// Creates a new AsyncStreamReader for the given stream. The // character encoding is set by encoding and the buffer size, // in number of 16-bit characters, is set by bufferSize. // internal AsyncStreamReader(Process process, Stream stream, Action<string> callback, Encoding encoding, int bufferSize) { Debug.Assert(process != null && stream != null && encoding != null && callback != null, "Invalid arguments!"); Debug.Assert(stream.CanRead, "Stream must be readable!"); Debug.Assert(bufferSize > 0, "Invalid buffer size!"); _process = process; _stream = stream; _userCallBack = callback; _encoding = encoding; _decoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) bufferSize = MinBufferSize; _byteBuffer = new byte[bufferSize]; _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); _charBuffer = new char[_maxCharsPerBuffer]; _eofEvent = new ManualResetEvent(false); _messageQueue = new Queue<string>(); }
public BinaryReader(Stream input, Encoding encoding, bool leaveOpen) { if (input == null) { throw new ArgumentNullException("input"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } if (!input.CanRead) { throw new ArgumentException(SR.Argument_StreamNotReadable); } _stream = input; _decoder = encoding.GetDecoder(); _maxCharsSize = encoding.GetMaxCharCount(MaxCharBytesSize); int minBufferSize = encoding.GetMaxByteCount(1); // max bytes per one char if (minBufferSize < 16) { minBufferSize = 16; } _buffer = new byte[minBufferSize]; // _charBuffer and _charBytes will be left null. // For Encodings that always use 2 bytes per char (or more), // special case them here to make Read() & Peek() faster. _2BytesPerChar = encoding is UnicodeEncoding; // check if BinaryReader is based on MemoryStream, and keep this for it's life // we cannot use "as" operator, since derived classes are not allowed _isMemoryStream = (_stream.GetType() == typeof(MemoryStream)); _leaveOpen = leaveOpen; Debug.Assert(_decoder != null, "[BinaryReader.ctor]_decoder!=null"); }
private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen) { _stream = stream; _encoding = encoding; _decoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } _byteBuffer = new byte[bufferSize]; _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); _charBuffer = new char[_maxCharsPerBuffer]; _byteLen = 0; _bytePos = 0; _detectEncoding = detectEncodingFromByteOrderMarks; _preamble = encoding.GetPreamble(); _checkPreamble = (_preamble.Length > 0); _isBlocked = false; _closable = !leaveOpen; }
internal void Initialize (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) { if (null == stream) throw new ArgumentNullException ("stream"); if (null == encoding) throw new ArgumentNullException ("encoding"); if (!stream.CanRead) throw new ArgumentException ("Cannot read stream"); if (bufferSize <= 0) throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive"); if (bufferSize < MinimumBufferSize) bufferSize = MinimumBufferSize; // since GetChars() might add flushed character, it // should have additional char buffer for extra 1 // (probably 1 is ok, but might be insufficient. I'm not sure) var decoded_buffer_size = encoding.GetMaxCharCount (bufferSize) + 1; // // Instead of allocating a new default buffer use the // last one if there is any available // if (bufferSize <= DefaultBufferSize && input_buffer_recycle != null) { lock (input_buffer_recycle_lock) { if (input_buffer_recycle != null) { input_buffer = input_buffer_recycle; input_buffer_recycle = null; } if (decoded_buffer_recycle != null && decoded_buffer_size <= decoded_buffer_recycle.Length) { decoded_buffer = decoded_buffer_recycle; decoded_buffer_recycle = null; } } } if (input_buffer == null) input_buffer = new byte [bufferSize]; else Array.Clear (input_buffer, 0, bufferSize); if (decoded_buffer == null) decoded_buffer = new char [decoded_buffer_size]; else Array.Clear (decoded_buffer, 0, decoded_buffer_size); base_stream = stream; this.buffer_size = bufferSize; this.encoding = encoding; decoder = encoding.GetDecoder (); byte [] preamble = encoding.GetPreamble (); do_checks = detectEncodingFromByteOrderMarks ? 1 : 0; do_checks += (preamble.Length == 0) ? 0 : 2; decoded_count = 0; pos = 0; }
/// <summary> /// Try to create a <see cref="SourceText"/> from the given stream using the given encoding. /// </summary> /// <param name="data">The input stream containing the encoded text. The stream will not be closed.</param> /// <param name="encoding">The expected encoding of the stream. The actual encoding used may be different if byte order marks are detected.</param> /// <param name="checksumAlgorithm">The checksum algorithm to use.</param> /// <param name="throwIfBinaryDetected">Throw <see cref="InvalidDataException"/> if binary (non-text) data is detected.</param> /// <returns>The <see cref="SourceText"/> decoded from the stream.</returns> /// <exception cref="DecoderFallbackException">The decoder was unable to decode the stream with the given encoding.</exception> /// <remarks> /// internal for unit testing /// </remarks> internal static SourceText Decode(Stream data, Encoding encoding, SourceHashAlgorithm checksumAlgorithm, bool throwIfBinaryDetected = false) { Debug.Assert(data != null); Debug.Assert(encoding != null); data.Seek(0, SeekOrigin.Begin); // For small streams, see if we can read the byte buffer directly. if (encoding.GetMaxCharCount((int)data.Length) < LargeObjectHeapLimitInChars) { byte[] buffer = TryGetByteArrayFromStream(data); if (buffer != null) { return SourceText.From(buffer, (int)data.Length, encoding, checksumAlgorithm, throwIfBinaryDetected); } } return SourceText.From(data, encoding, checksumAlgorithm, throwIfBinaryDetected); }
internal void Initialize (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) { if (null == stream) throw new ArgumentNullException ("stream"); if (null == encoding) throw new ArgumentNullException ("encoding"); if (!stream.CanRead) throw new ArgumentException ("Cannot read stream"); if (bufferSize <= 0) throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive"); if (bufferSize < MinimumBufferSize) bufferSize = MinimumBufferSize; base_stream = stream; input_buffer = new byte [bufferSize]; this.buffer_size = bufferSize; this.encoding = encoding; decoder = encoding.GetDecoder (); byte [] preamble = encoding.GetPreamble (); do_checks = detectEncodingFromByteOrderMarks ? 1 : 0; do_checks += (preamble.Length == 0) ? 0 : 2; // since GetChars() might add flushed character, it // should have additional char buffer for extra 1 // (probably 1 is ok, but might be insufficient. I'm not sure) decoded_buffer = new char [encoding.GetMaxCharCount (bufferSize) + 1]; decoded_count = 0; pos = 0; }
public override int GetMaxCharCount(int byteCount) { return(defaultEncoding.GetMaxCharCount(byteCount)); }
internal void Initialize(Stream stream, Encoding encoding, int bufferSize) { if (null == stream) { throw new ArgumentNullException("stream"); } if (null == encoding) { throw new ArgumentNullException("encoding"); } if (!stream.CanRead) { throw new ArgumentException(Resource.ErrorMessage44); } if (bufferSize <= 0) { throw new ArgumentException(Resource.ErrorMessage43, "bufferSize"); } if (bufferSize < MinimumBufferSize) { bufferSize = MinimumBufferSize; } m_baseStream = stream; m_inputBuffer = new byte[bufferSize]; this.m_bufferSize = bufferSize; this.m_encoding = encoding; m_decoder = encoding.GetDecoder(); m_decodedBuffer = new char[encoding.GetMaxCharCount(bufferSize)]; m_decodedCount = 0; m_currentDecodePosition = 0; }
/// <summary> /// Reads a textual line from a stream without reading ahead and consuming more /// bytes than necessary to read the line. /// </summary> /// <remarks> /// <para> /// This method is intended to be use where a stream is used with both binary /// data and encoded text. /// </para> /// <para> /// Because decoding without buffering requires detecting end of line characters /// reliabily without buffering, this method only supports the UTF-8 and ASCII /// encodings. /// </para> /// <para> /// This method uses TLS buffers and is thread safe. /// </para> /// </remarks> /// <param name="stream"> /// The stream to read the textual line from. /// </param> /// <param name="encoding"> /// The encoding to use. /// Only <see cref="Encoding.UTF8"/> and <see cref="Encoding.ASCII"/> are supported. /// </param> /// <param name="maxBytes"> /// The maximum number of bytes to read before throwing an <see cref="OverflowException"/>. /// If maxbytes is encountered, the stream is left in an unstable state for reading /// text. /// </param> /// <returns></returns> public static string ReadLineUnbuffered(this Stream stream, Encoding encoding, int maxBytes) { var lastByte = -1; var byteCount = 0; if (encoding != Encoding.UTF8 && encoding != Encoding.ASCII) { throw new ArgumentException(encoding.ToString(), "encoding"); } var retval = stream.ReadDynamicBlock(delegate(byte x) { if (x == '\n' && lastByte == '\r') { return false; } lastByte = x; byteCount++; if (byteCount > maxBytes) { throw new OverflowException(); } return true; }); var maxCharCount = encoding.GetMaxCharCount(retval.Right); if (t_CharBuffer == null || (t_CharBuffer != null && maxCharCount > t_CharBuffer.Length)) { int newLength = t_CharBuffer == null ? 64 : t_CharBuffer.Length; while (newLength < maxCharCount) { newLength *= 2; } Array.Resize(ref t_CharBuffer, newLength); } var charCount = Encoding.UTF8.GetChars(retval.Left, 0, lastByte == '\r' ? retval.Right - 1 : retval.Right, t_CharBuffer, 0); return new string(t_CharBuffer, 0, charCount); }
private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) { this.mStream = stream; this.mEncoding = encoding; this.mDecoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) bufferSize = MinBufferSize; this.mByteBuffer = new byte[bufferSize]; this.mMaxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); this.mCharBuffer = new char[this.mMaxCharsPerBuffer]; this.mByteLen = 0; this.mBytePos = 0; this.mDetectEncoding = detectEncodingFromByteOrderMarks; this.mPreamble = encoding.GetPreamble(); this.mCheckPreamble = this.mPreamble.Length > 0; this.mIsBlocked = false; this.mClosable = true; }
private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize) { this.stream = stream; this.encoding = encoding; decoder = encoding.GetDecoder(); if (bufferSize < MinBufferSize) bufferSize = MinBufferSize; byteBuffer = new byte[bufferSize]; _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize); charBuffer = new char[_maxCharsPerBuffer]; byteLen = 0; bytePos = 0; _detectEncoding = detectEncodingFromByteOrderMarks; _preamble = encoding.GetPreamble(); _checkPreamble = (_preamble.Length > 0); _isBlocked = false; _closable = true; // ByDefault all streams are closable unless explicitly told otherwise }
public NonBlockingStreamReader(Stream stream, Encoding encoding) { int buffer_size = DefaultBufferSize; base_stream = stream; input_buffer = new byte [buffer_size]; this.buffer_size = buffer_size; this.encoding = encoding; decoder = encoding.GetDecoder (); decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)]; decoded_count = 0; pos = 0; }
internal void Initialize(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size) { if (null == stream) { throw new ArgumentNullException("stream"); } if (null == encoding) { throw new ArgumentNullException("encoding"); } if (!stream.CanRead) { throw new ArgumentException("Cannot read stream"); } if (buffer_size <= 0) { throw new ArgumentOutOfRangeException("buffer_size", "The minimum size of the buffer must be positive"); } if (buffer_size < MinimumBufferSize) { buffer_size = MinimumBufferSize; } base_stream = stream; input_buffer = new byte[buffer_size]; this.buffer_size = buffer_size; this.encoding = encoding; decoder = encoding.GetDecoder(); byte[] preamble = encoding.GetPreamble(); do_checks = detect_encoding_from_bytemarks ? 1 : 0; do_checks += (preamble.Length == 0) ? 0 : 2; decoded_buffer = new char[encoding.GetMaxCharCount(buffer_size)]; decoded_count = 0; pos = 0; }
/// <summary> /// Constructs a <see cref="SourceText"/> from stream content. /// </summary> /// <param name="stream">Stream. The stream must be seekable.</param> /// <param name="encoding"> /// Data encoding to use if the stream doesn't start with Byte Order Mark specifying the encoding. /// <see cref="Encoding.UTF8"/> if not specified. /// </param> /// <param name="checksumAlgorithm"> /// Hash algorithm to use to calculate checksum of the text that's saved to PDB. /// </param> /// <param name="throwIfBinaryDetected">If the decoded text contains at least two consecutive NUL /// characters, then an <see cref="InvalidDataException"/> is thrown.</param> /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentException"> /// <paramref name="stream"/> doesn't support reading or seeking. /// <paramref name="checksumAlgorithm"/> is not supported. /// </exception> /// <exception cref="DecoderFallbackException">If the given encoding is set to use a throwing decoder as a fallback</exception> /// <exception cref="InvalidDataException">Two consecutive NUL characters were detected in the decoded text and <paramref name="throwIfBinaryDetected"/> was true.</exception> /// <exception cref="IOException">An I/O error occurs.</exception> /// <remarks>Reads from the beginning of the stream. Leaves the stream open.</remarks> public static SourceText From(Stream stream, Encoding encoding = null, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1, bool throwIfBinaryDetected = false) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead || !stream.CanSeek) { throw new ArgumentException(CodeAnalysisResources.StreamMustSupportReadAndSeek, nameof(stream)); } ValidateChecksumAlgorithm(checksumAlgorithm); encoding = encoding ?? s_utf8EncodingWithNoBOM; // If the resulting string would end up on the large object heap, then use LargeEncodedText. if (encoding.GetMaxCharCount((int)stream.Length) >= LargeObjectHeapLimitInChars) { return LargeEncodedText.Decode(stream, encoding, checksumAlgorithm, throwIfBinaryDetected); } string text = Decode(stream, encoding, out encoding); if (throwIfBinaryDetected && IsBinary(text)) { throw new InvalidDataException(); } var checksum = CalculateChecksum(stream, checksumAlgorithm); return new StringText(text, encoding, checksum, checksumAlgorithm); }
/// <summary> /// バイト配列の中から終端が\0で表された文字列を取り出す。 /// </summary> /// <remarks> /// バイト配列の長さはInt32.MaxValueを超えていても良い。 /// </remarks> /// <param name="bytes">デコードする最初のバイトへのポインタ</param> /// <param name="enc">文字エンコーディング</param> /// <returns>文字列(\0は含まない)</returns> public static unsafe string GetString(byte* bytes, Encoding enc) { //バイト長のカウント int byteCount = 0; while (*bytes != Nul) //終端\0に到達するまでシーク { checked { byteCount++; } //文字列のバイト長がInt32.MaxValueを超えたならエラー bytes++; } bytes -= byteCount; //生成されうる最大文字数のバッファを確保 int maxCharCount = enc.GetMaxCharCount(byteCount); fixed (char* buff = new char[maxCharCount]) { //バイト配列を文字列にデコード int len = enc.GetChars(bytes, byteCount, buff, maxCharCount); return new string(buff, 0, len); } }
public HttpRequestStreamReader( Stream stream, Encoding encoding, int bufferSize, ArrayPool<byte> bytePool, ArrayPool<char> charPool) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException(Resources.HttpRequestStreamReader_StreamNotReadable, nameof(stream)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } if (bytePool == null) { throw new ArgumentNullException(nameof(bytePool)); } if (charPool == null) { throw new ArgumentNullException(nameof(charPool)); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException(nameof(bufferSize)); } _stream = stream; _encoding = encoding; _byteBufferSize = bufferSize; _bytePool = bytePool; _charPool = charPool; _decoder = encoding.GetDecoder(); _byteBuffer = _bytePool.Rent(bufferSize); try { var requiredLength = encoding.GetMaxCharCount(bufferSize); _charBuffer = _charPool.Rent(requiredLength); } catch { _bytePool.Return(_byteBuffer); _byteBuffer = null; if (_charBuffer != null) { _charPool.Return(_charBuffer); _charBuffer = null; } } }