12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package de.tudarmstadt.informatik.hostage.wrapper;
- import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
- /**
- * Wrapper class for IO content.
- * @author Wulf Pfeiffer
- */
- public class Packet {
- private byte[] message = null;
- private boolean isStringMsg = false;
- private boolean isByteMsg = false;
- /**
- * Constructor.
- */
- public Packet() {
- message = null;
- }
-
- /**
- * Constructor.
- * If Packet is created with a String value, it is marked with a boolean.
- * @param message
- */
- public Packet(String message) {
- this.message = message.getBytes();
- isStringMsg = true;
- }
-
- /**
- * Constructor.
- * If Packet is created with a byte[] value, it is marked with a boolean.
- * @param message
- */
- public Packet(byte[] message) {
- this.message = message;
- isByteMsg = true;
- }
- /**
- * Returns the message value as byte[].
- * @return message value
- */
- public byte[] getMessage() {
- return message;
- }
- /**
- * Returns the class which the Packet was created with.
- * If the string constructor was used it returns String.class.
- * If the byte[] constructor was used it returns byte[].class.
- * Else it returns null.
- * @return class which the Packet was created with.
- */
- public Class<? extends Object> getType() {
- if (isStringMsg) {
- return String.class;
- } else if (isByteMsg){
- return byte[].class;
- } else {
- return null;
- }
- }
- /**
- * Checks whether the Packet was created with the given class or not.
- * @param type to be checked.
- * @return true if the packet was created with the given class, else false.
- */
- public boolean isOfType(Class<? extends Object> type) {
- return getType().equals(type);
- }
- /**
- * If the Packet was created with a byte[] it returns the hexadecimal byte value as a String,
- * else it returns a new String created by the byte[].
- */
- @Override
- public String toString() {
- if (isStringMsg) {
- return new String((byte[]) message);
- } else if (isByteMsg) {
- return HelperUtils.bytesToHexString((byte[]) message);
- } else {
- return null;
- }
- }
- }
|