package de.tudarmstadt.informatik.hostage.wrapper;

/**
 * Wrapper class for the payload of a network packet.
 * 
 * @author Mihai Plasoianu
 */
public class Packet {

	private byte[] payload;

	/**
	 * Constructs Packet from byte[]
	 * 
	 * @param payload
	 *            The byte[] payload
	 */
	public Packet(byte[] payload) {
		this.payload = payload;
	}

	/**
	 * Constructs Packet from String
	 * 
	 * @param payload
	 *            The String payload
	 */
	public Packet(String payload) {
		this.payload = payload.getBytes();
	}

	/**
	 * Returns a byte[] representation of the payload.
	 * 
	 * @return byte[] representation.
	 */
	public byte[] getBytes() {
		return payload;
	}

	/**
	 * Returns a String representation of the payload.
	 * 
	 * @return String representation.
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(payload.length);
		for (int i = 0; i < payload.length; ++i) {
			if (payload[i] < 32) {
				builder.append("{0x").append(payload[i]).append("}");
			} else {
				builder.append(Character.toString((char) payload[i]));
			}
		}
		return builder.toString();
	}

}