From 78261ff71c511233db14a33fdc078b1aac39a501 Mon Sep 17 00:00:00 2001 From: sta Date: Fri, 21 Feb 2014 16:46:16 +0900 Subject: [PATCH] Refactored Ext.cs --- websocket-sharp/Ext.cs | 453 +++++++++++++++++++---------------------- 1 file changed, 204 insertions(+), 249 deletions(-) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 484eb117..5db2cda2 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -2,11 +2,11 @@ /* * Ext.cs * - * Some part of this code are derived from Mono (http://www.mono-project.com). - * - * ParseBasicAuthResponseParams is derived from System.Net.HttpListenerContext.cs - * GetStatusDescription is derived from System.Net.HttpListenerResponse.cs - * IsPredefinedScheme and MaybeUri are derived from System.Uri.cs + * Some parts of this code are derived from Mono (http://www.mono-project.com): + * - GetStatusDescription is derived from System.Net.HttpListenerResponse.cs + * - IsPredefinedScheme is derived from System.Uri.cs + * - MaybeUri is derived from System.Uri.cs + * - ParseBasicAuthResponseParams is derived from System.Net.HttpListenerContext.cs * * The MIT License * @@ -42,7 +42,6 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.Compression; -using System.Linq; using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -131,8 +130,7 @@ namespace WebSocketSharp } } - private static byte [] readBytes ( - this Stream stream, byte [] buffer, int offset, int length) + private static byte [] readBytes (this Stream stream, byte [] buffer, int offset, int length) { var len = stream.Read (buffer, offset, length); if (len < 1) @@ -250,8 +248,7 @@ namespace WebSocketSharp : null; } - internal static string CheckIfValidControlData ( - this byte [] data, string paramName) + internal static string CheckIfValidControlData (this byte [] data, string paramName) { return data.Length > 125 ? String.Format ("'{0}' length must be less.", paramName) @@ -261,8 +258,7 @@ namespace WebSocketSharp internal static string CheckIfValidProtocols (this string [] protocols) { return protocols.Contains ( - protocol => - protocol == null || protocol.Length == 0 || !protocol.IsToken ()) + protocol => protocol == null || protocol.Length == 0 || !protocol.IsToken ()) ? "Contains an invalid value." : protocols.ContainsTwice () ? "Contains a value twice." @@ -308,8 +304,7 @@ namespace WebSocketSharp : null; } - internal static void Close ( - this HttpListenerResponse response, HttpStatusCode code) + internal static void Close (this HttpListenerResponse response, HttpStatusCode code) { response.StatusCode = (int) code; response.OutputStream.Close (); @@ -322,8 +317,7 @@ namespace WebSocketSharp response.Close (HttpStatusCode.Unauthorized); } - internal static byte [] Compress ( - this byte [] value, CompressionMethod method) + internal static byte [] Compress (this byte [] value, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? value.compress () @@ -337,16 +331,14 @@ namespace WebSocketSharp : stream; } - internal static byte [] CompressToArray ( - this Stream stream, CompressionMethod method) + internal static byte [] CompressToArray (this Stream stream, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? stream.compressToArray () : stream.ToByteArray (); } - internal static bool Contains ( - this IEnumerable source, Func comparer) + internal static bool Contains (this IEnumerable source, Func comparer) { foreach (T value in source) if (comparer (value)) @@ -388,8 +380,7 @@ namespace WebSocketSharp src.CopyTo (dest, false); } - internal static void CopyTo ( - this Stream src, Stream dest, bool setDefaultPosition) + internal static void CopyTo (this Stream src, Stream dest, bool setDefaultPosition) { var readLen = 0; var bufferLen = 256; @@ -402,24 +393,21 @@ namespace WebSocketSharp dest.Position = 0; } - internal static byte [] Decompress ( - this byte [] value, CompressionMethod method) + internal static byte [] Decompress (this byte [] value, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? value.decompress () : value; } - internal static Stream Decompress ( - this Stream stream, CompressionMethod method) + internal static Stream Decompress (this Stream stream, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? stream.decompress () : stream; } - internal static byte [] DecompressToArray ( - this Stream stream, CompressionMethod method) + internal static byte [] DecompressToArray (this Stream stream, CompressionMethod method) { return method == CompressionMethod.DEFLATE ? stream.decompressToArray () @@ -427,9 +415,8 @@ namespace WebSocketSharp } /// - /// Determines whether the specified equals the specified - /// , and invokes the specified Action<int> delegate - /// at the same time. + /// Determines whether the specified equals the specified , + /// and invokes the specified Action<int> delegate at the same time. /// /// /// true if equals ; @@ -442,9 +429,9 @@ namespace WebSocketSharp /// A to compare. /// /// - /// An Action<int> delegate that references the method(s) called at the - /// same time as comparing. An parameter to pass to the - /// method(s) is . + /// An Action<int> delegate that references the method(s) called at + /// the same time as comparing. An parameter to pass to + /// the method(s) is . /// /// /// isn't between 0 and 255. @@ -462,8 +449,8 @@ namespace WebSocketSharp /// Gets the absolute path from the specified . /// /// - /// A that represents the absolute path if it's - /// successfully found; otherwise, . + /// A that represents the absolute path if it's successfully found; + /// otherwise, . /// /// /// A that represents a URI to get the absolute path from. @@ -506,19 +493,17 @@ namespace WebSocketSharp : String.Empty; } - internal static string GetNameInternal ( - this string nameAndValue, string separator) + internal static string GetNameInternal (this string nameAndValue, string separator) { - int i = nameAndValue.IndexOf (separator); + var i = nameAndValue.IndexOf (separator); return i > 0 ? nameAndValue.Substring (0, i).Trim () : null; } - internal static string GetValueInternal ( - this string nameAndValue, string separator) + internal static string GetValueInternal (this string nameAndValue, string separator) { - int i = nameAndValue.IndexOf (separator); + var i = nameAndValue.IndexOf (separator); return i >= 0 && i < nameAndValue.Length - 1 ? nameAndValue.Substring (i + 1).Trim () : null; @@ -558,8 +543,8 @@ namespace WebSocketSharp internal static bool IsText (this string value) { - int len = value.Length; - for (int i = 0; i < len; i++) { + var len = value.Length; + for (var i = 0; i < len; i++) { char c = value [i]; if (c < 0x20 && !"\r\n\t".Contains (c)) return false; @@ -593,8 +578,7 @@ namespace WebSocketSharp : String.Format ("\"{0}\"", value.Replace ("\"", "\\\"")); } - internal static NameValueCollection ParseBasicAuthResponseParams ( - this string value) + internal static NameValueCollection ParseBasicAuthResponseParams (this string value) { // HTTP Basic Authentication response is a formatted Base64 string. var userPass = Encoding.Default.GetString ( @@ -649,8 +633,7 @@ namespace WebSocketSharp return stream.readBytes (new byte [length], 0, length); } - internal static byte [] ReadBytes ( - this Stream stream, long length, int bufferLength) + internal static byte [] ReadBytes (this Stream stream, long length, int bufferLength) { using (var result = new MemoryStream ()) { var count = length / bufferLength; @@ -674,10 +657,7 @@ namespace WebSocketSharp } internal static void ReadBytesAsync ( - this Stream stream, - int length, - Action completed, - Action error) + this Stream stream, int length, Action completed, Action error) { var buffer = new byte [length]; stream.BeginRead ( @@ -719,6 +699,18 @@ namespace WebSocketSharp : value; } + internal static T [] Reverse (this T [] array) + { + var len = array.Length; + T [] reverse = new T [len]; + + var end = len - 1; + for (var i = 0; i <= end; i++) + reverse [i] = array [end - i]; + + return reverse; + } + internal static IEnumerable SplitHeaderValue ( this string value, params char [] separator) { @@ -771,22 +763,22 @@ namespace WebSocketSharp } } - internal static byte [] ToByteArrayInternally ( - this ushort value, ByteOrder order) + internal static byte [] ToByteArrayInternally (this ushort value, ByteOrder order) { - var buffer = BitConverter.GetBytes (value); - return order.IsHostOrder () - ? buffer - : buffer.Reverse ().ToArray (); + var bytes = BitConverter.GetBytes (value); + if (!order.IsHostOrder ()) + Array.Reverse (bytes); + + return bytes; } - internal static byte [] ToByteArrayInternally ( - this ulong value, ByteOrder order) + internal static byte [] ToByteArrayInternally (this ulong value, ByteOrder order) { - var buffer = BitConverter.GetBytes (value); - return order.IsHostOrder () - ? buffer - : buffer.Reverse ().ToArray (); + var bytes = BitConverter.GetBytes (value); + if (!order.IsHostOrder ()) + Array.Reverse (bytes); + + return bytes; } internal static CompressionMethod ToCompressionMethod (this string value) @@ -805,8 +797,7 @@ namespace WebSocketSharp : String.Empty; } - internal static System.Net.IPAddress ToIPAddress ( - this string hostNameOrAddress) + internal static System.Net.IPAddress ToIPAddress (this string hostNameOrAddress) { try { var addrs = System.Net.Dns.GetHostAddresses (hostNameOrAddress); @@ -845,21 +836,18 @@ namespace WebSocketSharp /// . /// /// - /// true if a is successfully created; otherwise, - /// false. + /// true if a is successfully created; otherwise, false. /// /// /// A that represents the WebSocket URL to try. /// /// - /// When this method returns, a that represents the - /// WebSocket URL if is valid; otherwise, - /// . + /// When this method returns, a that represents the WebSocket URL if + /// is valid; otherwise, . /// /// - /// When this method returns, a that represents the - /// error message if is invalid; otherwise, - /// . + /// When this method returns, a that represents the error message if + /// is invalid; otherwise, . /// internal static bool TryCreateWebSocketUri ( this string uriString, out Uri result, out string message) @@ -936,12 +924,12 @@ namespace WebSocketSharp #region Public Methods /// - /// Determines whether the specified contains any of - /// characters in the specified array of . + /// Determines whether the specified contains any of characters + /// in the specified array of . /// /// - /// true if contains any of - /// ; otherwise, false. + /// true if contains any of ; + /// otherwise, false. /// /// /// A to test. @@ -959,12 +947,12 @@ namespace WebSocketSharp } /// - /// Determines whether the specified - /// contains the entry with the specified . + /// Determines whether the specified contains the entry + /// with the specified . /// /// - /// true if contains the entry with - /// ; otherwise, false. + /// true if contains the entry + /// with ; otherwise, false. /// /// /// A to test. @@ -980,13 +968,13 @@ namespace WebSocketSharp } /// - /// Determines whether the specified - /// contains the entry with the specified both and - /// . + /// Determines whether the specified contains the entry + /// with the specified both and . /// /// - /// true if contains the entry with both - /// and ; otherwise, false. + /// true if contains the entry + /// with both and ; + /// otherwise, false. /// /// /// A to test. @@ -997,8 +985,7 @@ namespace WebSocketSharp /// /// A that represents the value of the entry to find. /// - public static bool Contains ( - this NameValueCollection collection, string name, string value) + public static bool Contains (this NameValueCollection collection, string name, string value) { if (collection == null || collection.Count == 0) return false; @@ -1015,8 +1002,7 @@ namespace WebSocketSharp } /// - /// Emits the specified delegate if it isn't - /// . + /// Emits the specified delegate if it isn't . /// /// /// A to emit. @@ -1027,16 +1013,15 @@ namespace WebSocketSharp /// /// A that contains no event data. /// - public static void Emit ( - this EventHandler eventHandler, object sender, EventArgs e) + public static void Emit (this EventHandler eventHandler, object sender, EventArgs e) { if (eventHandler != null) eventHandler (sender, e); } /// - /// Emits the specified EventHandler<TEventArgs> delegate if it - /// isn't . + /// Emits the specified EventHandler<TEventArgs> delegate + /// if it isn't . /// /// /// An EventHandler<TEventArgs> to emit. @@ -1059,23 +1044,19 @@ namespace WebSocketSharp } /// - /// Gets the collection of the HTTP Cookies from the specified HTTP - /// . + /// Gets the collection of the HTTP cookies from the specified HTTP . /// /// - /// A that receives a collection of the HTTP - /// Cookies. + /// A that receives a collection of the HTTP cookies. /// /// - /// A that contains a collection of the HTTP - /// Headers. + /// A that contains a collection of the HTTP headers. /// /// - /// true if is a collection of the response - /// headers; otherwise, false. + /// true if is a collection of the response headers; + /// otherwise, false. /// - public static CookieCollection GetCookies ( - this NameValueCollection headers, bool response) + public static CookieCollection GetCookies (this NameValueCollection headers, bool response) { var name = response ? "Set-Cookie" : "Cookie"; return headers == null || !headers.Contains (name) @@ -1087,12 +1068,10 @@ namespace WebSocketSharp /// Gets the description of the specified HTTP status . /// /// - /// A that represents the description of the HTTP status - /// code. + /// A that represents the description of the HTTP status code. /// /// - /// One of values that indicate the HTTP status - /// codes. + /// One of enum values, indicates the HTTP status codes. /// public static string GetDescription (this HttpStatusCode code) { @@ -1100,16 +1079,15 @@ namespace WebSocketSharp } /// - /// Gets the name from the specified that contains a pair - /// of name and value separated by a separator string. + /// Gets the name from the specified that contains a pair of name and + /// value separated by a separator string. /// /// - /// A that represents the name if any; otherwise, - /// null. + /// A that represents the name if any; otherwise, null. /// /// - /// A that contains a pair of name and value separated by - /// a separator string. + /// A that contains a pair of name and value separated by a separator + /// string. /// /// /// A that represents a separator string. @@ -1123,16 +1101,15 @@ namespace WebSocketSharp } /// - /// Gets the name and value from the specified that - /// contains a pair of name and value separated by a separator string. + /// Gets the name and value from the specified that contains a pair of + /// name and value separated by a separator string. /// /// - /// A KeyValuePair<string, string> that represents the name and - /// value if any. + /// A KeyValuePair<string, string> that represents the name and value if any. /// /// - /// A that contains a pair of name and value separated by - /// a separator string. + /// A that contains a pair of name and value separated by a separator + /// string. /// /// /// A that represents a separator string. @@ -1151,8 +1128,7 @@ namespace WebSocketSharp /// Gets the description of the specified HTTP status . /// /// - /// A that represents the description of the HTTP status - /// code. + /// A that represents the description of the HTTP status code. /// /// /// An that represents the HTTP status code. @@ -1212,16 +1188,15 @@ namespace WebSocketSharp } /// - /// Gets the value from the specified that contains - /// a pair of name and value separated by a separator string. + /// Gets the value from the specified that contains a pair of name and + /// value separated by a separator string. /// /// - /// A that represents the value if any; otherwise, - /// null. + /// A that represents the value if any; otherwise, null. /// /// - /// A that contains a pair of name and value separated by - /// a separator string. + /// A that contains a pair of name and value separated by a separator + /// string. /// /// /// A that represents a separator string. @@ -1235,8 +1210,8 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is in the allowable - /// range of the WebSocket close status code. + /// Determines whether the specified is in the allowable range of + /// the WebSocket close status code. /// /// /// Not allowable ranges are the followings. @@ -1248,15 +1223,14 @@ namespace WebSocketSharp /// /// /// - /// Numbers which are greater than 4999 are out of the reserved close - /// status code ranges. + /// Numbers greater than 4999 are out of the reserved close status code ranges. /// /// /// /// /// - /// true if is in the allowable range of the - /// WebSocket close status code; otherwise, false. + /// true if is in the allowable range of the WebSocket + /// close status code; otherwise, false. /// /// /// A to test. @@ -1267,8 +1241,8 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is enclosed in the - /// specified . + /// Determines whether the specified is enclosed in the specified + /// . /// /// /// true if is enclosed in ; @@ -1289,15 +1263,15 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is host (this - /// computer architecture) byte order. + /// Determines whether the specified is host + /// (this computer architecture) byte order. /// /// - /// true if is host byte order; otherwise, - /// false. + /// true if is host byte order; + /// otherwise, false. /// /// - /// A to test. + /// One of the enum values, to test. /// public static bool IsHostOrder (this ByteOrder order) { @@ -1307,8 +1281,8 @@ namespace WebSocketSharp } /// - /// Determines whether the specified - /// represents the local IP address. + /// Determines whether the specified represents + /// the local IP address. /// /// /// true if represents the local IP address; @@ -1325,8 +1299,7 @@ namespace WebSocketSharp if (address == null) throw new ArgumentNullException ("address"); - if (address.Equals (System.Net.IPAddress.Any) || - System.Net.IPAddress.IsLoopback (address)) + if (address.Equals (System.Net.IPAddress.Any) || System.Net.IPAddress.IsLoopback (address)) return true; var host = System.Net.Dns.GetHostName (); @@ -1339,12 +1312,11 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is - /// or empty. + /// Determines whether the specified is or empty. /// /// - /// true if is or - /// empty; otherwise, false. + /// true if is or empty; + /// otherwise, false. /// /// /// A to test. @@ -1355,12 +1327,10 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is a predefined - /// scheme. + /// Determines whether the specified is a predefined scheme. /// /// - /// true if is a predefined scheme; otherwise, - /// false. + /// true if is a predefined scheme; otherwise, false. /// /// /// A to test. @@ -1391,12 +1361,12 @@ namespace WebSocketSharp } /// - /// Determines whether the specified is an - /// HTTP Upgrade request to switch to the specified . + /// Determines whether the specified is an HTTP Upgrade + /// request to switch to the specified . /// /// - /// true if is an HTTP Upgrade request to - /// switch to ; otherwise, false. + /// true if is an HTTP Upgrade request to switch to + /// ; otherwise, false. /// /// /// A that represents the HTTP request. @@ -1418,8 +1388,7 @@ namespace WebSocketSharp /// /// is empty. /// - public static bool IsUpgradeTo ( - this HttpListenerRequest request, string protocol) + public static bool IsUpgradeTo (this HttpListenerRequest request, string protocol) { if (request == null) throw new ArgumentNullException ("request"); @@ -1438,8 +1407,7 @@ namespace WebSocketSharp /// Determines whether the specified is a URI string. /// /// - /// true if is maybe a URI string; otherwise, - /// false. + /// true if is maybe a URI string; otherwise, false. /// /// /// A to test. @@ -1464,19 +1432,18 @@ namespace WebSocketSharp /// A sub-array starts at the specified element position. /// /// - /// An array of T that receives a sub-array, or an empty array of T if any - /// problems with the parameters. + /// An array of T that receives a sub-array, or an empty array of T if any problems + /// with the parameters. /// /// /// An array of T that contains the data to retrieve a sub-array. /// /// - /// An that contains the zero-based starting position of - /// a sub-array in . + /// An that contains the zero-based starting position of a sub-array + /// in . /// /// - /// An that contains the number of elements to retrieve - /// a sub-array. + /// An that contains the number of elements to retrieve a sub-array. /// /// /// The type of elements in the . @@ -1502,8 +1469,7 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate - /// times. + /// Executes the specified delegate times. /// /// /// An is the number of times to execute. @@ -1518,8 +1484,7 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate - /// times. + /// Executes the specified delegate times. /// /// /// A is the number of times to execute. @@ -1534,8 +1499,7 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate - /// times. + /// Executes the specified delegate times. /// /// /// A is the number of times to execute. @@ -1550,8 +1514,7 @@ namespace WebSocketSharp } /// - /// Executes the specified delegate - /// times. + /// Executes the specified delegate times. /// /// /// A is the number of times to execute. @@ -1566,16 +1529,15 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<int> delegate - /// times. + /// Executes the specified Action<int> delegate times. /// /// /// An is the number of times to execute. /// /// - /// An Action<int> delegate that references the method(s) to - /// execute. An parameter to pass to the method(s) is the - /// zero-based count of iteration. + /// An Action<int> delegate that references the method(s) to execute. + /// An parameter to pass to the method(s) is the zero-based count + /// of iteration. /// public static void Times (this int n, Action act) { @@ -1585,16 +1547,15 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<long> delegate - /// times. + /// Executes the specified Action<long> delegate times. /// /// /// A is the number of times to execute. /// /// - /// An Action<long> delegate that references the method(s) to - /// execute. A parameter to pass to the method(s) is the - /// zero-based count of iteration. + /// An Action<long> delegate that references the method(s) to execute. + /// A parameter to pass to the method(s) is the zero-based count + /// of iteration. /// public static void Times (this long n, Action act) { @@ -1604,16 +1565,15 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<uint> delegate - /// times. + /// Executes the specified Action<uint> delegate times. /// /// /// A is the number of times to execute. /// /// - /// An Action<uint> delegate that references the method(s) to - /// execute. A parameter to pass to the method(s) is the - /// zero-based count of iteration. + /// An Action<uint> delegate that references the method(s) to execute. + /// A parameter to pass to the method(s) is the zero-based count + /// of iteration. /// public static void Times (this uint n, Action act) { @@ -1623,16 +1583,15 @@ namespace WebSocketSharp } /// - /// Executes the specified Action<ulong> delegate - /// times. + /// Executes the specified Action<ulong> delegate times. /// /// /// A is the number of times to execute. /// /// - /// An Action<ulong> delegate that references the method(s) to - /// execute. A parameter to pass to this method(s) is the - /// zero-based count of iteration. + /// An Action<ulong> delegate that references the method(s) to execute. + /// A parameter to pass to this method(s) is the zero-based count + /// of iteration. /// public static void Times (this ulong n, Action act) { @@ -1642,21 +1601,20 @@ namespace WebSocketSharp } /// - /// Converts the specified array of to the specified type - /// data. + /// Converts the specified array of to the specified type data. /// /// /// A T converted from , or a default value of T if /// is an empty array of or if the /// type of T isn't , , , /// , , , , - /// , or . + /// , , or . /// /// /// An array of to convert. /// /// - /// A that indicates the byte order of + /// One of the enum values, indicates the byte order of /// . /// /// @@ -1701,8 +1659,7 @@ namespace WebSocketSharp } /// - /// Converts the specified to an array of - /// . + /// Converts the specified to an array of . /// /// /// An array of converted from . @@ -1711,7 +1668,7 @@ namespace WebSocketSharp /// A T to convert. /// /// - /// A that indicates the byte order of the return. + /// One of the enum values, indicates the byte order of the return. /// /// /// The type of . The T must be a value type. @@ -1720,38 +1677,38 @@ namespace WebSocketSharp where T : struct { var type = typeof (T); - var buffer = type == typeof (Boolean) - ? BitConverter.GetBytes ((Boolean)(object) value) - : type == typeof (Byte) - ? new byte [] { (Byte)(object) value } - : type == typeof (Char) - ? BitConverter.GetBytes ((Char)(object) value) - : type == typeof (Double) - ? BitConverter.GetBytes ((Double)(object) value) - : type == typeof (Int16) - ? BitConverter.GetBytes ((Int16)(object) value) - : type == typeof (Int32) - ? BitConverter.GetBytes ((Int32)(object) value) - : type == typeof (Int64) - ? BitConverter.GetBytes ((Int64)(object) value) - : type == typeof (Single) - ? BitConverter.GetBytes ((Single)(object) value) - : type == typeof (UInt16) - ? BitConverter.GetBytes ((UInt16)(object) value) - : type == typeof (UInt32) - ? BitConverter.GetBytes ((UInt32)(object) value) - : type == typeof (UInt64) - ? BitConverter.GetBytes ((UInt64)(object) value) - : new byte []{}; + var bytes = type == typeof (Boolean) + ? BitConverter.GetBytes ((Boolean)(object) value) + : type == typeof (Byte) + ? new byte [] { (Byte)(object) value } + : type == typeof (Char) + ? BitConverter.GetBytes ((Char)(object) value) + : type == typeof (Double) + ? BitConverter.GetBytes ((Double)(object) value) + : type == typeof (Int16) + ? BitConverter.GetBytes ((Int16)(object) value) + : type == typeof (Int32) + ? BitConverter.GetBytes ((Int32)(object) value) + : type == typeof (Int64) + ? BitConverter.GetBytes ((Int64)(object) value) + : type == typeof (Single) + ? BitConverter.GetBytes ((Single)(object) value) + : type == typeof (UInt16) + ? BitConverter.GetBytes ((UInt16)(object) value) + : type == typeof (UInt32) + ? BitConverter.GetBytes ((UInt32)(object) value) + : type == typeof (UInt64) + ? BitConverter.GetBytes ((UInt64)(object) value) + : new byte [0]; - return buffer.Length <= 1 || order.IsHostOrder () - ? buffer - : buffer.Reverse ().ToArray (); + if (bytes.Length > 1 && !order.IsHostOrder ()) + Array.Reverse (bytes); + + return bytes; } /// - /// Converts the order of the specified array of to the - /// host byte order. + /// Converts the order of the specified array of to the host byte order. /// /// /// An array of converted from . @@ -1760,7 +1717,7 @@ namespace WebSocketSharp /// An array of to convert. /// /// - /// One of values that indicate the byte order of + /// One of the enum values, indicates the byte order of /// . /// /// @@ -1771,9 +1728,9 @@ namespace WebSocketSharp if (src == null) throw new ArgumentNullException ("src"); - return src.Length <= 1 || srcOrder.IsHostOrder () - ? src - : src.Reverse ().ToArray (); + return src.Length > 1 && !srcOrder.IsHostOrder () + ? src.Reverse () + : src; } /// @@ -1782,8 +1739,8 @@ namespace WebSocketSharp /// specified . /// /// - /// A converted from , or - /// if it's empty. + /// A converted from , + /// or if is empty. /// /// /// An array of T to convert. @@ -1810,8 +1767,7 @@ namespace WebSocketSharp separator = String.Empty; var buffer = new StringBuilder (64); - (len - 1).Times ( - i => buffer.AppendFormat ("{0}{1}", array [i].ToString (), separator)); + (len - 1).Times (i => buffer.AppendFormat ("{0}{1}", array [i].ToString (), separator)); buffer.Append (array [len - 1].ToString ()); return buffer.ToString (); @@ -1821,8 +1777,8 @@ namespace WebSocketSharp /// Converts the specified to a . /// /// - /// A converted from , or - /// if it's or empty. + /// A converted from , or + /// if is or empty. /// /// /// A to convert. @@ -1840,8 +1796,8 @@ namespace WebSocketSharp /// URL-decodes the specified . /// /// - /// A that receives the decoded string, or the - /// if it's or empty. + /// A that receives the decoded string, or the + /// if it's or empty. /// /// /// A to decode. @@ -1857,8 +1813,8 @@ namespace WebSocketSharp /// URL-encodes the specified . /// /// - /// A that receives the encoded string, or - /// if it's or empty. + /// A that receives the encoded string, or + /// if it's or empty. /// /// /// A to encode. @@ -1871,7 +1827,7 @@ namespace WebSocketSharp } /// - /// Writes the specified data using the specified + /// Writes the specified data with the specified /// . /// /// @@ -1879,13 +1835,12 @@ namespace WebSocketSharp /// used to write the content data. /// /// - /// An array of that contains the content data to write. + /// An array of that represents the content data to write. /// /// /// is . /// - public static void WriteContent ( - this HttpListenerResponse response, byte [] content) + public static void WriteContent (this HttpListenerResponse response, byte [] content) { if (response == null) throw new ArgumentNullException ("response");