Пример #1
0
        /// <summary>
        /// Write a <see cref="ReadOnlySpan{T}"/> array data in the <paramref name="stream"/>.
        /// </summary>
        /// <typeparam name="T">The type of items in the <paramref name="array"/>.</typeparam>
        /// <param name="stream">The stream to write.</param>
        /// <param name="array">The span to be written.</param>
        public static void WriteSpan <T>(this Stream stream, ReadOnlySpan <T> array) where T : unmanaged
        {
            if (array == null || array.Length == 0)
            {
                return;
            }

            byte[] buffer = new byte[array.Length * MemUtil.SizeOf <T>()];
            MemUtil.Copy <byte, T>(buffer, array);
            stream.Write(buffer, 0, buffer.Length);
        }
Пример #2
0
 public static void Set <T>(ref T[] array, ref int length, ReadOnlySpan <T> value) where T : unmanaged
 {
     if (array == null || !(value.Length <= array.Length))
     {
         if (array != null && array.Length > 0)
         {
             ArrayPool <T> .Return(array);
         }
         array = ArrayPool <T> .GetT(value.Length);
     }
     length = value.Length;
     MemUtil.Copy(array, value);
 }
Пример #3
0
        public GlfwMouseState(GlfwMouseState original) : this()
        {
            MemUtil.Copy(_mb.Ptr, original._mb.Ptr, _mb.TotalSize);

            PreviousPosition = original.PreviousPosition;
            Position         = original.Position;

            PreviousScroll = original.PreviousScroll;
            Scroll         = original.Scroll;

            IsAnyButtonDown   = original.IsAnyButtonDown;
            IsAnyButtonRepeat = original.IsAnyButtonRepeat;
            IsAnyButtonUp     = original.IsAnyButtonUp;
        }
Пример #4
0
        /// <summary>
        /// Read a <see cref="ReadOnlySpan{T}"/> from the <paramref name="stream"/>.
        /// </summary>
        /// <typeparam name="T">The type of items in the returned <see cref="ReadOnlySpan{T}"/>.</typeparam>
        /// <param name="stream">The stream to read.</param>
        /// <param name="count">The count of T items to read.</param>
        /// <returns>A <see cref="ReadOnlySpan{T}"/> with items read from the <paramref name="stream"/>.</returns>
        public static ReadOnlySpan <T> ReadSpan <T>(this Stream stream, uint count) where T : unmanaged
        {
            if (count == 0)
            {
                return(Array.Empty <T>());
            }

            T[] array = new T[count];

            byte[] buffer = new byte[count * MemUtil.SizeOf <T>()];
            stream.Read(buffer, 0, buffer.Length);

            MemUtil.Copy <T, byte>(array, buffer);

            return(array);
        }
Пример #5
0
        /// <summary>
        /// Writes a block of bytes to the current stream using data read from a buffer.
        /// </summary>
        /// <param name="buffer">The buffer to write data from.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at
        /// which to begin copying bytes to the current stream.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        /// <exception cref="ObjectDisposedException">The current stream is closed.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is
        /// <see langword="null"/></exception>
        /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/>
        /// is greater than the buffer length.</exception>
        public override void Write(byte[] buffer, int offset, int count)
        {
            ValidateBufferArguments(buffer, offset, count);
            EnsureNotClosed();

            var size = _position + count;

            if (size > _length)
            {
                EnsureCapacity(size);
                _length = size;
            }

            MemUtil.Copy(MemUtil.Add(_memoryBlock.Ptr, _position), new ReadOnlySpan <byte>(buffer, offset, count));

            _position = size;
        }
Пример #6
0
        /// <summary>
        /// Reads a block of bytes from the current stream and writes the data to a buffer.
        /// </summary>
        /// <param name="buffer">When this method returns, contains the specified byte array
        /// with the values between <paramref name="offset"/> and (<paramref name="offset"/> +
        /// <paramref name="count"/> - 1) replaced by the characters read from the current
        /// stream.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at
        /// which to begin storing data from the current stream.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <returns>The total number of bytes written into the buffer. This can be less than
        /// the number of bytes requested if that number of bytes are not currently available,
        /// or zero if the end of the stream is reached before any bytes are read.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is
        /// <see langword="null"/></exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or
        /// <paramref name="count"/> is negative.</exception>
        /// <exception cref="ArgumentException"><paramref name="offset"/> subtracted from the
        /// buffer length is less than <paramref name="count"/>.</exception>
        /// <exception cref="ObjectDisposedException">The current stream is closed.</exception>
        public override int Read(byte[] buffer, int offset, int count)
        {
            ValidateBufferArguments(buffer, offset, count);
            EnsureNotClosed();

            int toRead = (int)Math.Min(_length - _position, count);

            if (toRead <= 0)
            {
                return(0);
            }


            MemUtil.Copy <byte>(buffer, MemUtil.Add(_memoryBlock.Ptr, _position));

            _position += toRead;

            return(toRead);
        }
Пример #7
0
 internal GlfwKeyboardState(GlfwKeyboardState keyboardState) : this()
 {
     MemUtil.Copy(_mb.Ptr, keyboardState._mb.Ptr, _mb.TotalSize);
 }