| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Text; |
| | 4 | |
|
| | 5 | | namespace KernelCommunication |
| | 6 | | { |
| | 7 | | // Write BigEndian binary to stream |
| | 8 | | public class BinaryWriter : IDisposable |
| | 9 | | { |
| | 10 | | private Stream stream; |
| | 11 | |
|
| 0 | 12 | | public BinaryWriter(Stream stream) |
| | 13 | | { |
| 0 | 14 | | this.stream = stream; |
| 0 | 15 | | } |
| | 16 | |
|
| | 17 | | public unsafe void WriteInt32(int value) |
| | 18 | | { |
| 53 | 19 | | byte* ptr = (byte*)&value; |
| 53 | 20 | | stream.WriteByte(ptr[3]); |
| 53 | 21 | | stream.WriteByte(ptr[2]); |
| 53 | 22 | | stream.WriteByte(ptr[1]); |
| 53 | 23 | | stream.WriteByte(ptr[0]); |
| 53 | 24 | | } |
| | 25 | |
|
| | 26 | | public unsafe void WriteSingle(float value) |
| | 27 | | { |
| 20 | 28 | | byte* ptr = (byte*)&value; |
| 20 | 29 | | stream.WriteByte(ptr[3]); |
| 20 | 30 | | stream.WriteByte(ptr[2]); |
| 20 | 31 | | stream.WriteByte(ptr[1]); |
| 20 | 32 | | stream.WriteByte(ptr[0]); |
| 20 | 33 | | } |
| | 34 | |
|
| | 35 | | public unsafe void WriteInt64(long value) |
| | 36 | | { |
| 11 | 37 | | byte* ptr = (byte*)&value; |
| 11 | 38 | | stream.WriteByte(ptr[7]); |
| 11 | 39 | | stream.WriteByte(ptr[6]); |
| 11 | 40 | | stream.WriteByte(ptr[5]); |
| 11 | 41 | | stream.WriteByte(ptr[4]); |
| 11 | 42 | | stream.WriteByte(ptr[3]); |
| 11 | 43 | | stream.WriteByte(ptr[2]); |
| 11 | 44 | | stream.WriteByte(ptr[1]); |
| 11 | 45 | | stream.WriteByte(ptr[0]); |
| 11 | 46 | | } |
| | 47 | |
|
| | 48 | | public void WriteBytes(byte[] value) |
| | 49 | | { |
| 8 | 50 | | stream.Write(value, 0, value.Length); |
| 8 | 51 | | } |
| | 52 | |
|
| | 53 | | public void WriteString(string value) |
| | 54 | | { |
| 0 | 55 | | var bytes = Encoding.UTF8.GetBytes(value); |
| 0 | 56 | | stream.Write(bytes, 0, bytes.Length); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Dispose() |
| | 60 | | { |
| 1 | 61 | | stream = null; |
| 1 | 62 | | } |
| | 63 | | } |
| | 64 | | } |