From 9d514440bf75c5f2006782b3a22b2f505c8ff181 Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 15 Oct 2015 16:04:43 +0900 Subject: [PATCH] [Modify] Add an extension method --- websocket-sharp/Ext.cs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 4e041a13..e004217b 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -296,6 +296,44 @@ namespace WebSocketSharp destination.Write (buff, 0, nread); } + internal static void CopyToAsync ( + this Stream source, + Stream destination, + int bufferLength, + Action completed, + Action error) + { + var buff = new byte[bufferLength]; + + AsyncCallback callback = null; + callback = ar => { + try { + var nread = source.EndRead (ar); + if (nread <= 0) { + if (completed != null) + completed (); + + return; + } + + destination.Write (buff, 0, nread); + source.BeginRead (buff, 0, bufferLength, callback, null); + } + catch (Exception ex) { + if (error != null) + error (ex); + } + }; + + try { + source.BeginRead (buff, 0, bufferLength, callback, null); + } + catch (Exception ex) { + if (error != null) + error (ex); + } + } + internal static byte[] Decompress (this byte[] data, CompressionMethod method) { return method == CompressionMethod.Deflate