BufferedBinaryReader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. // This is a modified version of the script from: https://jacksondunstan.com/articles/3568
  7. /// <summary> Much faster than BinaryReader </summary>
  8. public class BufferedBinaryReader : IDisposable {
  9. private readonly Stream stream;
  10. private readonly byte[] buffer;
  11. private readonly int bufferSize;
  12. private int bufferOffset;
  13. private int bufferedBytes;
  14. private int byteStride;
  15. private Bit2Converter bit2Converter;
  16. private Bit4Converter bit4Converter;
  17. public long Position { get { return stream.Position + bufferOffset; } set { stream.Position = value; bufferedBytes = 0; bufferOffset = 0; } }
  18. public BufferedBinaryReader(Stream stream, int bufferSize) {
  19. this.stream = stream;
  20. this.bufferSize = bufferSize;
  21. buffer = new byte[bufferSize];
  22. bufferOffset = 0;
  23. bufferedBytes = 0;
  24. }
  25. private void FillBuffer(int byteCount) {
  26. int unreadBytes = bufferedBytes - bufferOffset;
  27. if (unreadBytes < byteCount) {
  28. // If not enough bytes left in buffer
  29. if (unreadBytes != 0) {
  30. // If buffer still has unread bytes, move them to start of buffer
  31. Buffer.BlockCopy(buffer, bufferOffset, buffer, 0, unreadBytes);
  32. }
  33. bufferedBytes = stream.Read(buffer, unreadBytes, bufferSize - unreadBytes) + unreadBytes;
  34. bufferOffset = 0;
  35. }
  36. }
  37. public byte ReadByte() {
  38. FillBuffer(1);
  39. return buffer[bufferOffset++];
  40. }
  41. public sbyte ReadSByte() {
  42. FillBuffer(1);
  43. return (sbyte) buffer[bufferOffset++];
  44. }
  45. public ushort ReadUInt16() {
  46. FillBuffer(sizeof(ushort));
  47. return bit2Converter.Read(buffer, ref bufferOffset).@ushort;
  48. }
  49. public short ReadInt16() {
  50. FillBuffer(sizeof(short));
  51. return bit2Converter.Read(buffer, ref bufferOffset).@short;
  52. }
  53. public uint ReadUInt32() {
  54. FillBuffer(sizeof(uint));
  55. return bit4Converter.Read(buffer, ref bufferOffset).@uint;
  56. }
  57. public int ReadInt32() {
  58. FillBuffer(sizeof(int));
  59. return bit4Converter.Read(buffer, ref bufferOffset).@int;
  60. }
  61. public float ReadSingle() {
  62. FillBuffer(sizeof(float));
  63. return bit4Converter.Read(buffer, ref bufferOffset).@float;
  64. }
  65. public void Skip(int bytes) {
  66. FillBuffer(bytes);
  67. bufferOffset += bytes;
  68. }
  69. [StructLayout(LayoutKind.Explicit)]
  70. public struct Bit2Converter {
  71. [FieldOffset(0)] public byte b0;
  72. [FieldOffset(1)] public byte b1;
  73. [FieldOffset(0)] public short @short;
  74. [FieldOffset(0)] public ushort @ushort;
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public Bit2Converter Read(byte[] buffer, ref int bufferOffset) {
  77. b0 = buffer[bufferOffset++];
  78. b1 = buffer[bufferOffset++];
  79. return this;
  80. }
  81. }
  82. [StructLayout(LayoutKind.Explicit)]
  83. public struct Bit4Converter {
  84. [FieldOffset(0)] public byte b0;
  85. [FieldOffset(1)] public byte b1;
  86. [FieldOffset(2)] public byte b2;
  87. [FieldOffset(3)] public byte b3;
  88. [FieldOffset(0)] public float @float;
  89. [FieldOffset(0)] public int @int;
  90. [FieldOffset(0)] public uint @uint;
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public Bit4Converter Read(byte[] buffer, ref int bufferOffset) {
  93. b0 = buffer[bufferOffset++];
  94. b1 = buffer[bufferOffset++];
  95. b2 = buffer[bufferOffset++];
  96. b3 = buffer[bufferOffset++];
  97. return this;
  98. }
  99. }
  100. public void Dispose() {
  101. stream.Close();
  102. }
  103. }