Browse Source

Remove BitConverter from EndianBinaryReader

Also eliminate unnessary offset arithmetic.
af/merge-core
Jason Nelson 8 years ago
parent
commit
db77dc6536
  1. 68
      src/ImageSharp/IO/EndianBinaryReader.cs

68
src/ImageSharp/IO/EndianBinaryReader.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers.Binary;
using System.IO; using System.IO;
using System.Text; using System.Text;
@ -41,7 +42,7 @@ namespace SixLabors.ImageSharp.IO
/// <summary> /// <summary>
/// The endianness used to read data /// The endianness used to read data
/// </summary> /// </summary>
private Endianness endianness; private readonly Endianness endianness;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="EndianBinaryReader"/> class. /// Initializes a new instance of the <see cref="EndianBinaryReader"/> class.
@ -74,7 +75,6 @@ namespace SixLabors.ImageSharp.IO
Guard.IsTrue(stream.CanRead, nameof(stream), "Stream isn't readable"); Guard.IsTrue(stream.CanRead, nameof(stream), "Stream isn't readable");
this.BaseStream = stream; this.BaseStream = stream;
this.BitConverter = EndianBitConverter.GetConverter(endianness);
this.Encoding = encoding; this.Encoding = encoding;
this.decoder = encoding.GetDecoder(); this.decoder = encoding.GetDecoder();
this.endianness = endianness; this.endianness = endianness;
@ -96,11 +96,6 @@ namespace SixLabors.ImageSharp.IO
/// </summary> /// </summary>
public Stream BaseStream { get; } public Stream BaseStream { get; }
/// <summary>
/// Gets the bit converter used to read values from the stream.
/// </summary>
internal EndianBitConverter BitConverter { get; }
/// <summary> /// <summary>
/// Closes the reader, including the underlying stream. /// Closes the reader, including the underlying stream.
/// </summary> /// </summary>
@ -149,7 +144,6 @@ namespace SixLabors.ImageSharp.IO
this.ReadInternal(this.storageBuffer, 1); this.ReadInternal(this.storageBuffer, 1);
return this.storageBuffer[0] != 0; return this.storageBuffer[0] != 0;
} }
/// <summary> /// <summary>
@ -160,7 +154,10 @@ namespace SixLabors.ImageSharp.IO
public short ReadInt16() public short ReadInt16()
{ {
this.ReadInternal(this.storageBuffer, 2); this.ReadInternal(this.storageBuffer, 2);
return this.BitConverter.ToInt16(this.storageBuffer, 0);
return (this.endianness == Endianness.BigEndian)
? BinaryPrimitives.ReadInt16BigEndian(this.storageBuffer)
: BinaryPrimitives.ReadInt16LittleEndian(this.storageBuffer);
} }
/// <summary> /// <summary>
@ -171,7 +168,10 @@ namespace SixLabors.ImageSharp.IO
public int ReadInt32() public int ReadInt32()
{ {
this.ReadInternal(this.storageBuffer, 4); this.ReadInternal(this.storageBuffer, 4);
return this.BitConverter.ToInt32(this.storageBuffer, 0);
return (this.endianness == Endianness.BigEndian)
? BinaryPrimitives.ReadInt32BigEndian(this.storageBuffer)
: BinaryPrimitives.ReadInt32LittleEndian(this.storageBuffer);
} }
/// <summary> /// <summary>
@ -182,7 +182,10 @@ namespace SixLabors.ImageSharp.IO
public long ReadInt64() public long ReadInt64()
{ {
this.ReadInternal(this.storageBuffer, 8); this.ReadInternal(this.storageBuffer, 8);
return this.BitConverter.ToInt64(this.storageBuffer, 0);
return (this.endianness == Endianness.BigEndian)
? BinaryPrimitives.ReadInt64BigEndian(this.storageBuffer)
: BinaryPrimitives.ReadInt64LittleEndian(this.storageBuffer);
} }
/// <summary> /// <summary>
@ -193,7 +196,10 @@ namespace SixLabors.ImageSharp.IO
public ushort ReadUInt16() public ushort ReadUInt16()
{ {
this.ReadInternal(this.storageBuffer, 2); this.ReadInternal(this.storageBuffer, 2);
return this.BitConverter.ToUInt16(this.storageBuffer, 0);
return (this.endianness == Endianness.BigEndian)
? BinaryPrimitives.ReadUInt16BigEndian(this.storageBuffer)
: BinaryPrimitives.ReadUInt16LittleEndian(this.storageBuffer);
} }
/// <summary> /// <summary>
@ -204,7 +210,10 @@ namespace SixLabors.ImageSharp.IO
public uint ReadUInt32() public uint ReadUInt32()
{ {
this.ReadInternal(this.storageBuffer, 4); this.ReadInternal(this.storageBuffer, 4);
return this.BitConverter.ToUInt32(this.storageBuffer, 0);
return (this.endianness == Endianness.BigEndian)
? BinaryPrimitives.ReadUInt32BigEndian(this.storageBuffer)
: BinaryPrimitives.ReadUInt32LittleEndian(this.storageBuffer);
} }
/// <summary> /// <summary>
@ -215,7 +224,11 @@ namespace SixLabors.ImageSharp.IO
public ulong ReadUInt64() public ulong ReadUInt64()
{ {
this.ReadInternal(this.storageBuffer, 8); this.ReadInternal(this.storageBuffer, 8);
return this.BitConverter.ToUInt64(this.storageBuffer, 0);
return (this.endianness == Endianness.BigEndian)
? BinaryPrimitives.ReadUInt64BigEndian(this.storageBuffer)
: BinaryPrimitives.ReadUInt64LittleEndian(this.storageBuffer);
} }
/// <summary> /// <summary>
@ -223,10 +236,11 @@ namespace SixLabors.ImageSharp.IO
/// for this reader. 4 bytes are read. /// for this reader. 4 bytes are read.
/// </summary> /// </summary>
/// <returns>The floating point value read</returns> /// <returns>The floating point value read</returns>
public float ReadSingle() public unsafe float ReadSingle()
{ {
this.ReadInternal(this.storageBuffer, 4); int intValue = ReadInt32();
return this.BitConverter.ToSingle(this.storageBuffer, 0);
return *((float*)&intValue);
} }
/// <summary> /// <summary>
@ -234,10 +248,11 @@ namespace SixLabors.ImageSharp.IO
/// for this reader. 8 bytes are read. /// for this reader. 8 bytes are read.
/// </summary> /// </summary>
/// <returns>The floating point value read</returns> /// <returns>The floating point value read</returns>
public double ReadDouble() public unsafe double ReadDouble()
{ {
this.ReadInternal(this.storageBuffer, 8); long value = this.ReadInt64();
return this.BitConverter.ToDouble(this.storageBuffer, 0);
return *((double*)&value);
} }
/// <summary> /// <summary>
@ -245,10 +260,17 @@ namespace SixLabors.ImageSharp.IO
/// for this reader. 16 bytes are read. /// for this reader. 16 bytes are read.
/// </summary> /// </summary>
/// <returns>The decimal value read</returns> /// <returns>The decimal value read</returns>
public decimal ReadDecimal() public unsafe decimal ReadDecimal()
{ {
this.ReadInternal(this.storageBuffer, 16); decimal result = 0m;
return this.BitConverter.ToDecimal(this.storageBuffer, 0); int* presult = (int*)&result;
presult[0] = this.ReadInt32();
presult[1] = this.ReadInt32();
presult[2] = this.ReadInt32();
presult[3] = this.ReadInt32();
return result;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save