-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextPacket.java
More file actions
67 lines (57 loc) · 1.45 KB
/
TextPacket.java
File metadata and controls
67 lines (57 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @author Thomas Moroney
*/
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Class for packet content that represents acknowledgements
*
*/
public class TextPacket extends PacketContent {
String text;
/**
* Constructor that takes in string.
* @param text Text string.
*/
TextPacket(String text) {
type= TEXTPACKET; // equal to 10 (value to identify)
this.text = text;
}
/**
* Constructs an object out of a datagram packet.
* @param packet Packet that contains a string.
*/
protected TextPacket(ObjectInputStream oin) { // when receive a packet it comes as ObjectInputStream (string)
try {
type= TEXTPACKET; // just assigning
text= oin.readUTF(); // takes content and turns it back into original form - must change to readbytes etc to read data
}
catch(Exception e) {e.printStackTrace();}
}
/**
* Writes the content into an ObjectOutputStream
*
*/
protected void toObjectOutputStream(ObjectOutputStream oout) { // converts to UTF8 for transfer
try {
oout.writeUTF(text); // UTF8 encoding
}
catch(Exception e) {e.printStackTrace();} //
}
/**
* Returns the content of the packet as String.
*
* @return Returns the content of the packet as String.
*/
public String toString() {
return "Content:" + text;
}
/**
* Returns the info contained in the packet.
*
* @return Returns the info contained in the packet.
*/
public String getPacketInfo() {
return text;
}
}