本文整理汇总了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);
}
}
示例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;
}
示例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();
}
示例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);
}
}
示例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;
}
示例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;
}
示例7: AsyncComparer
private AsyncComparer(Stream source, byte[] expected, Action<bool> completed)
{
_expected = expected;
_src = source;
_completed = completed;
_src.BeginRead(_buffer, 0, ChunkSize, OnRead, null);
}
示例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;
}
示例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);
}
示例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;
}
示例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 );
}
示例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);
}
示例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;
}
示例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);
}
示例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
);
}