Refactored a few for Ext.cs

This commit is contained in:
sta 2015-05-29 17:49:08 +09:00
parent 3c8b8210ac
commit 66173be9cb

View File

@ -144,36 +144,26 @@ 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 count)
{ {
var len = 0; var cnt = 0;
try { try {
len = stream.Read (buffer, offset, length); cnt = stream.Read (buffer, offset, count);
if (len < 1) if (cnt < 1)
return buffer.SubArray (0, offset); return buffer.SubArray (0, offset);
while (len < length) { while (cnt < count) {
var nread = stream.Read (buffer, offset + len, length - len); var nread = stream.Read (buffer, offset + cnt, count - cnt);
if (nread < 1) if (nread < 1)
break; break;
len += nread; cnt += nread;
} }
} }
catch { catch {
} }
return len < length ? buffer.SubArray (0, offset + len) : buffer; return cnt < count ? buffer.SubArray (0, offset + cnt) : buffer;
}
private static bool readBytes (
this Stream stream, byte[] buffer, int offset, int length, Stream destination)
{
var bytes = stream.readBytes (buffer, offset, length);
var len = bytes.Length;
destination.Write (bytes, 0, len);
return len == offset + length;
} }
private static void times (this ulong n, Action action) private static void times (this ulong n, Action action)
@ -182,6 +172,15 @@ namespace WebSocketSharp
action (); action ();
} }
private static bool writeTo (this Stream stream, Stream destination, int length, byte[] buffer)
{
var bytes = stream.readBytes (buffer, 0, length);
var len = bytes.Length;
destination.Write (bytes, 0, len);
return len == length;
}
#endregion #endregion
#region Internal Methods #region Internal Methods
@ -630,14 +629,14 @@ namespace WebSocketSharp
var buff = new byte[bufferLength]; var buff = new byte[bufferLength];
var end = false; var end = false;
for (long i = 0; i < cnt; i++) { for (long i = 0; i < cnt; i++) {
if (!stream.readBytes (buff, 0, bufferLength, dest)) { if (!stream.writeTo (dest, bufferLength, buff)) {
end = true; end = true;
break; break;
} }
} }
if (!end && rem > 0) if (!end && rem > 0)
stream.readBytes (new byte[rem], 0, rem, dest); stream.writeTo (dest, rem, new byte[rem]);
dest.Close (); dest.Close ();
return dest.ToArray (); return dest.ToArray ();