6 namespace SiliconStudio.Core.IO
13 private readonly byte* dataStart;
14 private readonly byte* dataEnd;
15 private byte* dataCurrent;
23 : this((byte*)data, length)
35 this.dataStart = data;
36 this.dataCurrent = data;
37 this.dataEnd = data + length;
46 public override long Seek(
long offset, SeekOrigin origin)
50 case SeekOrigin.Begin:
51 dataCurrent = dataStart + offset;
53 case SeekOrigin.Current:
54 dataCurrent += offset;
57 dataCurrent = dataEnd + offset;
60 throw new ArgumentOutOfRangeException(
"origin");
62 return dataCurrent - dataStart;
68 throw new InvalidOperationException(
"Length can't be changed.");
72 public override int Read(byte[] buffer,
int offset,
int count)
74 var bytesLeft = (int)(dataEnd - dataCurrent);
75 if (count > bytesLeft)
77 Utilities.Read((IntPtr)dataCurrent, buffer, offset, count);
83 public override void Write(byte[] buffer,
int offset,
int count)
85 var bytesLeft = (int)(dataEnd - dataCurrent);
86 if (count > bytesLeft)
87 throw new InvalidOperationException(
"Buffer too small");
89 Utilities.Write((IntPtr)dataCurrent, buffer, offset, count);
96 var bytesLeft = (int)(dataEnd - dataCurrent);
97 if (count > bytesLeft)
100 Utilities.CopyMemory(buffer, (IntPtr)dataCurrent, count);
101 dataCurrent +=
count;
108 var bytesLeft = (int)(dataEnd - dataCurrent);
109 if (count > bytesLeft)
110 throw new InvalidOperationException(
"Buffer too small");
112 Utilities.CopyMemory((IntPtr)dataCurrent, buffer, count);
113 dataCurrent +=
count;
119 if (dataCurrent == dataEnd)
122 return *dataCurrent++;
128 if (dataCurrent == dataEnd)
129 throw new InvalidOperationException(
"Buffer too small");
131 *dataCurrent++ = value;
137 if (dataCurrent +
sizeof(ushort) > dataEnd)
138 throw new InvalidOperationException(
"Buffer too small");
140 var result = *((ushort*)dataCurrent);
141 dataCurrent +=
sizeof(ushort);
147 if (dataCurrent +
sizeof(uint) > dataEnd)
148 throw new InvalidOperationException(
"Buffer too small");
150 var result = *((uint*)dataCurrent);
151 dataCurrent +=
sizeof(uint);
158 if (dataCurrent +
sizeof(ulong) > dataEnd)
159 throw new InvalidOperationException(
"Buffer too small");
161 var result = *((ulong*)dataCurrent);
162 dataCurrent +=
sizeof(ulong);
167 public override void Write(ushort i)
169 if (dataCurrent +
sizeof(ushort) > dataEnd)
170 throw new InvalidOperationException(
"Buffer too small");
172 *((ushort*)dataCurrent) = i;
173 dataCurrent +=
sizeof(ushort);
179 if (dataCurrent +
sizeof(uint) > dataEnd)
180 throw new InvalidOperationException(
"Buffer too small");
182 *((uint*)dataCurrent) = i;
183 dataCurrent +=
sizeof(uint);
189 if (dataCurrent +
sizeof(ulong) > dataEnd)
190 throw new InvalidOperationException(
"Buffer too small");
192 *((ulong*)dataCurrent) = i;
193 dataCurrent +=
sizeof(ulong);
197 public override bool CanRead
203 public override bool CanSeek
209 public override bool CanWrite
215 public override long Length
217 get {
return dataEnd - dataStart; }
221 public override long Position
223 get {
return dataCurrent - dataStart; }
224 set { dataCurrent = dataStart + value; }
override void Write(uint i)
override uint ReadUInt32()
override void Write(IntPtr buffer, int count)
Writes a block of bytes to this stream using data from a buffer. The buffer containing data to write ...
override void Write(ulong i)
override void Write(byte[] buffer, int offset, int count)
A MemoryStream over a native memory region.
override void WriteByte(byte value)
override ushort ReadUInt16()
A Stream with additional methods for native read and write operations using IntPtr.
NativeMemoryStream(byte *data, long length)
Initializes a new instance of the NativeMemoryStream class.
override long Seek(long offset, SeekOrigin origin)
override void Write(ushort i)
override ulong ReadUInt64()
override int Read(IntPtr buffer, int count)
Reads a block of bytes from the stream and writes the data in a given buffer. When this method return...
override void SetLength(long value)
override int Read(byte[] buffer, int offset, int count)
NativeMemoryStream(IntPtr data, long length)
Initializes a new instance of the NativeMemoryStream class.