using System;

namespace DM2AE
{
	// 
	public class BinaryThruRead4Bit
	{
		// 
		private System.IO.Stream m_FileIn;
		// 
		private System.Byte m_Data, m_DataMask;
		// 
		private System.Byte ReadByte()
		{
			int x = m_FileIn.ReadByte();
			if (x == -1)
				throw new System.IO.EndOfStreamException("EOF Reached.");
			return (System.Byte)x;
		}
		// 
		public BinaryThruRead4Bit(
			System.IO.Stream FileIn)
		{
			m_FileIn = FileIn;
			m_Data = m_DataMask = 0;
		}
		// 
		public void Close()
		{
			m_FileIn = null;
			m_Data = m_DataMask = 0;
		}
		// 
		public System.Byte Read4Bit()
		{
			if (m_DataMask == 0)
			{
				m_Data = ReadByte();
				m_DataMask = 0xFF;
			}
			System.Byte RetVal = (System.Byte)((m_Data & 0xf0) / 16);
			m_DataMask = (System.Byte)((m_DataMask * 16) & 0xf0);
			m_Data = (System.Byte)((m_Data * 16) & 0xf0);
			return RetVal;
		}
		// 
		public System.Byte Peek4Bit()
		{
			if (m_DataMask == 0)
			{
				m_Data = ReadByte();
				m_DataMask = 0xFF;
			}
			return (System.Byte)((m_Data & 0xf0) / 16);
		}
	};
}
