PageRenderTime 20ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/WorldView/Utilities/BufferedBinaryReader.cs

#
C# | 147 lines | 107 code | 40 blank | 0 comment | 12 complexity | 540a8b7c490a46dc85de168c70904f4e MD5 | raw file
  1. using System;
  2. using System.IO;
  3. namespace MoreTerra.Utilities
  4. {
  5. class BufferedBinaryReader
  6. {
  7. private Int32 bufferSize;
  8. private Int32 maxBufferSize;
  9. private Byte[] buffer;
  10. private Stream stream;
  11. private Int64 bufferPosition;
  12. private Int64 startPosition;
  13. private Int64 endPosition;
  14. private Int64 streamEnd;
  15. private Int32 oldMininum;
  16. public BufferedBinaryReader(Stream str, Int32 bSize, Int32 oldMin)
  17. {
  18. if (str == null)
  19. return;
  20. stream = str;
  21. streamEnd = stream.Length;
  22. oldMininum = oldMin;
  23. if (bSize < 0)
  24. return;
  25. maxBufferSize = bSize;
  26. buffer = new Byte[maxBufferSize];
  27. bufferPosition = stream.Position;
  28. startPosition = stream.Position;
  29. endPosition = startPosition + bufferSize;
  30. }
  31. public Boolean ReadBoolean()
  32. {
  33. Int64 readPos;
  34. readPos = bufferPosition - startPosition;
  35. if (readPos >= bufferSize)
  36. {
  37. FillBuffer();
  38. if (bufferSize <= 0)
  39. throw new EndOfStreamException();
  40. readPos = bufferPosition - startPosition;
  41. }
  42. bufferPosition++;
  43. if (buffer[readPos] > 1)
  44. return true;
  45. return false;
  46. }
  47. public Byte ReadByte()
  48. {
  49. Int64 readPos;
  50. readPos = bufferPosition - startPosition;
  51. if (readPos >= bufferSize)
  52. {
  53. FillBuffer();
  54. if (bufferSize <= 0)
  55. throw new EndOfStreamException();
  56. readPos = bufferPosition - startPosition;
  57. }
  58. bufferPosition++;
  59. return buffer[readPos];
  60. }
  61. public short ReadInt16()
  62. {
  63. Int16 ret16 = 0;
  64. Int64 readPos;
  65. readPos = bufferPosition - startPosition;
  66. if (readPos >= bufferSize - 1)
  67. {
  68. FillBuffer();
  69. if (bufferSize <= 0)
  70. throw new EndOfStreamException();
  71. readPos = bufferPosition - startPosition;
  72. }
  73. bufferPosition += 2;
  74. ret16 = buffer[readPos++];
  75. ret16 += (Int16) (buffer[readPos++] * 256);
  76. return ret16;
  77. }
  78. private void FillBuffer()
  79. {
  80. Int64 readOffset;
  81. Int32 readCount;
  82. Int32 readBytes;
  83. readOffset = stream.Position - oldMininum;
  84. if (readOffset < 0)
  85. readOffset = 0;
  86. readCount = maxBufferSize;
  87. if (readOffset + readCount > streamEnd)
  88. readCount = (Int32) (streamEnd - readOffset);
  89. stream.Seek(readOffset, SeekOrigin.Begin);
  90. readBytes = stream.Read(buffer, 0, readCount);
  91. startPosition = readOffset;
  92. endPosition = readOffset + readBytes;
  93. bufferSize = readBytes;
  94. }
  95. public void Seek(Int64 offset)
  96. {
  97. bufferPosition = offset;
  98. }
  99. public Int64 Position
  100. {
  101. get
  102. {
  103. return bufferPosition;
  104. }
  105. }
  106. }
  107. }