ByteOrderSwappingBinaryReader.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //Apache2, 2014-2016, Samuel Carlsson, WinterDev
  2. using System;
  3. using System.IO;
  4. namespace Typography.OpenFont
  5. {
  6. class ByteOrderSwappingBinaryReader : BinaryReader
  7. {
  8. //All OpenType fonts use Motorola-style byte ordering (Big Endian)
  9. //
  10. public ByteOrderSwappingBinaryReader(Stream input)
  11. : base(input)
  12. {
  13. }
  14. protected override void Dispose(bool disposing)
  15. {
  16. GC.SuppressFinalize(this);
  17. base.Dispose(disposing);
  18. }
  19. //
  20. //as original
  21. //
  22. //public override byte ReadByte() { return base.ReadByte(); }
  23. //
  24. //we override the 4 methods here
  25. //
  26. public override short ReadInt16() => BitConverter.ToInt16(RR(2), 8 - 2);
  27. public override ushort ReadUInt16() => BitConverter.ToUInt16(RR(2), 8 - 2);
  28. public override uint ReadUInt32() => BitConverter.ToUInt32(RR(4), 8 - 4);
  29. public override ulong ReadUInt64() => BitConverter.ToUInt64(RR(8), 8 - 8);
  30. //used in CFF font
  31. public override double ReadDouble() => BitConverter.ToDouble(RR(8), 8 - 8);
  32. //used in CFF font
  33. public override int ReadInt32() => BitConverter.ToInt32(RR(4), 8 - 4);
  34. //
  35. readonly byte[] _reusable_buffer = new byte[8]; //fix buffer size to 8 bytes
  36. /// <summary>
  37. /// read and reverse
  38. /// </summary>
  39. /// <param name="count"></param>
  40. /// <returns></returns>
  41. private byte[] RR(int count)
  42. {
  43. base.Read(_reusable_buffer, 0, count);
  44. Array.Reverse(_reusable_buffer);
  45. return _reusable_buffer;
  46. }
  47. //we don't use these methods in our OpenFont, so => throw the exception
  48. public override int PeekChar() { throw new NotImplementedException(); }
  49. public override int Read() { throw new NotImplementedException(); }
  50. public override int Read(byte[] buffer, int index, int count) => base.Read(buffer, index, count);
  51. public override int Read(char[] buffer, int index, int count) { throw new NotImplementedException(); }
  52. public override bool ReadBoolean() { throw new NotImplementedException(); }
  53. public override char ReadChar() { throw new NotImplementedException(); }
  54. public override char[] ReadChars(int count) { throw new NotImplementedException(); }
  55. public override decimal ReadDecimal() { throw new NotImplementedException(); }
  56. public override long ReadInt64() { throw new NotImplementedException(); }
  57. public override sbyte ReadSByte() { throw new NotImplementedException(); }
  58. public override float ReadSingle() { throw new NotImplementedException(); }
  59. public override string ReadString() { throw new NotImplementedException(); }
  60. //
  61. }
  62. }