-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessage.java
More file actions
56 lines (46 loc) · 1.41 KB
/
Message.java
File metadata and controls
56 lines (46 loc) · 1.41 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
package com.yurii.salimov.lesson12.task03;
import java.io.*;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public final class Message implements Serializable {
private static final long serialVersionUID = 1L;
private final String from;
private final String to;
private final transient String text;
public Message(final String from, final String to, final String text) {
this.from = from;
this.to = to;
this.text = text;
}
@Override
public String toString() {
return "Message{" +
"from='" + this.from + '\'' +
", to='" + this.to + '\'' +
", text='" + this.text + '\'' +
'}';
}
public void writeToStream(final OutputStream out) throws IOException {
try (ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bs);
DataOutputStream ds = new DataOutputStream(out);) {
os.writeObject(this);
os.writeUTF(this.text);
byte[] packet = bs.toByteArray();
ds.writeInt(packet.length);
ds.write(packet);
ds.flush();
}
}
public String getFrom() {
return this.from;
}
public String getTo() {
return this.to;
}
public String getText() {
return this.text;
}
}