websocket-server – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using System.Linq; |
||
5 | using System.Text; |
||
6 | |||
7 | namespace WebSockets.Common |
||
8 | { |
||
9 | internal class BinaryReaderWriter |
||
10 | { |
||
11 | public static byte[] ReadExactly(int length, Stream stream) |
||
12 | { |
||
13 | byte[] buffer = new byte[length]; |
||
14 | if (length == 0) |
||
15 | { |
||
16 | return buffer; |
||
17 | } |
||
18 | |||
19 | int offset = 0; |
||
20 | do |
||
21 | { |
||
22 | int bytesRead = stream.Read(buffer, offset, length - offset); |
||
23 | if (bytesRead == 0) |
||
24 | { |
||
25 | throw new EndOfStreamException(string.Format("Unexpected end of stream encountered whilst attempting to read {0:#,##0} bytes", length)); |
||
26 | } |
||
27 | |||
28 | offset += bytesRead; |
||
29 | } while (offset < length); |
||
30 | |||
31 | return buffer; |
||
32 | } |
||
33 | |||
34 | public static ushort ReadUShortExactly(Stream stream, bool isLittleEndian) |
||
35 | { |
||
36 | byte[] lenBuffer = BinaryReaderWriter.ReadExactly(2, stream); |
||
37 | |||
38 | if (!isLittleEndian) |
||
39 | { |
||
40 | Array.Reverse(lenBuffer); // big endian |
||
41 | } |
||
42 | |||
43 | return BitConverter.ToUInt16(lenBuffer, 0); |
||
44 | } |
||
45 | |||
46 | public static ulong ReadULongExactly(Stream stream, bool isLittleEndian) |
||
47 | { |
||
48 | byte[] lenBuffer = BinaryReaderWriter.ReadExactly(8, stream); |
||
49 | |||
50 | if (!isLittleEndian) |
||
51 | { |
||
52 | Array.Reverse(lenBuffer); // big endian |
||
53 | } |
||
54 | |||
55 | return BitConverter.ToUInt64(lenBuffer, 0); |
||
56 | } |
||
57 | |||
58 | public static long ReadLongExactly(Stream stream, bool isLittleEndian) |
||
59 | { |
||
60 | byte[] lenBuffer = BinaryReaderWriter.ReadExactly(8, stream); |
||
61 | |||
62 | if (!isLittleEndian) |
||
63 | { |
||
64 | Array.Reverse(lenBuffer); // big endian |
||
65 | } |
||
66 | |||
67 | return BitConverter.ToInt64(lenBuffer, 0); |
||
68 | } |
||
69 | |||
70 | public static void WriteULong(ulong value, Stream stream, bool isLittleEndian) |
||
71 | { |
||
72 | byte[] buffer = BitConverter.GetBytes(value); |
||
73 | if (BitConverter.IsLittleEndian && ! isLittleEndian) |
||
74 | { |
||
75 | Array.Reverse(buffer); |
||
76 | } |
||
77 | |||
78 | stream.Write(buffer, 0, buffer.Length); |
||
79 | } |
||
80 | |||
81 | public static void WriteLong(long value, Stream stream, bool isLittleEndian) |
||
82 | { |
||
83 | byte[] buffer = BitConverter.GetBytes(value); |
||
84 | if (BitConverter.IsLittleEndian && !isLittleEndian) |
||
85 | { |
||
86 | Array.Reverse(buffer); |
||
87 | } |
||
88 | |||
89 | stream.Write(buffer, 0, buffer.Length); |
||
90 | } |
||
91 | |||
92 | public static void WriteUShort(ushort value, Stream stream, bool isLittleEndian) |
||
93 | { |
||
94 | byte[] buffer = BitConverter.GetBytes(value); |
||
95 | if (BitConverter.IsLittleEndian && !isLittleEndian) |
||
96 | { |
||
97 | Array.Reverse(buffer); |
||
98 | } |
||
99 | |||
100 | stream.Write(buffer, 0, buffer.Length); |
||
101 | } |
||
102 | } |
||
103 | } |