package de.tudarmstadt.informatik.hostage.wrapper; import de.tudarmstadt.informatik.hostage.commons.HelperUtils; /** * Wrapper class for the payload of a network packet. * * @author Mihai Plasoianu * @author Wulf Pfeiffer */ 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. * If the payload contains a ASCII character the whole * String will be represented as a String of a byte values. * E.g.: the byte[] {0x01, 0x4A, 0x03} would look like * the String "01, 4A, 03". * Otherwise a normal String will be created with the payload. * * @return String representation. */ @Override public String toString() { for (int i = 0; i < payload.length; ++i) { if (((char) payload[i]) < 9) { return HelperUtils.bytesToHexString(payload); } } return new String(payload); } }