private Console() { ReadLock = new Object(); WriteLock = new Object(); String csname = encoding(); if (csname != null) { try { Cs = Charset.ForName(csname); } catch (Exception) { } } if (Cs == null) { Cs = Charset.DefaultCharset(); } @out = StreamEncoder.forOutputStreamWriter(new FileOutputStream(FileDescriptor.@out), WriteLock, Cs); Pw = new PrintWriterAnonymousInnerClassHelper(this, @out); Formatter = new Formatter(@out); Reader_Renamed = new LineReader(this, StreamDecoder.forInputStreamReader(new FileInputStream(FileDescriptor.@in), ReadLock, Cs)); Rcb = new char[1024]; }
protected void P_ErrorDataReceived(object sender, DataReceivedEventArgs e) { bool flag = e.Data != null; if (flag) { Data = e.Data; ContextData.Add(e.Data); bool flag2 = e.Data.IndexOf("configuration") != -1; if (flag2) { try { Configuration = e.Data.Substring(e.Data.IndexOf("configuration") + 14); } catch { } } bool flag3 = e.Data.IndexOf("Duration") != -1; if (flag3) { try { Duration = TimeSpan.Parse(e.Data.Substring(e.Data.IndexOf("Duration") + 10, 11)); } catch { } } bool flag4 = e.Data.IndexOf("start") != -1; if (flag4) { try { StartTime = e.Data.Substring(e.Data.IndexOf("start") + 7, 8); } catch { } } bool flag5 = e.Data.IndexOf("Duration") != -1 && e.Data.IndexOf("start") != -1; if (flag5) { try { PrimaryBitrate = e.Data.Substring(e.Data.IndexOf("bitrate") + 8, e.Data.IndexOf("kb/s") - (e.Data.IndexOf("bitrate") + 8 - 4)).Trim().Replace(" ", ""); } catch { } try { PrimaryFileSize = ((double)(int.Parse(PrimaryBitrate.Substring(0, PrimaryBitrate.Length - 4)) / 1024) * Duration.TotalSeconds / 8.0).ToString() + "M"; } catch { } } bool flag6 = e.Data.IndexOf("Stream") != -1 && e.Data.IndexOf("mapping") == -1 && e.Data.IndexOf("->") == -1; if (flag6) { try { StreamInfo.Add(e.Data.Substring(e.Data.IndexOf("Stream") + 6)); } catch { } try { PrimaryFPS = double.Parse(e.Data.Substring(e.Data.IndexOf("fps") - 6, 5)); } catch { } try { bool flag7 = e.Data.IndexOf("Video") != -1; if (flag7) { HasVideo = true; } } catch { } try { bool flag8 = e.Data.IndexOf("Audio") != -1; if (flag8) { HasAudio = true; } } catch { } } bool flag9 = e.Data.IndexOf("encoder") != -1; if (flag9) { try { StreamEncoder.Add(e.Data.Substring(e.Data.IndexOf("encoder") + 18)); } catch { } } bool flag10 = e.Data.IndexOf("->") != -1; if (flag10) { try { StreamMapping.Add(e.Data); } catch { } } bool flag11 = e.Data.IndexOf("frame") != -1 && e.Data.IndexOf("fps") != -1; if (flag11) { try { Frame = Convert.ToInt32(e.Data.Substring(e.Data.IndexOf("frame") + 6, e.Data.IndexOf("fps") - (e.Data.IndexOf("frame") + 6)).Trim()); } catch { } } bool flag12 = e.Data.IndexOf("fps") != -1 && e.Data.IndexOf("q") != -1; if (flag12) { try { FPS = Convert.ToInt32(e.Data.Substring(e.Data.IndexOf("fps") + 4, e.Data.IndexOf("q") - (e.Data.IndexOf("fps") + 4)).Trim()); } catch { } } bool flag13 = e.Data.IndexOf("q") != -1 && e.Data.IndexOf("size") != -1; if (flag13) { try { Quality = Convert.ToInt32(e.Data.Substring(e.Data.IndexOf("q") + 2, e.Data.IndexOf("size") - (e.Data.IndexOf("q") + 5)).Trim()); } catch { } } bool flag14 = e.Data.IndexOf("size") != -1 && e.Data.IndexOf("time") != -1; if (flag14) { try { CompiledSize = e.Data.Substring(e.Data.IndexOf("size") + 5, e.Data.IndexOf("time") - (e.Data.IndexOf("size") + 5)).Trim(); } catch { } } bool flag15 = e.Data.IndexOf("time") != -1 && e.Data.IndexOf("bitrate") != -1; if (flag15) { try { CompiledTime = TimeSpan.Parse(e.Data.Substring(e.Data.IndexOf("time") + 5, e.Data.IndexOf("bitrate") - (e.Data.IndexOf("time") + 5)).Trim()); } catch { } } bool flag16 = e.Data.IndexOf("bitrate") != -1 && e.Data.IndexOf("speed") != -1; if (flag16) { try { Bitrate = e.Data.Substring(e.Data.IndexOf("bitrate") + 8, Data.IndexOf("speed") - (e.Data.IndexOf("bitrate") + 8)).Trim(); } catch { } } bool flag17 = e.Data.IndexOf("speed") != -1; if (flag17) { try { Speed = e.Data.Substring(e.Data.IndexOf("speed") + 6).Trim(); } catch { } } bool flag18 = Speed != "" && Speed != "0.00x" && Duration != TimeSpan.Zero && CompiledTime != TimeSpan.Zero; if (flag18) { try { ForecastTime = TimeSpan.FromSeconds((Duration.TotalSeconds - CompiledTime.TotalSeconds) / double.Parse(Speed.Replace("x", ""))); } catch { } } else { ForecastTime = TimeSpan.Zero; } } }
/// <summary> /// 识别编码 /// </summary> /// <param name="objHttpItems"></param> private void GetEncodingNew(ref HttpItems objHttpItems) { string temp = Encoding.Default.GetString(_rawResponse, 0, _rawResponse.Length); Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline); string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty; if (charter.IndexOf("\"") > 0) { charter = charter.Split('\"')[0]; } if (charter.IndexOf(" ") > 0) { charter = charter.Split(' ')[0]; } charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty); if (charter.Length > 0) { charter = charter.ToLower().Replace("iso-8859-1", "gbk"); if (string.IsNullOrEmpty(_response.CharacterSet.Trim()) || _response.CharacterSet.Trim().Contains("utf")) { objHttpItems.Encoding = Encoding.UTF8; } else { Encoding ed = StreamEncoder.GetEncodingFromBytes(_rawResponse); if (ed != null) { objHttpItems.Encoding = ed; } else { //强制UTF8 objHttpItems.Encoding = Encoding.UTF8; } } } else { if (_response.CharacterSet != null) { if (_response.CharacterSet.ToLower().Trim() == "iso-8859-1") { // encoding = Encoding.GetEncoding("gbk"); 直接改为UTF8编码 objHttpItems.Encoding = Encoding.GetEncoding("gbk");//Encoding.UTF8; } else { if (string.IsNullOrEmpty(_response.CharacterSet.Trim()) || _response.CharacterSet.Trim().Contains("utf")) { objHttpItems.Encoding = Encoding.UTF8; } else { objHttpItems.Encoding = Encoding.GetEncoding(_response.CharacterSet); } } } else { Encoding ed = StreamEncoder.GetEncodingFromBytes(_rawResponse); if (ed != null) { objHttpItems.Encoding = ed; } else { //强制UTF8 objHttpItems.Encoding = Encoding.UTF8; } } } }
/// <summary> /// 处理响应结果 /// </summary> /// <param name="objHttpItems">请求设置参数</param> /// <returns></returns> private HttpResults GetResponesInfo(ref HttpItems objHttpItems) { #region 设置返回结果 _result.StatusCode = _response.StatusCode; _result.StatusDescription = _response.StatusDescription; _result.Header = _response.Headers; if (_response.Cookies != null) { _result.CookieCollection = _response.Cookies; } if (_response.ResponseUri != null) { _result.ResponseUrl = _response.ResponseUri.ToString(); } if (_response.Headers["set-cookie"] != null) { //清理垃圾Cookie信息 _result.Cookie = new XJHTTP().ClearCookie(_response.Headers["set-cookie"]); _result.RawCookie = _response.Headers["set-cookie"]; } //处理返回值Container _result.Container = objHttpItems.Container; #endregion #region 急速请求,不解析结果数据 if (objHttpItems.ResultType == ResultType.So) { return(_result); } #endregion #region 应答结果转换为内存数据 using (_stream = new MemoryStream()) { //GZIIP处理 if (_response.ContentEncoding != null && _response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { _stream = GetMemoryStream(new GZipStream(_response.GetResponseStream(), CompressionMode.Decompress)); } else { _stream = GetMemoryStream(_response.GetResponseStream()); } //获取Byte _rawResponse = _stream.ToArray(); } #endregion #region 如果设置返回结果类型则返回Byte数据 if (objHttpItems.ResultType == ResultType.Byte) { _result.ResultByte = _rawResponse; return(_result); } #endregion #region 处理编码结果 if (objHttpItems.Encoding == null)//如果用户没有指定编码格式 { try { Encoding ed = StreamEncoder.GetEncodingFromBytes(_rawResponse); if (ed != null) { objHttpItems.Encoding = ed; } else { GetEncodingNew(ref objHttpItems); } } catch { //强制UTF8 objHttpItems.Encoding = Encoding.UTF8; } } //得到返回的HTML try { if (_rawResponse.Length > 0) { _result.Html = objHttpItems.Encoding.GetString(_rawResponse); } else { _result.Html = "HttpCode接收数据长度为0.请检查数据包.当前状态码:" + _result.StatusCodeNum.ToString() + _result.Html; } _response.Close(); } catch { _result.Html = "HttpCode接收数据出现异常..请检查数据包.当前状态码:" + _result.StatusCodeNum.ToString() + _result.Html; } #endregion return(_result); }
/// <summary> /// Constructs a writer that encodes characters using the given encoder and /// writes the resulting bytes to the given channel. /// /// <para> The resulting stream will contain an internal output buffer of at /// least <tt>minBufferCap</tt> bytes. The stream's <tt>write</tt> methods /// will, as needed, flush the buffer by writing bytes to the underlying /// channel; if the channel is in non-blocking mode when bytes are to be /// written then an <seealso cref="IllegalBlockingModeException"/> will be thrown. /// The resulting stream will not otherwise be buffered. Closing the stream /// will in turn cause the channel to be closed. </para> /// </summary> /// <param name="ch"> /// The channel to which bytes will be written /// </param> /// <param name="enc"> /// The charset encoder to be used /// </param> /// <param name="minBufferCap"> /// The minimum capacity of the internal byte buffer, /// or <tt>-1</tt> if an implementation-dependent /// default capacity is to be used /// </param> /// <returns> A new writer </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not allowed in .NET: //ORIGINAL LINE: public static java.io.Writer newWriter(final WritableByteChannel ch, final java.nio.charset.CharsetEncoder enc, final int minBufferCap) public static Writer NewWriter(WritableByteChannel ch, CharsetEncoder enc, int minBufferCap) { CheckNotNull(ch, "ch"); return(StreamEncoder.forEncoder(ch, enc.Reset(), minBufferCap)); }