Ejemplo n.º 1
0
 /// <summary>
 ///   Gets a sequence of elements from a position in the buffer into a target buffer.
 /// </summary>
 /// <param name="positionInBytes">Relative position in bytes from the beginning of the buffer to get the data from.</param>
 /// <param name = "buffer">An array of values to be read from the buffer.</param>
 /// <param name = "offset">The zero-based byte offset in buffer at which to begin storing
 ///   the data read from the current buffer.</param>
 /// <param name = "count">The number of values to be read from the current buffer.</param>
 public void GetRange <T>(int positionInBytes, T[] buffer, int offset, int count) where T : struct
 {
     unsafe
     {
         SdxUtilities.Read((IntPtr)(_buffer + positionInBytes), buffer, offset, count);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 ///   Gets an array of values from a position in the buffer.
 /// </summary>
 /// <param name="positionInBytes">Relative position in bytes from the beginning of the buffer to get the data from.</param>
 /// <param name="count">number of T instance to get from the positionInBytes</param>
 /// <typeparam name = "T">The type of the values to be read from the buffer.</typeparam>
 /// <returns>An array of values that was read from the current buffer.</returns>
 public T[] GetRange <T>(int positionInBytes, int count) where T : struct
 {
     unsafe
     {
         var result = new T[count];
         SdxUtilities.Read((IntPtr)(_buffer + positionInBytes), result, 0, count);
         return(result);
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 ///   Gets a single value from the current buffer at the specified position.
 /// </summary>
 /// <param name="positionInBytes">Relative position in bytes from the beginning of the buffer to get the data from.</param>
 /// <typeparam name = "T">The type of the value to be read from the buffer.</typeparam>
 /// <returns>The value that was read.</returns>
 public T Get <T>(int positionInBytes) where T : struct
 {
     unsafe
     {
         T result = default(T);
         SdxUtilities.Read((IntPtr)(_buffer + positionInBytes), ref result);
         return(result);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        ///   Reads a sequence of elements from the current stream into a target buffer and
        ///   advances the position within the stream by the number of bytes read.
        /// </summary>
        /// <remarks>
        /// In order to provide faster read/write, this operation doesn't check stream bound.
        /// A client must carefully not read/write above the size of this datastream.
        /// </remarks>
        /// <param name = "buffer">An array of values to be read from the stream.</param>
        /// <param name = "offset">The zero-based byte offset in buffer at which to begin storing
        ///   the data read from the current stream.</param>
        /// <param name = "count">The number of values to be read from the current stream.</param>
        /// <returns>The number of bytes read from the stream.</returns>
        /// <exception cref = "T:System.NotSupportedException">This stream does not support reading.</exception>
        public int ReadRange <T>(T[] buffer, int offset, int count) where T : struct
        {
            unsafe
            {
                if (!_canRead)
                {
                    throw new NotSupportedException();
                }

                var oldPosition = _position;
                _position = (byte *)SdxUtilities.Read((IntPtr)(_buffer + _position), buffer, offset, count) - _buffer;
                return((int)(_position - oldPosition));
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///   Reads an array of values from the current stream, and advances the current position
        ///   within this stream by the number of bytes written.
        /// </summary>
        /// <remarks>
        /// In order to provide faster read/write, this operation doesn't check stream bound.
        /// A client must carefully not read/write above the size of this datastream.
        /// </remarks>
        /// <typeparam name = "T">The type of the values to be read from the stream.</typeparam>
        /// <returns>An array of values that was read from the current stream.</returns>
        public T[] ReadRange <T>(int count) where T : struct
        {
            unsafe
            {
                if (!_canRead)
                {
                    throw new NotSupportedException();
                }

                byte *from   = _buffer + _position;
                var   result = new T[count];
                _position = (byte *)SdxUtilities.Read((IntPtr)from, result, 0, count) - _buffer;
                return(result);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts this instance to a read only byte buffer.
        /// </summary>
        /// <returns>A readonly byte buffer.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// DataPointer is Zero
        /// or
        /// Size cannot be &lt; 0
        /// </exception>
        public byte[] ToArray()
        {
            if (Pointer == IntPtr.Zero)
            {
                throw new InvalidOperationException("DataPointer is Zero");
            }
            if (Size < 0)
            {
                throw new InvalidOperationException("Size cannot be < 0");
            }
            var buffer = new byte[Size];

            SdxUtilities.Read(Pointer, buffer, 0, Size);
            return(buffer);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Reads the content of the unmanaged memory location of this instance to the specified buffer.
 /// </summary>
 /// <typeparam name="T">Type of a buffer element</typeparam>
 /// <param name="buffer">The buffer.</param>
 /// <param name="offset">The offset in the array to write to.</param>
 /// <param name="count">The number of T element to read from the memory location.</param>
 /// <exception cref="System.ArgumentNullException">buffer</exception>
 /// <exception cref="System.InvalidOperationException">DataPointer is Zero</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">buffer;Total buffer size cannot be larger than size of this data pointer</exception>
 public void CopyTo <T>(T[] buffer, int offset, int count) where T : struct
 {
     if (buffer == null)
     {
         throw new ArgumentNullException("buffer");
     }
     if (Pointer == IntPtr.Zero)
     {
         throw new InvalidOperationException("DataPointer is Zero");
     }
     if (offset < 0)
     {
         throw new ArgumentOutOfRangeException("offset", "Must be >= 0");
     }
     if (count <= 0)
     {
         throw new ArgumentOutOfRangeException("count", "Must be > 0");
     }
     if (count * SdxUtilities.SizeOf <T>() > Size)
     {
         throw new ArgumentOutOfRangeException("buffer", "Total buffer size cannot be larger than size of this data pointer");
     }
     SdxUtilities.Read(Pointer, buffer, offset, count);
 }