1 using System;
2
3 namespace System.IO
4 {
5 public class MemoryWriter
6 {
7 public nothrow MemoryWriter(byte* ptr_, long count_) : ptr(ptr_), pos(ptr), count(count_)
8 {
9 }
10 public void Write(byte x)
11 {
12 if (pos - ptr >= count)
13 {
14 throw Exception("memory writer: end of space");
15 }
16 *pos++ = x;
17 }
18 public void Write(sbyte x)
19 {
20 Write(cast<byte>(x));
21 }
22 public void Write(ushort x)
23 {
24 byte b0 = cast<byte>(x >> 8u);
25 byte b1 = cast<byte>(x);
26 Write(b0);
27 Write(b1);
28 }
29 public void Write(short x)
30 {
31 Write(cast<ushort>(x));
32 }
33 public void Write(uint x)
34 {
35 byte b0 = cast<byte>(x >> 24u);
36 byte b1 = cast<byte>(x >> 16u);
37 byte b2 = cast<byte>(x >> 8u);
38 byte b3 = cast<byte>(x);
39 Write(b0);
40 Write(b1);
41 Write(b2);
42 Write(b3);
43 }
44 public void Write(int x)
45 {
46 Write(cast<uint>(x));
47 }
48 public void Write(ulong x)
49 {
50 byte b0 = cast<byte>(x >> 56u);
51 byte b1 = cast<byte>(x >> 48u);
52 byte b2 = cast<byte>(x >> 40u);
53 byte b3 = cast<byte>(x >> 32u);
54 byte b4 = cast<byte>(x >> 24u);
55 byte b5 = cast<byte>(x >> 16u);
56 byte b6 = cast<byte>(x >> 8u);
57 byte b7 = cast<byte>(x);
58 Write(b0);
59 Write(b1);
60 Write(b2);
61 Write(b3);
62 Write(b4);
63 Write(b5);
64 Write(b6);
65 Write(b7);
66 }
67 public void Write(long x)
68 {
69 Write(cast<ulong>(x));
70 }
71 public void Write(const DateTime& dt)
72 {
73 Date date = dt.GetDate();
74 Write(date.Year());
75 Write(cast<sbyte>(date.GetMonth()));
76 Write(date.Day());
77 Write(dt.Seconds());
78 }
79 public void Write(const string& s)
80 {
81 for (char c : s)
82 {
83 Write(cast<byte>(c));
84 }
85 Write(cast<byte>(0u));
86 }
87 public long Offset() const
88 {
89 return pos - ptr;
90 }
91 private byte* ptr;
92 private byte* pos;
93 private long count;
94 }
95 }