当前位置: 首页>>代码示例>>C#>>正文


C# Stream.BeginRead方法代码示例

本文整理汇总了C#中System.IO.Stream.BeginRead方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.BeginRead方法的具体用法?C# Stream.BeginRead怎么用?C# Stream.BeginRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Stream的用法示例。


在下文中一共展示了Stream.BeginRead方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadUnsignedVarint

 private static void ReadUnsignedVarint(
     Stream stream,
     Bcp.ReadState readState,
     ProcessReadVarint processReadVarint,
     BcpDelegate.ExceptionHandler exceptionHandler)
 {
     var buffer = new byte[1];
     var i = 0;
     uint result = 0U;
     AsyncCallback asyncCallback = null;
     asyncCallback = asyncResult =>
     {
         try
         {
             int numBytesRead = stream.EndRead(asyncResult);
             if (numBytesRead != 1)
             {
                 exceptionHandler(new EndOfStreamException());
             }
             uint b = buffer[0];
             if (i < 32)
             {
                 if (b >= 0x80)
                 {
                     result |= ((b & 0x7f) << i);
                     i += 7;
                     stream.BeginRead(buffer, 0, 1, asyncCallback, readState);
                 }
                 else
                 {
                     result |= (b << i);
                     processReadVarint(result);
                 }
             }
             else
             {
                 exceptionHandler(new BcpException.VarintTooBig());
             }
         }
         catch (Exception e)
         {
             exceptionHandler(e);
         }
     };
     try
     {
         stream.BeginRead(buffer, 0, 1, asyncCallback, readState);
     }
     catch (Exception e)
     {
         exceptionHandler(e);
     }
 }
开发者ID:qifun,项目名称:CSharpBcp,代码行数:53,代码来源:BcpIO.cs

示例2: StreamContainsSeqeunce

 public bool StreamContainsSeqeunce(Stream contentStream)
 {
     int num;
     if (contentStream == null)
     {
         throw new ArgumentNullException("contentStream");
     }
     if (this.FSequence == null)
     {
         throw new ArgumentNullException("sequence");
     }
     if (this.FSequence.Length == 0)
     {
         return false;
     }
     byte[] buffer = new byte[0x40000];
     byte[] buffer2 = new byte[0x40000];
     byte[] buffer3 = buffer;
     byte[] buffer4 = buffer2;
     int offset = 0;
     bool flag = false;
     IAsyncResult asyncResult = contentStream.BeginRead(buffer3, 0, buffer3.Length, null, null);
     do
     {
         if (!base.OnProgress(0))
         {
             break;
         }
         num = contentStream.EndRead(asyncResult);
         if (num > 0)
         {
             buffer3 = Interlocked.Exchange<byte[]>(ref buffer4, buffer3);
             int num3 = offset;
             offset = Math.Min(this.FSequence.Length - 1, num);
             asyncResult = contentStream.BeginRead(buffer3, offset, buffer3.Length - offset, null, null);
             flag = ByteArrayHelper.IndexOf(this.FSequence, buffer4, buffer4.Length) >= 0;
             Array.Copy(buffer4, (num + num3) - offset, buffer3, 0, offset);
         }
         else
         {
             asyncResult = null;
         }
     }
     while ((num > 0) && !flag);
     if (asyncResult != null)
     {
         contentStream.EndRead(asyncResult);
     }
     return flag;
 }
开发者ID:shankithegreat,项目名称:commanderdotnet,代码行数:50,代码来源:HexContentFilter.cs

示例3: Main

        static void Main(string[] args)
        {
            _readBuffer = new byte[960];
            Console.WriteLine("Opening \"{0}\" for playback", OpenALHelper.PlaybackDevices[0].DeviceName);
            Console.WriteLine("Opening \"{0}\" for capture", OpenALHelper.CaptureDevices[0].DeviceName);

            _playback = OpenALHelper.PlaybackDevices[0].OpenStream(48000, OpenALAudioFormat.Mono16Bit);
            _playback.Listener.Position = new Vector3() { X = 0.0f, Y = 0.0f, Z = 0.0f };
            _playback.Listener.Velocity = new Vector3() { X = 0.0f, Y = 0.0f, Z = 0.0f };
            _playback.Listener.Orientation = new Orientation()
                {
                    At = new Vector3() { X = 0.0f, Y = 0.0f, Z = 1.0f },
                    Up = new Vector3() { X = 0.0f, Y = 1.0f, Z = 0.0f }
                };
            _playback.ALPosition = new Vector3() { X = 0.0f, Y = 0.0f, Z = 0.0f };
            _playback.Velocity = new Vector3() { X = 0.0f, Y = 0.0f, Z = 0.0f };
            _capture = OpenALHelper.CaptureDevices[0].OpenStream(48000, OpenALAudioFormat.Mono16Bit, 10);
            _capture.BeginRead(_readBuffer, 0, _readBuffer.Length, Callback, null);

            while (true)
            {
                PrintUI();
                if (ProcessInput())
                    break;
            }

            _playback.Close();
            _playback.Dispose();
            _capture.Close();
            _capture.Dispose();
        }
开发者ID:JennettaGong,项目名称:OpenAL.NET,代码行数:31,代码来源:Program.cs

示例4: StreamReadAsync

        public static void StreamReadAsync(
            Stream stream,
            byte[] buffer,
            int offset,
            int count,
            SynchronizationContextWrapper syncContext,
            Action<int, Exception> callback)
        {
            Exception error = null;
            int length = 0;
            try
            {
                stream.BeginRead(buffer, offset, count,
                    delegate(IAsyncResult ar)
                    {
                        try
                        {
                            length = stream.EndRead(ar);
                        }
                        catch (Exception e)
                        {
                            error = e;
                        }

                        callback(length, error);
                    },
                    null);
            }
            catch (Exception e)
            {
                error = e;
                callback(length, error);
            }
        }
开发者ID:harunpehlivan,项目名称:LiveSDK-for-Windows,代码行数:34,代码来源:Platform.cs

示例5: Pipe

        /// <summary>
        /// Pipes data from one stream to another
        /// </summary>
        /// <param name="input">Stream to read from</param>
        /// <param name="output">Stream to write to</param>
        /// <param name="MaxBufferSize">Size of buffer to use</param>
        /// <returns>Number of bytes piped</returns>
        public static int Pipe(Stream input, Stream output, int MaxBufferSize)
        {
            //Internal buffer are two buffers, each half of allowed buffer size, aligned to 1kb blocks
            int bufferSize = (MaxBufferSize / 2) & ~1023;
            if (bufferSize <= 0) throw new Exception("Specified buffer size to small");

            byte[][] buffer = new byte[2][];
            buffer[0] = new byte[bufferSize];
            buffer[1] = new byte[bufferSize];
            int currentBuffer = 0;

            int r, total=0;
            IAsyncResult asyncRead,asyncWrite;

            //Read first block
            r = input.Read(buffer[currentBuffer], 0, bufferSize);

            //Continue while we're getting data
            while (r > 0)
            {
                //read and write simultaneously
                asyncWrite = output.BeginWrite(buffer[currentBuffer], 0, r, null, null);
                asyncRead = input.BeginRead(buffer[1-currentBuffer], 0, bufferSize, null, null);
                //Wait for both
                output.EndWrite(asyncWrite);
                r = input.EndRead(asyncRead);
                //Switch buffers
                currentBuffer = 1 - currentBuffer;
                //Count bytes
                total += r;
            }

            //Return number of bytes piped
            return total;
        }
开发者ID:abdul-baten,项目名称:hbcms,代码行数:42,代码来源:DBRead.aspx.cs

示例6: Connect

        public bool Connect()
        {
            try
            {
                // Initialize Packet Factory
                PacketHandler.Initialize();

                // Connect Socket
                m_Socket = new TcpClient(m_ServerIP, m_Port);
                m_Stream = m_Socket.GetStream();
                m_Stream.BeginRead(m_bRecvBuffer, 0, MAX_PACKET_SIZE, new AsyncCallback(EndDataReceive), null);

                if (m_Socket.Connected) {
                    Debugger.Log("Connected to {0} on {1}", m_ServerIP, m_Port);
                } else {
                    Disconnect();
                    throw new ApplicationException("Failed to connect to server");
                }

                return true;
            }
            catch(Exception e)
            {
                Debugger.Log (Debugger.LogLevel.Info, "SocketServer", "Connect: {0}", e.Message);
            }
            return false;
        }
开发者ID:rramsden,项目名称:multiverse-client,代码行数:27,代码来源:SocketClient.cs

示例7: AsyncComparer

        private AsyncComparer(Stream source, byte[] expected, Action<bool> completed)
        {
            _expected = expected;
            _src = source;
            _completed = completed;

            _src.BeginRead(_buffer, 0, ChunkSize, OnRead, null);
        }
开发者ID:namelesstwice,项目名称:NConfiguration,代码行数:8,代码来源:AsyncComparer.cs

示例8: Connect

 public void Connect()
 {
     _channel = StreamFactory.CreateStream(_connString);
     readOp = _channel.BeginRead(headerBuffer, 0, 1, callback, null);
     // Give the qosManager a handle to the streams
     qosManager.SetStreamManager(this);
     _connected = true;
 }
开发者ID:EriclLee,项目名称:MqttDotNet,代码行数:8,代码来源:StreamManager.cs

示例9: ReadDataFromStream

        private static string ReadDataFromStream(Stream str)
        {
            var buff = new byte[10000];
            var asyncResult = str.BeginRead(buff, 0, buff.Length, null, null);

            var bytesRead = str.EndRead(asyncResult);
            return Encoding.UTF8.GetString(buff, 0, bytesRead);
        }
开发者ID:pstephens,项目名称:BibleLegacy,代码行数:8,代码来源:ResourceLoaderTests.cs

示例10: OnEnter

        private IAsyncResult OnEnter(Object sender, EventArgs e, AsyncCallback cb, Object state) {
            Debug.Assert(_inputStream == null);
            _app = (HttpApplication)sender;
            HttpContext context = _app.Context;
            HttpRequest request = context.Request;
            HttpWorkerRequest wr = context.WorkerRequest;
            HttpAsyncResult httpAsyncResult = new HttpAsyncResult(cb, state);
            AsyncPreloadModeFlags asyncPreloadMode = context.AsyncPreloadMode;
            int contentLength;
            bool isForm = false;
            bool isFormMultiPart = false;

            if (asyncPreloadMode == AsyncPreloadModeFlags.None
                || request.ReadEntityBodyMode != ReadEntityBodyMode.None
                || wr == null
                || !wr.SupportsAsyncRead
                || !wr.HasEntityBody()
                || wr.IsEntireEntityBodyIsPreloaded()
                || context.Handler == null
                || context.Handler is TransferRequestHandler
                || context.Handler is DefaultHttpHandler
                || (contentLength = request.ContentLength) > RuntimeConfig.GetConfig(context).HttpRuntime.MaxRequestLengthBytes
                || ((isForm = StringUtil.StringStartsWithIgnoreCase(request.ContentType, "application/x-www-form-urlencoded"))
                    && (asyncPreloadMode & AsyncPreloadModeFlags.Form) != AsyncPreloadModeFlags.Form)
                || ((isFormMultiPart = StringUtil.StringStartsWithIgnoreCase(request.ContentType, "multipart/form-data"))
                    && (asyncPreloadMode & AsyncPreloadModeFlags.FormMultiPart) != AsyncPreloadModeFlags.FormMultiPart)
                || !isForm && !isFormMultiPart && (asyncPreloadMode & AsyncPreloadModeFlags.NonForm) != AsyncPreloadModeFlags.NonForm
                ) {
                Debug.Trace("AsyncPreload", " *** AsyncPreload skipped *** ");
                httpAsyncResult.Complete(true, null, null);
                return httpAsyncResult;
            }

            Debug.Trace("AsyncPreload", " *** AsyncPreload started *** ");
            try {
                if (_callback == null) {
                    _callback = new AsyncCallback(OnAsyncCompletion);
                }
                _inputStream = request.GetBufferedInputStream();
                byte[] buffer = _app.EntityBuffer;
                int bytesRead = 0;
                // loop to prevent recursive calls and potential stack overflow if/when it completes synchronously
                do {
                    IAsyncResult readAsyncResult = _inputStream.BeginRead(buffer, 0, buffer.Length, _callback, httpAsyncResult);
                    if (!readAsyncResult.CompletedSynchronously) {
                        return httpAsyncResult;
                    }
                    bytesRead = _inputStream.EndRead(readAsyncResult);
                } while (bytesRead != 0);
            }
            catch {
                Reset();
                throw;
            }
            httpAsyncResult.Complete(true, null, null);
            return httpAsyncResult;
        }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:57,代码来源:ImplicitAsyncPreloadModule.cs

示例11: BeginRead

 public IAsyncResult BeginRead( Stream stream, AsyncCallback callback )
 {
     if ( IsFull ) {
         Debug.Print( "FancyBuffer.BeginRead: buffer is full" );
         throw new Exception( "Read buffer is full." );
     }
     _stream = stream;
     return _stream.BeginRead( Buffer, Offset, Available, callback, this );
 }
开发者ID:BGCX261,项目名称:ziveirc-svn-to-git,代码行数:9,代码来源:FancyBuffer.cs

示例12: AsyncStreamReader

		public AsyncStreamReader(Stream stream, Action<byte[]> completed, int bufferSize)
		{
			_bufferSize = bufferSize;
			_buffer = new byte[_bufferSize];
			_completed = completed;
			_stream = stream;
			_all = new List<byte>();

			_stream.BeginRead(_buffer, 0, _bufferSize, Callback, this);
		}
开发者ID:adamjmoon,项目名称:AwsSesWrapper.Net,代码行数:10,代码来源:AsyncStreamReader.cs

示例13: ReadTask

		private static Task<int> ReadTask(Stream stream, byte[] buffer, int offset, int count, object state)
		{
			var tcs = new TaskCompletionSource<int>();
			stream.BeginRead(buffer, offset, count, ar =>
			{
				try { tcs.SetResult(stream.EndRead(ar)); }
				catch (Exception exc) { tcs.SetException(exc); }
			}, state);
			return tcs.Task;
		}
开发者ID:Bindsi,项目名称:AsyncAwaitWorkshop,代码行数:10,代码来源:Example.cs

示例14: ReadStream

 public static int ReadStream(Stream stream, byte[] buffer, int offset, int count, int timeout)
 {
     if (stream == null || buffer == null || buffer.Length < (offset + count))
         return -1;
     IAsyncResult arRead = stream.BeginRead(buffer, offset, count, null, null);
     if (!arRead.AsyncWaitHandle.WaitOne(timeout))
     {
         throw new TimeoutException("Could not read within the timeout period.");
     }
     return stream.EndRead(arRead);
 }
开发者ID:JoeGilkey,项目名称:RadioLog,代码行数:11,代码来源:StreamReadHelper.cs

示例15: Copy

		public static void Copy( Stream in_sourceStream, Stream out_destStream ) {
			Streams streams = new Streams( in_sourceStream, out_destStream );

			IAsyncResult ar = in_sourceStream.BeginRead(
				streams.Buffer, 
				0, 
				streams.BufferSize, 
				new AsyncCallback( CopyStreamCallback ), 
				streams
			);
		}
开发者ID:modulexcite,项目名称:Comet-server-for-ASP.NET,代码行数:11,代码来源:CopyStreamAsync.cs


注:本文中的System.IO.Stream.BeginRead方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。