1 // =================================
 2 // Copyright (c) 2021 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Collections;
 8 
 9 namespace System.IO
10 {
11     public class MemoryByteStream : ByteStream
12     {
13         public MemoryByteStream() : content()readPos(0)
14         {
15         }
16         public MemoryByteStream(byte* datalong count) : content()readPos(0)
17         {
18             this->Write(datacount);
19         }
20         suppress MemoryByteStream(const MemoryByteStream&);
21         suppress void operator=(const MemoryByteStream&);
22         public default nothrow MemoryByteStream(MemoryByteStream&&);
23         public default nothrow void operator=(MemoryByteStream&&);
24         public inline nothrow const byte* Data() const
25         {
26             return content.CBegin().Ptr();
27         }
28         public inline nothrow long Count() const
29         {
30             return content.Count();
31         }
32         public inline nothrow long ReadPos() const
33         {
34             return readPos;
35         }
36         public override int ReadByte()
37         {
38             if (readPos < content.Count())
39             {
40                 return content[readPos++];
41             }
42             return -1;
43         }
44         public override long Read(byte* buflong count)
45         {
46             long bytesRead = 0;
47             long n = content.Count();
48             while (count > 0 && readPos < n)
49             {
50                 *buf++ = content[readPos++];
51                 ++bytesRead;
52                 --count;
53             }
54             return bytesRead;
55         }
56         public override void Write(byte x)
57         {
58             content.Add(x);
59         }
60         public override void Write(byte* buflong count)
61         {
62             while (count > 0)
63             {
64                 content.Add(*buf++);
65                 --count;
66             }
67         }
68         public override void Seek(long posOrigin origin)
69         {
70             switch (origin)
71             {
72                 case Origin.seekSet: readPos = pos; break;
73                 case Origin.seekCur: readPos = readPos + pos; break;
74                 case Origin.seekEnd: readPos = content.Count() + pos; break;
75             }
76         }
77         public override long Tell()
78         {
79             return readPos;
80         }
81         private List<byte> content;
82         private long readPos;
83     }
84 }