Пример #1
0
 /// <summary>Writes the specified array into the stream.</summary>
 /// <typeparam name="T">The type of the array item.</typeparam>
 /// <param name="items">The items.</param>
 public void Write <T>(T[] items)
 {
     if (items == null)
     {
         return;
     }
     if (items.GetType().GetElementType() == typeof(string))
     {
         var hMem  = SafeHGlobalHandle.CreateFromStringList(items as string[], StringListPackMethod.Packed, CharSet, 0);
         var bytes = hMem.ToArray <byte>(hMem.Size);
         Write(bytes, 0, bytes.Length);
     }
     else
     {
         var stSize = Marshal.SizeOf(typeof(T));
         if (stSize * items.Length + Position > Capacity)
         {
             throw new ArgumentException();
         }
         for (var i = 0; i < items.Length; i++)
         {
             Marshal.StructureToPtr(items[i], PositionPtr.Offset(i * stSize), false);
         }
         Position += stSize * items.Length;
     }
 }
Пример #2
0
        /// <summary>Reads a blittable type from the current stream and advances the position within the stream by the number of bytes read.</summary>
        /// <typeparam name="T">The type of the object to read.</typeparam>
        /// <returns>An object of type <typeparamref name="T"/>.</returns>
        /// <exception cref="ArgumentException">Type to be read must be blittable. - T</exception>
        /// <exception cref="ArgumentOutOfRangeException" />
        public T Read <T>()
        {
            //if (!typeof(T).IsBlittable()) throw new ArgumentException(@"Type to be read must be blittable.", nameof(T));
            var sz = Marshal.SizeOf(typeof(T));

            if (Position + sz > Capacity)
            {
                throw new ArgumentOutOfRangeException();
            }
            var ret = PositionPtr.ToStructure <T>();

            Position += sz;
            return(ret);
        }