context: //#define ERROR
  
  using System;
  
  namespace L2_login
  {
  	public class ByteBuffer
  	{
  		private const int MAX_LENGTH = 1024;
  		private byte[] _data;
  		private int _length;
  		private int _maxlength;
  		private int _index;
  
  		public ByteBuffer(int len = MAX_LENGTH)
  		{
              if (len > _maxlength)
              {
                  _maxlength = len;
              }
              else
              {
                  _maxlength = MAX_LENGTH;
              }
  
  			_data = new byte[_maxlength];
  			_length = len;
  			_index = 0;
  		}
  
  		public ByteBuffer(byte[] buff)
  		{
  			_length = buff.Length;
  
  			if(_length > _maxlength)
  			{
  				_maxlength = _length;
  			}
  
  			_data = new byte[_maxlength];
  			//_length = //already set
  			_index = 0;
  
              buff.CopyTo(_data, 0);
  		}
  
          public ByteBuffer(ByteBuffer buff)
          {
              _length = buff._length;
              _maxlength = buff._maxlength;
  
              _data = new byte[_maxlength];
              _index = 0;
  
              buff._data.CopyTo(_data, 0);
          }
  
  		public int Length()
  		{
  			return _length;
  		}
  
  		public void Resize(int len)
  		{
  			if(len <= _maxlength)
  			{
  				//the new length is less than or equal to our maxlength
  				_length = len;
  			}
  			if(len > _maxlength)
  			{
  				//the new length is larger than our max length so we need to resize accordingly
  
  				//make a temp array of the length of our data
  				byte[] _copy = new byte[_length];
  
  				//copy our data over
                  _data.CopyTo(_copy, 0);
  
  				//set the new max length
  				_maxlength = len;
  
  				//remake our data array
  				_data = new byte[_maxlength];
  
  				//copy data back in up to length(still the size of the old data set)
                  _copy.CopyTo(_data, 0);
  
  				//set length to our max length
  				_length = _maxlength;
  			}
  		}
  
  		public void ResetIndex()
  		{
  			_index = 0;
  		}
  
  		public void ClearData()
  		{
  			for(int i = 0; i < _maxlength; i ++)
  			{
  				_data[i] = (byte)(0);
  			}
  		}
  
  		//read data
  
  		public UInt16 ReadUInt16()
  		{
  			if(_length >= _index + 2)
  			{
  				UInt16 temp = System.BitConverter.ToUInt16(_data,_index);
  				_index += 2;
  				return temp;
  			}
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadUInt16");
  #endif
  			return 0;
  		}
  
  		public UInt32 ReadUInt32()
  		{
  			if(_length >= _index + 4)
  			{
  				UInt32 temp = System.BitConverter.ToUInt32(_data,_index);
  				_index += 4;
  				return temp;
              }
  
  #if DEBUG
              //Globals.l2net_home.Add_Error("read beyond array size ReadUInt32");
  #endif
  			return 0;
  		}
  
  		public UInt64 ReadUInt64()
  		{
  			if(_length >= _index + 8)
  			{
  				UInt64 temp = System.BitConverter.ToUInt64(_data,_index);
  				_index += 8;
  				return temp;
              }
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadUInt64");
  #endif
  			return 0;
  		}
  		public Int16 ReadInt16()
  		{
  			if(_length >= _index + 2)
  			{
  				Int16 temp = System.BitConverter.ToInt16(_data,_index);
  				_index += 2;
  				return temp;
              }
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadInt16");
  #endif
  			return 0;
  		}
  
  		public Int32 ReadInt32()
  		{
  			if(_length >= _index + 4)
  			{
  				Int32 temp = System.BitConverter.ToInt32(_data,_index);
  				_index += 4;
  				return temp;
              }
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadInt32");
  #endif
  			return 0;
  		}
  
  		public Int64 ReadInt64()
  		{
  			if(_length >= _index + 8)
  			{
  				Int64 temp = System.BitConverter.ToInt64(_data,_index);
  				_index += 8;
  				return temp;
              }
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadInt64");
  #endif
  			return 0;
  		}
  
  		public double ReadDouble()
  		{
  			if(_length >= _index + 8)
  			{
  				double temp = System.BitConverter.ToDouble(_data,_index);
  				_index += 8;
  				return temp;
              }
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadDouble");
  #endif
  			return 0;
  		}
  
  		public char ReadChar()//2 bytes, unicode, use ReadBtye for 1 byte
  		{
  			if(_length >= _index + 2)
  			{
  				char temp = System.BitConverter.ToChar(_data,_index);
  				_index += 2;
  				return temp;
              }
  
  #if DEBUG && ERROR
              Globals.l2net_home.Add_Error("read beyond array size ReadChar");
  #endif
  			return (char)0;
  		}
  
  		public byte ReadByte()
  		{
  			if(_length >= _index + 1)
  			{
  				byte temp = _data[_index];
  
gt: 				_index += 1;