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); }
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); }
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); }
public override ulong ReadUInt64() { if (mSwap) { return(EndiannessSwapUtility.Swap(base.ReadUInt64())); } else { return(base.ReadUInt64()); } }
public override float ReadSingle() { if (mSwap) { return(EndiannessSwapUtility.Swap(base.ReadSingle())); } else { return(base.ReadSingle()); } }
public override int ReadInt32() { if (mSwap) { return(EndiannessSwapUtility.Swap(base.ReadInt32())); } else { return(base.ReadInt32()); } }
public override double ReadDouble() { if (mSwap) { return(EndiannessSwapUtility.Swap(base.ReadDouble())); } else { return(base.ReadDouble()); } }
public override decimal ReadDecimal() { if (mSwap) { return(EndiannessSwapUtility.Swap(base.ReadDecimal())); } else { return(base.ReadDecimal()); } }
public override ushort ReadUInt16() { if (mSwap) { return(EndiannessSwapUtility.Swap(base.ReadUInt16())); } else { return(base.ReadUInt16()); } }
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); }
public override void Write(decimal value) { base.Write(mSwap ? EndiannessSwapUtility.Swap(value) : value); }