-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPacket.java
More file actions
64 lines (56 loc) · 1.36 KB
/
Copy pathMyPacket.java
File metadata and controls
64 lines (56 loc) · 1.36 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
/**
* @author Thomas Moroney
*/
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
/**
* Class for packet content that represents the contents of the file in a byte array
*
*/
public class MyPacket extends PacketContent {
byte[] byteArray;
/**
* Constructor that takes in information about a file.
* @param byteArray Byte array containing file contents.
*/
MyPacket(byte[] byteArray) {
type= DATAPACKET;
this.byteArray = byteArray;
}
/**
* Reads ObjectInputStream into a byte array.
* @param oin ObjectInputStream that contains file content.
*/
protected MyPacket(ObjectInputStream oin) {
try {
type= DATAPACKET;
for(int i=0; i<byteArray.length; i++) {
byteArray[i] = oin.readByte();
}
}
catch(Exception e) {e.printStackTrace();}
}
/**
* Writes the content into an ObjectOutputStream
*
*/
protected void toObjectOutputStream(ObjectOutputStream oout) {
try {
for(int i=0; i<byteArray.length; i++) {
oout.writeByte(byteArray[i]);
}
}
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() {
String s = new String(byteArray, StandardCharsets.UTF_8);
System.out.println("USING TO STRING HERE ALERT");
return s;
}
}