From 4d5add5e8d10bccc9539cba64a084e62b3451bfc Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 10 Sep 2015 14:20:04 +0900 Subject: [PATCH] [Modify] Add ReadBytesAsync2 method Add ReadBytesAsync2 (Stream, long, int, Action, Action) method to support pull request #153 --- websocket-sharp/Ext.cs | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index ea1dd804..b7d437bc 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -666,6 +666,54 @@ namespace WebSocketSharp stream.readBytesAsync (new byte[length], 0, length, completed, error); } + internal static void ReadBytesAsync2 ( + this Stream stream, + long length, + int bufferLength, + Action completed, + Action error) + { + var dest = new MemoryStream (); + var buff = new byte[bufferLength]; + + Action read = null; + read = len => { + if (len < bufferLength) { + bufferLength = (int) len; + buff = new byte[bufferLength]; + } + + stream.readBytesAsync ( + buff, + 0, + bufferLength, + bytes => { + var nread = bytes.Length; + if (nread > 0) + dest.Write (bytes, 0, nread); + + if (nread == 0 || (len -= nread) == 0) { + if (completed != null) { + dest.Close (); + completed (dest.ToArray ()); + } + + dest.Dispose (); + return; + } + + read (len); + }, + ex => { + dest.Dispose (); + if (error != null) + error (ex); + }); + }; + + read (length); + } + internal static string RemovePrefix (this string value, params string[] prefixes) { var idx = 0;