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); } }
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); }
//------------------------------------------------------ // // 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); }
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; } } } } }
//------------------------------------------------------ // // 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); }
public String ByteToString(byte[] val) { return(CurrentEncoding.GetString(val)); }