using System;

public class Buffer {
	private int index;
	private byte[] array;

	public Buffer() {
		array = new byte[1024];
		index = 0;
	}

	public Buffer(byte[] array) {
		this.array = array;
		index = 0;
	}

	public byte[] getArray() {
		return array;
	}

	public int getLength() {
		return index;
	}

	public void writeBool(bool value) {
		byte[] conversion = BitConverter.GetBytes(value);

		for (int i = 0; i < conversion.Length; i++) {
			array[index + i] = conversion[i];
		}

		index += conversion.Length;
	}

	public void writeInt(int value) {
		byte[] conversion = BitConverter.GetBytes(value);

		for (int i = 0; i < conversion.Length; i++) {
			array[index + i] = conversion[i];
		}

		index += conversion.Length;
	}

	public void writeFloat(float value) {
		byte[] conversion = BitConverter.GetBytes(value);

		for (int i = 0; i < conversion.Length; i++) {
			array[index + i] = conversion[i];
		}

		index += conversion.Length;
	}

	public void writeChar(char value) {
		byte[] conversion = BitConverter.GetBytes(value);

		for (int i = 0; i < conversion.Length; i++) {
			array[index + i] = conversion[i];
		}

		index += conversion.Length;
	}

	public void writeString(string value) {
		writeInt(value.Length);
		foreach(char c in value.ToCharArray()) {
			writeChar(c);
		}
	}


	public bool readBool() {
		bool value = BitConverter.ToBoolean(array, index);
		index += 1;
		return value;
	}

	public int readInt() {
		int value = BitConverter.ToInt32(array, index);
		index += 4;
		return value;
	}

	public float readFloat() {
		float value = BitConverter.ToSingle(array, index);
		index += 4;
		return value;
	}

	public char readChar() {
		char value = BitConverter.ToChar(array, index);
		index += 2;
		return value;
	}

	public string readString() {
		string value = "";
		int length = readInt();
		for (int i = 0; i < length; i++) {
			value += readChar();
		}

		return value;
	}
}
