Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NativeMemoryStream.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.IO;
5 
6 namespace SiliconStudio.Core.IO
7 {
8  /// <summary>
9  /// A <see cref="MemoryStream"/> over a native memory region.
10  /// </summary>
11  public unsafe class NativeMemoryStream : NativeStream
12  {
13  private readonly byte* dataStart;
14  private readonly byte* dataEnd;
15  private byte* dataCurrent;
16 
17  /// <summary>
18  /// Initializes a new instance of the <see cref="NativeMemoryStream"/> class.
19  /// </summary>
20  /// <param name="data">The native data pointer.</param>
21  /// <param name="length">The data length.</param>
22  public NativeMemoryStream(IntPtr data, long length)
23  : this((byte*)data, length)
24  {
25  }
26 
27 
28  /// <summary>
29  /// Initializes a new instance of the <see cref="NativeMemoryStream"/> class.
30  /// </summary>
31  /// <param name="data">The native data pointer.</param>
32  /// <param name="length">The data length.</param>
33  public NativeMemoryStream(byte* data, long length)
34  {
35  this.dataStart = data;
36  this.dataCurrent = data;
37  this.dataEnd = data + length;
38  }
39 
40  /// <inheritdoc/>
41  public override void Flush()
42  {
43  }
44 
45  /// <inheritdoc/>
46  public override long Seek(long offset, SeekOrigin origin)
47  {
48  switch (origin)
49  {
50  case SeekOrigin.Begin:
51  dataCurrent = dataStart + offset;
52  break;
53  case SeekOrigin.Current:
54  dataCurrent += offset;
55  break;
56  case SeekOrigin.End:
57  dataCurrent = dataEnd + offset;
58  break;
59  default:
60  throw new ArgumentOutOfRangeException("origin");
61  }
62  return dataCurrent - dataStart;
63  }
64 
65  /// <inheritdoc/>
66  public override void SetLength(long value)
67  {
68  throw new InvalidOperationException("Length can't be changed.");
69  }
70 
71  /// <inheritdoc/>
72  public override int Read(byte[] buffer, int offset, int count)
73  {
74  var bytesLeft = (int)(dataEnd - dataCurrent);
75  if (count > bytesLeft)
76  count = bytesLeft;
77  Utilities.Read((IntPtr)dataCurrent, buffer, offset, count);
78  dataCurrent += count;
79  return count;
80  }
81 
82  /// <inheritdoc/>
83  public override void Write(byte[] buffer, int offset, int count)
84  {
85  var bytesLeft = (int)(dataEnd - dataCurrent);
86  if (count > bytesLeft)
87  throw new InvalidOperationException("Buffer too small");
88 
89  Utilities.Write((IntPtr)dataCurrent, buffer, offset, count);
90  dataCurrent += count;
91  }
92 
93  /// <inheritdoc/>
94  public override int Read(IntPtr buffer, int count)
95  {
96  var bytesLeft = (int)(dataEnd - dataCurrent);
97  if (count > bytesLeft)
98  count = bytesLeft;
99 
100  Utilities.CopyMemory(buffer, (IntPtr)dataCurrent, count);
101  dataCurrent += count;
102  return count;
103  }
104 
105  /// <inheritdoc/>
106  public override void Write(IntPtr buffer, int count)
107  {
108  var bytesLeft = (int)(dataEnd - dataCurrent);
109  if (count > bytesLeft)
110  throw new InvalidOperationException("Buffer too small");
111 
112  Utilities.CopyMemory((IntPtr)dataCurrent, buffer, count);
113  dataCurrent += count;
114  }
115 
116  /// <inheritdoc/>
117  public override int ReadByte()
118  {
119  if (dataCurrent == dataEnd)
120  return -1;
121 
122  return *dataCurrent++;
123  }
124 
125  /// <inheritdoc/>
126  public override void WriteByte(byte value)
127  {
128  if (dataCurrent == dataEnd)
129  throw new InvalidOperationException("Buffer too small");
130 
131  *dataCurrent++ = value;
132  }
133 
134  /// <inheritdoc/>
135  public override ushort ReadUInt16()
136  {
137  if (dataCurrent + sizeof(ushort) > dataEnd)
138  throw new InvalidOperationException("Buffer too small");
139 
140  var result = *((ushort*)dataCurrent);
141  dataCurrent += sizeof(ushort);
142  return result;
143  }
144 
145  public override uint ReadUInt32()
146  {
147  if (dataCurrent + sizeof(uint) > dataEnd)
148  throw new InvalidOperationException("Buffer too small");
149 
150  var result = *((uint*)dataCurrent);
151  dataCurrent += sizeof(uint);
152  return result;
153  }
154 
155  /// <inheritdoc/>
156  public override ulong ReadUInt64()
157  {
158  if (dataCurrent + sizeof(ulong) > dataEnd)
159  throw new InvalidOperationException("Buffer too small");
160 
161  var result = *((ulong*)dataCurrent);
162  dataCurrent += sizeof(ulong);
163  return result;
164  }
165 
166  /// <inheritdoc/>
167  public override void Write(ushort i)
168  {
169  if (dataCurrent + sizeof(ushort) > dataEnd)
170  throw new InvalidOperationException("Buffer too small");
171 
172  *((ushort*)dataCurrent) = i;
173  dataCurrent += sizeof(ushort);
174  }
175 
176  /// <inheritdoc/>
177  public override void Write(uint i)
178  {
179  if (dataCurrent + sizeof(uint) > dataEnd)
180  throw new InvalidOperationException("Buffer too small");
181 
182  *((uint*)dataCurrent) = i;
183  dataCurrent += sizeof(uint);
184  }
185 
186  /// <inheritdoc/>
187  public override void Write(ulong i)
188  {
189  if (dataCurrent + sizeof(ulong) > dataEnd)
190  throw new InvalidOperationException("Buffer too small");
191 
192  *((ulong*)dataCurrent) = i;
193  dataCurrent += sizeof(ulong);
194  }
195 
196  /// <inheritdoc/>
197  public override bool CanRead
198  {
199  get { return true; }
200  }
201 
202  /// <inheritdoc/>
203  public override bool CanSeek
204  {
205  get { return true; }
206  }
207 
208  /// <inheritdoc/>
209  public override bool CanWrite
210  {
211  get { return true; }
212  }
213 
214  /// <inheritdoc/>
215  public override long Length
216  {
217  get { return dataEnd - dataStart; }
218  }
219 
220  /// <inheritdoc/>
221  public override long Position
222  {
223  get { return dataCurrent - dataStart; }
224  set { dataCurrent = dataStart + value; }
225  }
226  }
227 }
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 ...
_In_ size_t count
Definition: DirectXTexP.h:174
override void Write(byte[] buffer, int offset, int count)
A MemoryStream over a native memory region.
A Stream with additional methods for native read and write operations using IntPtr.
Definition: NativeStream.cs:11
NativeMemoryStream(byte *data, long length)
Initializes a new instance of the NativeMemoryStream class.
override long Seek(long offset, SeekOrigin origin)
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 int Read(byte[] buffer, int offset, int count)
NativeMemoryStream(IntPtr data, long length)
Initializes a new instance of the NativeMemoryStream class.