Code:
using System;
using System.Text;
namespace BufferSharp
{
public class BufferData
{
private byte[] _buffer; //Holds our total amount of bytes.
private int _offset; // Holds our current offset in the byte array.
public BufferData()
{
_buffer = new byte[0];
}
/// <summary>
/// Writes A Byte Into The Buffer
/// </summary>
/// <param name="value">Byte Value To Write Into The Buffer.</param>
public void WriteByte(byte value)
{
byte[] src = BitConverter.GetBytes(value);
byte[] tmparray = new byte[_buffer.Length + src.Length];
Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
_buffer = tmparray;
}
/// <summary>
/// Writes A 64 Bit Integer Into The Buffer.
/// </summary>
/// <param name="value">Long Value To Write Into The Buffer.</param>
public void WriteLong(long value)
{
byte[] src = BitConverter.GetBytes(value);
byte[] tmparray = new byte[_buffer.Length + src.Length];
Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
_buffer = tmparray;
}
/// <summary>
/// Writes A 16 Bit Integer Into The Buffer.
/// </summary>
/// <param name="value">Int Value To Write Into The Buffer.</param>
public void WriteInt16(Int16 value)
{
byte[] src = BitConverter.GetBytes(value);
byte[] tmparray = new byte[_buffer.Length + src.Length];
Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
_buffer = tmparray;
}
/// <summary>
/// Writes A String Into The Buffer.
/// </summary>
/// <param name="value">String Value To Write Into The Buffer.</param>
public void WriteString(string value)
{
byte[] src = ASCIIEncoding.ASCII.GetBytes(value);
WriteByte((byte)src.Length);
byte[] tmpdata = new byte[_buffer.Length + src.Length];
Buffer.BlockCopy(_buffer, 0, tmpdata, 0, _buffer.Length);
Buffer.BlockCopy(src, 0, tmpdata, tmpdata.Length - src.Length, src.Length);
_buffer = tmpdata;
}
/// <summary>
/// Writes A 32 Bit Integer Into The Buffer.
/// </summary>
/// <param name="value">Int Value To Write Into The Buffer</param>
public void WriteInt32(Int32 value)
{
byte[] src = BitConverter.GetBytes(value);
byte[] tmparray = new byte[_buffer.Length + src.Length];
Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
_buffer = tmparray;
}
/// <summary>
/// Grabs The Buffer For Usage.
/// </summary>
/// <returns></returns>
public byte[] GrabBuffer()
{
return _buffer;
}
/// <summary>
/// Prepares The Buffer For Reading.
/// </summary>
/// <param name="buffer">Byte Array To Begin Reading From.</param>
public void BeginRead(byte[] buffer)
{
_buffer = buffer;
_offset = 0;
}
/// <summary>
/// Reads An Int From The Buffer.
/// </summary>
/// <returns>A 16 Bit Int Value.</returns>
public Int16 ReadInt16()
{
byte[] tmpdata = new byte[3];
System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 2);
_offset = _offset + 2;
return BitConverter.ToInt16(tmpdata, 0);
}
/// <summary>
/// Reads An Int From The Buffer.
/// </summary>
/// <returns>A 32 Bit Int Value</returns>
public Int32 ReadInt32()
{
byte[] tmpdata = new byte[5];
System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 4);
_offset = _offset + 4;
return BitConverter.ToInt32(tmpdata, 0);
}
/// <summary>
/// Reads A Byte From The Buffer.
/// </summary>
/// <returns>A 8 Bit Byte Value.</returns>
public byte ReadByte()
{
byte[] tmpdata = new byte[2];
System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 1);
_offset = _offset + 2;
return Convert.ToByte(BitConverter.ToInt16(tmpdata, 0));
}
/// <summary>
/// Reads A String From The Buffer.
/// </summary>
/// <returns>String Read From The Buffer.</returns>
public string ReadString()
{
byte size = ReadByte();
byte[] tmpdata = new byte[size + 1];
System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, size);
_offset = _offset + size;
return System.Text.ASCIIEncoding.ASCII.GetString(tmpdata);
}
/// <summary>
/// Reads A Long Value From The Buffer
/// </summary>
/// <returns>A 64 Bit Int Value Read From The Buffer.</returns>
public long ReadLong()
{
byte[] tmpdata = new byte[9];
System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 8);
_offset = _offset + 8;
return BitConverter.ToInt64(tmpdata, 0);
}
}
}
Regards,