Added the Ext.UTF8Decode method

This commit is contained in:
sta 2015-08-22 11:50:19 +09:00
parent ddb5819a0e
commit f8f68e49b0
3 changed files with 12 additions and 16 deletions

View File

@ -87,7 +87,7 @@ namespace WebSocketSharp
: (ushort) CloseStatusCode.NoStatus; : (ushort) CloseStatusCode.NoStatus;
_reason = len > 2 _reason = len > 2
? Encoding.UTF8.GetString (_rawData.SubArray (2, len - 2)) ? _rawData.SubArray (2, len - 2).UTF8Decode ()
: String.Empty; : String.Empty;
} }

View File

@ -838,6 +838,16 @@ namespace WebSocketSharp
: value.Substring (start + 1, len).Replace ("\\\"", "\""); : value.Substring (start + 1, len).Replace ("\\\"", "\"");
} }
internal static string UTF8Decode (this byte[] bytes)
{
try {
return Encoding.UTF8.GetString (bytes);
}
catch {
return null;
}
}
internal static void WriteBytes (this Stream stream, byte[] bytes) internal static void WriteBytes (this Stream stream, byte[] bytes)
{ {
using (var input = new MemoryStream (bytes)) using (var input = new MemoryStream (bytes))

View File

@ -88,7 +88,7 @@ namespace WebSocketSharp
get { get {
if (!_dataSet) { if (!_dataSet) {
_data = _opcode != Opcode.Binary _data = _opcode != Opcode.Binary
? convertToString (_rawData) ? _rawData.UTF8Decode ()
: BitConverter.ToString (_rawData); : BitConverter.ToString (_rawData);
_dataSet = true; _dataSet = true;
@ -123,19 +123,5 @@ namespace WebSocketSharp
} }
#endregion #endregion
#region Private Methods
private static string convertToString (byte[] rawData)
{
try {
return Encoding.UTF8.GetString (rawData);
}
catch {
return null;
}
}
#endregion
} }
} }