short ReadStreamShort(this Stream stream, int offset, bool little) { #if SAFE_CAST byte[] intBytes = new byte[2]; stream.Position = offset; stream.Read(intBytes, 0, intBytes.Length); if (BitConverter.IsLittleEndian != little) { Reverse(intBytes, 0, intBytes.Length); } fixed(byte *bs = intBytes) { short *c = (short *)bs; return(*c); } #else if (BitConverter.IsLittleEndian == little) { ShortField fs = new ShortField(stream, offset); return(fs.ShortVal); } else { RShortField fs = new RShortField(stream, offset); return(fs.ShortVal); } #endif }
void WriteStreamShort(this Stream stream, int offset, short value, bool little) { #if SAFE_CAST byte[] intBytes = new byte[2]; fixed(byte *bs = intBytes) { short *c = (short *)bs; *c = value; if (BitConverter.IsLittleEndian != little) { Reverse2(c); } } stream.Position = offset; stream.Write(intBytes, 0, intBytes.Length); #else if (BitConverter.IsLittleEndian == little) { ShortField fs = new ShortField(value); fs.Fill(stream, offset); } else { RShortField fs = new RShortField(value); fs.Fill(stream, offset); } #endif }
void WriteShort(this byte[] arr, int offset, short value, bool little) { #if SAFE_CAST fixed(byte *bs = arr) { short *target = (short *)(bs + offset); (*target) = value; } if (BitConverter.IsLittleEndian != little) { Reverse(arr, offset, 2); } #else if (BitConverter.IsLittleEndian == little) { ShortField fs = new ShortField(value); fs.Fill(arr, offset); } else { RShortField fs = new RShortField(value); fs.Fill(arr, offset); } #endif }
short ReadShort(this byte[] arr, int offset, bool little) { #if SAFE_CAST #if PROFILER Profiler.BeginSample("ReadShort"); #endif short val; fixed(byte *bytes = arr) { byte *databytes = bytes + offset; val = *((short *)databytes); } if (BitConverter.IsLittleEndian != little) { Reverse2(&val); } #if PROFILER Profiler.EndSample(); #endif return(val); #else if (BitConverter.IsLittleEndian == little) { ShortField fs = new ShortField(arr, offset); return(fs.ShortVal); } else { RShortField fs = new RShortField(arr, offset); return(fs.ShortVal); } #endif }