PageRenderTime 208ms CodeModel.GetById 60ms app.highlight 65ms RepoModel.GetById 76ms app.codeStats 1ms

/WorldView/Utilities/BufferedBinaryReader.cs

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