Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- /// <summary>
- /// Rio MemBinaryWriter 1.0
- /// </summary>
- public class MemBinaryWriter : IDisposable
- {
- private byte[] _Buffer;
- private int _Pos = 0;
- /// <summary>
- /// Current position in buffer
- /// </summary>
- public int Position
- {
- get { return _Pos; }
- set { _Pos = value; }
- }
- /// <summary>
- /// Buffer lenght
- /// </summary>
- public int Lenght => _Buffer.Length;
- /// <summary>
- /// Initializate the new instance of MemBinaryWriter with setted buffer capacity
- /// </summary>
- public MemBinaryWriter(int capacity)
- {
- _Buffer = new byte[capacity];
- }
- /// <summary>
- /// Initializate the new instance of MemBinaryWriter with unknown buffer capacity
- /// </summary>
- public MemBinaryWriter()
- {
- _Buffer = new byte[1];
- }
- /// <summary>
- /// Get the buffer
- /// </summary>
- public byte[] GetBuffer()
- {
- return _Buffer;
- }
- /// <summary>
- /// Write bytes into buffer
- /// </summary>
- public void WriteBytes(byte[] bytes)
- {
- if (bytes != null) //Bytes not a null
- {
- //Check array lenght, and expand if need
- if (bytes.Length >= (_Buffer.Length - _Pos))
- Array.Resize(ref _Buffer, _Buffer.Length + (bytes.Length - (_Buffer.Length - _Pos)));
- //Write to buffer
- Array.Copy(bytes, 0, _Buffer, _Pos, bytes.Length);
- //Offseting to the new position
- _Pos += bytes.Length;
- }
- }
- /// <summary>
- /// Write any value, int16, double, byte, byte array and etc.
- /// </summary>
- public bool Write<T>(T value)
- {
- object obj = value;
- Type valType = typeof(T);
- if (valType == typeof(byte[]))
- {
- WriteBytes((byte[])obj);
- return true;
- }
- else
- {
- byte[] array = null;
- switch (Type.GetTypeCode(valType))
- {
- case TypeCode.Boolean:
- array = BitConverter.GetBytes((bool)obj);
- break;
- case TypeCode.Char:
- array = BitConverter.GetBytes((char)obj);
- break;
- case TypeCode.Byte:
- array = new byte[] { (byte)obj };
- break;
- case TypeCode.Int16:
- array = BitConverter.GetBytes((short)obj);
- break;
- case TypeCode.UInt16:
- array = BitConverter.GetBytes((ushort)obj);
- break;
- case TypeCode.Int32:
- array = BitConverter.GetBytes((int)obj);
- break;
- case TypeCode.UInt32:
- array = BitConverter.GetBytes((uint)obj);
- break;
- case TypeCode.Int64:
- array = BitConverter.GetBytes((long)obj);
- break;
- case TypeCode.UInt64:
- array = BitConverter.GetBytes((ulong)obj);
- break;
- case TypeCode.Single:
- array = BitConverter.GetBytes((float)obj);
- break;
- case TypeCode.Double:
- array = BitConverter.GetBytes((double)obj);
- break;
- default:
- return false;
- }
- WriteBytes(array);
- return true;
- }
- }
- /// <summary>
- /// Write string into buffer
- /// </summary>
- public void WriteString(string str)
- {
- if (str != null)
- WriteBytes(Encoding.UTF8.GetBytes(str + '\0')); //Plus zero terminator
- else
- WriteBytes(Encoding.UTF8.GetBytes(string.Empty + '\0')); //String are null!
- }
- //Fake dispose :), its only for 'using (var bw = new MemBinaryWriter(arr)) {}'
- public void Dispose() { }
- }
Add Comment
Please, Sign In to add comment