示例#1
0
        public void Write <T>(T[] value)
            where T : struct
        {
            // Allocate bytes for struct
            int typeSize = Marshal.SizeOf <T>();
            var bytes    = new byte[typeSize * value.Length];

            unsafe
            {
                fixed(byte *ptr = bytes)
                {
                    for (int i = 0; i < value.Length; i++)
                    {
                        // Marshal the structure into the allocated byte array
                        if (mSwap)
                        {
                            Marshal.StructureToPtr(EndiannessSwapUtility.Swap(value[i]), (IntPtr)(ptr + (i * typeSize)), true);
                        }
                        else
                        {
                            Marshal.StructureToPtr(value[i], (IntPtr)(ptr + (i * typeSize)), true);
                        }
                    }
                }
            }

            // Lastly write out the bytes
            Write(bytes);
        }
示例#2
0
        public T[] ReadStruct <T>(int count)
            where T : struct
        {
            T[] objects = new T[count];

            int typeSize = Marshal.SizeOf <T>();
            var bytes    = ReadBytes(typeSize * count);

            unsafe
            {
                fixed(byte *ptr = bytes)
                {
                    for (int i = 0; i < objects.Length; i++)
                    {
                        if (mSwap)
                        {
                            objects[i] = EndiannessSwapUtility.Swap(Marshal.PtrToStructure <T>((IntPtr)(ptr + (i * typeSize))));
                        }
                        else
                        {
                            objects[i] = Marshal.PtrToStructure <T>((IntPtr)(ptr + (i * typeSize)));
                        }
                    }
                }
            }

            return(objects);
        }
示例#3
0
        public void Write <T>(ref T value)
            where T : struct
        {
            // Allocate bytes for struct
            var bytes = new byte[Marshal.SizeOf <T>()];

            unsafe
            {
                fixed(byte *ptr = bytes)
                {
                    // Marshal the structure into the allocated byte array
                    if (mSwap)
                    {
                        Marshal.StructureToPtr(EndiannessSwapUtility.Swap(value), (IntPtr)ptr, true);
                    }
                    else
                    {
                        Marshal.StructureToPtr(value, (IntPtr)ptr, true);
                    }
                }
            }

            // Lastly write out the bytes
            Write(bytes);
        }
示例#4
0
 public override ulong ReadUInt64()
 {
     if (mSwap)
     {
         return(EndiannessSwapUtility.Swap(base.ReadUInt64()));
     }
     else
     {
         return(base.ReadUInt64());
     }
 }
示例#5
0
 public override float ReadSingle()
 {
     if (mSwap)
     {
         return(EndiannessSwapUtility.Swap(base.ReadSingle()));
     }
     else
     {
         return(base.ReadSingle());
     }
 }
示例#6
0
 public override int ReadInt32()
 {
     if (mSwap)
     {
         return(EndiannessSwapUtility.Swap(base.ReadInt32()));
     }
     else
     {
         return(base.ReadInt32());
     }
 }
示例#7
0
 public override double ReadDouble()
 {
     if (mSwap)
     {
         return(EndiannessSwapUtility.Swap(base.ReadDouble()));
     }
     else
     {
         return(base.ReadDouble());
     }
 }
示例#8
0
 public override decimal ReadDecimal()
 {
     if (mSwap)
     {
         return(EndiannessSwapUtility.Swap(base.ReadDecimal()));
     }
     else
     {
         return(base.ReadDecimal());
     }
 }
示例#9
0
 public override ushort ReadUInt16()
 {
     if (mSwap)
     {
         return(EndiannessSwapUtility.Swap(base.ReadUInt16()));
     }
     else
     {
         return(base.ReadUInt16());
     }
 }
示例#10
0
        public T ReadStruct <T>()
            where T : struct
        {
            T obj;

            var bytes = ReadBytes(Marshal.SizeOf <T>());

            unsafe
            {
                fixed(byte *ptr = bytes)
                {
                    obj = Marshal.PtrToStructure <T>((IntPtr)ptr);
                }
            }

            if (mSwap)
            {
                obj = EndiannessSwapUtility.Swap(obj);
            }

            return(obj);
        }
示例#11
0
 public override void Write(decimal value)
 {
     base.Write(mSwap ? EndiannessSwapUtility.Swap(value) : value);
 }