Packet.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package de.tudarmstadt.informatik.hostage.wrapper;
  2. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  3. /**
  4. * Wrapper class for the payload of a network packet.
  5. *
  6. * @author Mihai Plasoianu
  7. * @author Wulf Pfeiffer
  8. */
  9. public class Packet {
  10. private byte[] payload;
  11. /**
  12. * Constructs Packet from byte[]
  13. *
  14. * @param payload
  15. * The byte[] payload
  16. */
  17. public Packet(byte[] payload) {
  18. this.payload = payload;
  19. }
  20. /**
  21. * Constructs Packet from String
  22. *
  23. * @param payload
  24. * The String payload
  25. */
  26. public Packet(String payload) {
  27. this.payload = payload.getBytes();
  28. }
  29. /**
  30. * Returns a byte[] representation of the payload.
  31. *
  32. * @return byte[] representation.
  33. */
  34. public byte[] getBytes() {
  35. return payload;
  36. }
  37. /**
  38. * Returns a String representation of the payload.
  39. * If the payload contains a ASCII character the whole
  40. * String will be represented as a String of a byte values.
  41. * E.g.: the byte[] {0x01, 0x4A, 0x03} would look like
  42. * the String "01, 4A, 03".
  43. * Otherwise a normal String will be created with the payload.
  44. *
  45. * @return String representation.
  46. */
  47. @Override
  48. public String toString() {
  49. for (int i = 0; i < payload.length; ++i) {
  50. if (((char) payload[i]) < 9) {
  51. return HelperUtils.bytesToHexString(payload);
  52. }
  53. }
  54. return new String(payload);
  55. }
  56. }