示例#1
0
        internal unsafe void AppendExtraBuffer(byte *buffer, int bufferLength)
        {
            // Then convert the bytes to chars
            int   charLen = CurrentEncoding.GetMaxCharCount(bufferLength);
            char *charPtr = stackalloc char[charLen];

            charLen = CurrentEncoding.GetChars(buffer, bufferLength, charPtr, charLen);

            // Ensure our buffer is large enough to hold all of the data
            if (IsUnprocessedBufferEmpty())
            {
                _startIndex = _endIndex = 0;
            }
            else
            {
                Debug.Assert(_endIndex > 0);
                int spaceRemaining = _unprocessedBufferToBeRead.Length - _endIndex;
                if (spaceRemaining < charLen)
                {
                    Array.Resize(ref _unprocessedBufferToBeRead, _unprocessedBufferToBeRead.Length * 2);
                }
            }

            // Copy the data into our buffer
            Marshal.Copy((IntPtr)charPtr, _unprocessedBufferToBeRead, _endIndex, charLen);
            _endIndex += charLen;
        }
示例#2
0
        public long AbsolutePosition()
        {
            // The number of bytes that the already-read characters need when encoded.
            int numReadBytes = CurrentEncoding.GetByteCount(_charBuffer, 0, _charPos);

            return(BaseStream.Position - _byteLen + numReadBytes);
        }
        public override int Read()
        {
            var ch = base.Read();

            _position += CurrentEncoding.GetByteCount(new char[] { (char)ch });
            return(ch);
        }
 /// <summary>
 /// Writes contents of toPrint with default encoding
 /// </summary>
 /// <param name="toPrint">Content to write</param>
 public void Print(string toPrint)
 {
     if (string.IsNullOrEmpty(toPrint))
     {
         throw new ArgumentException("toPrint cannot be empty or null", "toPrint");
     }
     Print(CurrentEncoding.GetBytes(toPrint));
 }
    public override string ReadLine()
    {
        string line = base.ReadLine();

        if (line != null)
        {
            _position += CurrentEncoding.GetByteCount(line);
        }
        _position += CurrentEncoding.GetByteCount(Environment.NewLine);
        return(line);
    }
示例#6
0
        public void Read(TextReader stream)
        {
            if (stream == null)
            {
                // Error Message? Log?
                return;
            }

            string str = stream.ReadToEnd();

            byte[]       byteArray = CurrentEncoding.GetBytes(str);
            MemoryStream mstream   = new MemoryStream(byteArray);

            mstream.Position = 0;             //set just to make sure...

            this.Read(new BinaryReader(mstream, this.CurrentEncoding));
        }
示例#7
0
        /// <summary> Reads the buffer in the raw mode. </summary>
        unsafe void ReadExtraBuffer()
        {
            Debug.Assert(IsExtraBufferEmpty());

            byte *bufPtr = stackalloc byte[BytesToBeRead];
            int   result;

            Interop.CheckIo(result = Interop.Sys.ReadStdinUnbuffered(bufPtr, BytesToBeRead));

            Debug.Assert(result > 0 && result <= BytesToBeRead);

            // Now we need to convert the byte buffer to char buffer.
            fixed(char *unprocessedBufPtr = _unprocessedBufferToBeRead)
            {
                _startIndex = 0;
                _endIndex   = CurrentEncoding.GetChars(bufPtr, result, unprocessedBufPtr, _unprocessedBufferToBeRead.Length) - 1;
            }
        }
 private void UpdatePreview()
 {
     try
     {
         using (FileStream aStream = File.OpenRead(FileName))
         {
             aStream.Read(_Bytes, 0, _Bytes.Length);
             aStream.Close();
         }
         Preview = CurrentEncoding.GetString(_Bytes);
         txtPreview.Foreground = new SolidColorBrush(Colors.Black);
     }
     catch (System.Exception ex)
     {
         Preview = ex.Message + ex.StackTrace;
         txtPreview.Foreground = new SolidColorBrush(Colors.Red);
     }
 }
        private void DetermineUnitSizeAndEndianess()
        {
            Span <byte> span = stackalloc byte[4];

            if (CurrentEncoding is null)
            {
                // unknown encoding, assume UTF-8
                UnitSize     = 1;
                _isBigEndian = false;
                return;
            }

            switch (CurrentEncoding)
            {
            default:
                throw new Exception($"Unsupported encoding type {CurrentEncoding.GetType()}");

            case UTF7Encoding utf7Encoding:
            case UTF8Encoding utf8Encoding:
            case ASCIIEncoding aSCIIEncoding:
                _isBigEndian = false;
                UnitSize     = 1;
                break;

            case UTF32Encoding utf32Encoding:
                CurrentEncoding.GetBytes("a", span);
                _isBigEndian = span[0] == 0;
                UnitSize     = 4;
                break;

            case UnicodeEncoding unicodeEncoding:
                CurrentEncoding.GetBytes("a", span);
                _isBigEndian = span[0] == 0;
                UnitSize     = 2;
                break;
            }
        }
示例#10
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            string strBuffer = CurrentEncoding.GetString(buffer, offset, count);

            #region =如果不是HTML文档,不作处理=

            var bof = new Regex("<html", RegexOptions.IgnoreCase);
            if (!bof.IsMatch(strBuffer.ToString()))
            {
                responseStream.Write(buffer, offset, count);
                return;
            }
            #endregion

            //Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);

            //if (!eof.IsMatch(strBuffer))
            //{
            //    responseHtml.Append(strBuffer);
            //}
            //else
            //{

            //}

            responseHtml.Append(strBuffer);
            string finalHtml = responseHtml.ToString();

            finalHtml = _func(finalHtml);

            // Transform the response and write it back out

            byte[] data = CurrentEncoding.GetBytes(finalHtml);

            responseStream.Write(data, 0, data.Length);
        }
示例#11
0
        public override string ReadLine()
        {
            LineNumber++;

            string line = base.ReadLine();

            if (line != null)
            {
                if (ToServerEncoding)
                {
                    return(Extensions.ConvertEncoding(line, CurrentEncoding, SdeAppConfiguration.EncodingServer, _isUtf8));
                }
                if (ToClientEncoding)
                {
                    return(Extensions.ConvertEncoding(line, CurrentEncoding, EncodingService.DisplayEncoding, _isUtf8));
                }
                if (_auto)
                {
                    return(EncodingService.DisplayEncoding.GetString(CurrentEncoding.GetBytes(line)));
                }
            }

            return(line);
        }
示例#12
0
        /// <summary>
        /// Gets the actual position
        /// </summary>
        /// <returns></returns>
        public long GetActualPosition()
        {
            //https://stackoverflow.com/questions/5404267/streamreader-and-seeking

            var numBytesLeft = CurrentEncoding.GetByteCount(_charBuffer, _charPos, _charLen - _charPos);

            // For variable-byte encodings, deal with partial chars at the end of the buffer
            var numFragments = 0;

            if (_byteLen <= 0 || CurrentEncoding.IsSingleByte)
            {
                return(BaseStream.Position - numBytesLeft - numFragments);
            }

            switch (CurrentEncoding.CodePage)
            {
            // UTF-8
            case 65001:
            {
                byte byteCountMask = 0;

                while (_byteBuffer[_byteLen - numFragments - 1] >> 6 == 2
                       )  // if the byte is "10xx xxxx", it's a continuation-byte
                {
                    byteCountMask |=
                        (byte)(1 << ++numFragments);         // count bytes & build the "complete char" mask
                }
                if (_byteBuffer[_byteLen - numFragments - 1] >> 6 == 3
                    )     // if the byte is "11xx xxxx", it starts a multi-byte char.
                {
                    byteCountMask |=
                        (byte)(1 << ++numFragments);         // count bytes & build the "complete char" mask
                }
                // see if we found as many bytes as the leading-byte says to expect
                if (numFragments > 1 && _byteBuffer[_byteLen - numFragments] >> (7 - numFragments)
                    == byteCountMask)
                {
                    numFragments = 0;         // no partial-char in the byte-buffer to account for
                }
                break;
            }

            // UTF-16LE
            case 1200:
            {
                if (_byteBuffer[_byteLen - 1] >= 0xd8)       // high-surrogate
                {
                    numFragments = 2;                        // account for the partial character
                }
                break;
            }

            // UTF-16BE
            case 1201:
            {
                if (_byteBuffer[_byteLen - 2] >= 0xd8)       // high-surrogate
                {
                    numFragments = 2;                        // account for the partial character
                }
                break;
            }
            }

            return(BaseStream.Position - numBytesLeft - numFragments);
        }
 public void Print(string toPrint)
 {
     Print(CurrentEncoding.GetBytes(toPrint));
 }
示例#14
0
        private void SetRtfIndex(RtfToken token, int controlStartIndex)
        {
            while (_rtfIndex < _rtfBytes.Length && IsControlCharValid(CurByte))
            {
                _rtfIndex++;
            }

            int    controlLength = _rtfIndex - controlStartIndex;
            string controlName   = CurrentEncoding.GetString(_rtfBytes, controlStartIndex, controlLength);

            // If control sequence > MAX_CONTROL_LENGTH characters, invalid input.
            if (controlLength > MAX_CONTROL_LENGTH)
            {
                token.Type = RtfTokenType.TokenInvalid;
            }
            else
            {
                token.Type = RtfTokenType.TokenControl;
                token.RtfControlWordInfo = RtfControlWordLookup(controlName);

                if (_rtfIndex < _rtfBytes.Length)
                {
                    if (CurByte == ' ')
                    {
                        _rtfIndex++;
                    }
                    else if (IsParameterStart(CurByte))
                    {
                        bool isNegative = false;

                        if (CurByte == '-')
                        {
                            isNegative = true;
                            _rtfIndex++;
                        }

                        long parameter = 0;

                        int paramStartIndex = _rtfIndex;

                        while (_rtfIndex < _rtfBytes.Length && IsParameterFollow(CurByte))
                        {
                            parameter = (parameter * 10) + (CurByte - '0');
                            _rtfIndex++;
                        }

                        int paramLength = _rtfIndex - paramStartIndex;

                        // Following space is not part of text input
                        if (_rtfIndex < _rtfBytes.Length && CurByte == ' ')
                        {
                            _rtfIndex++;
                        }

                        if (isNegative)
                        {
                            parameter = -parameter;
                        }

                        // If parameter is too long, invalid input.
                        if (paramLength > MAX_PARAM_LENGTH)
                        {
                            token.Type = RtfTokenType.TokenInvalid;
                        }
                        else
                        {
                            token.Parameter = parameter;
                        }
                    }
                }
            }
        }
示例#15
0
 public String ByteToString(byte[] val)
 {
     return(CurrentEncoding.GetString(val));
 }
示例#16
0
        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

        #region Private Methods

        /// <summary>
        /// Called to process sequence of text and \'hh encoded bytes.
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private RtfToXamlError NextText(RtfToken token)
        {
            RtfToXamlError rtfToXamlError = RtfToXamlError.None;

            _rtfLastIndex = _rtfIndex;

            token.Empty();
            token.Type = RtfTokenType.TokenText;

            int  s       = _rtfIndex;
            int  e       = s;
            bool bSawHex = false;

            while (e < _rtfBytes.Length)
            {
                if (IsControl(_rtfBytes[e]))
                {
                    if (_rtfBytes[e] == (byte)'\\' &&
                        e + 3 < _rtfBytes.Length &&
                        _rtfBytes[e + 1] == '\'' &&
                        IsHex(_rtfBytes[e + 2]) &&
                        IsHex(_rtfBytes[e + 3]))
                    {
                        e      += 4;
                        bSawHex = true;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (_rtfBytes[e] == '\r' || _rtfBytes[e] == '\n' || _rtfBytes[e] == 0)
                {
                    break;
                }
                else
                {
                    e++;
                }
            }

            if (s == e)
            {
                token.Type = RtfTokenType.TokenInvalid;
            }
            else
            {
                _rtfIndex = e;

                if (bSawHex)
                {
                    int    i     = 0;
                    int    n     = e - s;
                    byte[] bytes = new byte[n];
                    while (s < e)
                    {
                        if (_rtfBytes[s] == '\\')
                        {
                            bytes[i++] = (byte)((byte)(HexToByte(_rtfBytes[s + 2]) << 4) + HexToByte(_rtfBytes[s + 3]));
                            s         += 4;
                        }
                        else
                        {
                            bytes[i++] = _rtfBytes[s++];
                        }
                    }

                    token.Text = CurrentEncoding.GetString(bytes, 0, i);
                }
                else
                {
                    token.Text = CurrentEncoding.GetString(_rtfBytes, s, e - s);
                }
            }

            return(rtfToXamlError);
        }
 /// <summary>
 /// Gets the position of the internal <see cref="Stream"/>.
 /// </summary>
 /// <returns>The position in the stream.</returns>
 public virtual long Position()
 {
     return(BaseStream.Position + (_charPos > 0 ? CurrentEncoding.GetBytes(_charBuffer, 0, _charPos + 1).Length : 0));
 }
示例#18
0
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------

        #region Internal Methods

        /// <summary>
        ///
        /// </summary>
        /// <param name="token"></param>
        /// <param name="formatState"></param>
        /// <returns></returns>
        internal RtfToXamlError Next(RtfToken token, FormatState formatState)
        {
            RtfToXamlError rtfToXamlError = RtfToXamlError.None;

            _rtfLastIndex = _rtfIndex;

            token.Empty();

            if (_rtfIndex >= _rtfBytes.Length)
            {
                token.Type = RtfTokenType.TokenEOF;
                return(rtfToXamlError);
            }

            int  rtfStartIndex = _rtfIndex;
            byte tokenChar     = _rtfBytes[_rtfIndex++];

            switch (tokenChar)
            {
            // GroupStart
            case (byte)'{':
                token.Type = RtfTokenType.TokenGroupStart;
                break;

            // GroupEnd
            case (byte)'}':
                token.Type = RtfTokenType.TokenGroupEnd;
                break;

            // Control Word
            case (byte)'\r':
            case (byte)'\n':
                token.Type = RtfTokenType.TokenNewline;
                break;

            case (byte)0:
                token.Type = RtfTokenType.TokenNullChar;
                break;

            case (byte)'\\':
                // Input ends with control sequence
                if (_rtfIndex >= _rtfBytes.Length)
                {
                    token.Type = RtfTokenType.TokenInvalid;
                }
                // Normal control character
                else
                {
                    if (IsControlCharValid(CurByte))
                    {
                        int controlStartIndex = _rtfIndex;

                        // Set _rtfIndex to get actual control
                        SetRtfIndex(token, controlStartIndex);

                        // Also provide actual control text - useful for unknown controls
                        token.Text = CurrentEncoding.GetString(_rtfBytes, controlStartIndex - 1, _rtfIndex - rtfStartIndex);
                    }
                    // Hex character
                    else if (CurByte == (byte)'\'')
                    {
                        _rtfIndex--;
                        return(NextText(token));
                    }
                    // Explicit destination
                    else if (CurByte == '*')
                    {
                        _rtfIndex++;
                        token.Type = RtfTokenType.TokenDestination;
                    }
                    // Quoted control character (be generous) - should be limited to "'-*;\_{|}~"
                    else
                    {
                        token.Type = RtfTokenType.TokenTextSymbol;
                        token.Text = CurrentEncoding.GetString(_rtfBytes, _rtfIndex, 1);

                        _rtfIndex++;
                    }
                }

                break;

            // Text or Picture data
            default:
                _rtfIndex--;

                if (formatState != null && formatState.RtfDestination == RtfDestination.DestPicture)
                {
                    token.Type = RtfTokenType.TokenPictureData;
                    break;
                }
                else
                {
                    return(NextText(token));
                }
            }

            return(rtfToXamlError);
        }
        public void Print(string toPrint, IContentParameters parameters)
        {
            IContentReplacer replacer = new ContentReplacer(parameters);

            Print(replacer.Replace(CurrentEncoding.GetBytes(toPrint)));
        }