-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
186 lines (167 loc) · 5.89 KB
/
Copy pathMessage.java
File metadata and controls
186 lines (167 loc) · 5.89 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
/**
* Team Project -- Message
*
* This file creates the message objects for when each user sends a message
*
* @author William Boulton, Jai Menon, Alan Yi, Mukund Venkatesh
*
* @version November 1, 2024
*
*/
public class Message implements MessageInterface {
private String sender; // usernames of sender and reciever
private String reciever;
private String content;
private byte[] pictureContent;
private boolean containsPicture;
private static int pictureLocation;
private int messageID;
private String pictureFile;
private final String pictureNumbers = "resources/picture.txt";
private final String messageIDFile = "resources/MessageIDCounter.txt";
private final String imageFolder = "resources/pictures/";
public final static Object LOCK = new Object();
// the file containing all sent and recieved messages for each user is just
// username.txt
// this should parse psv of some format, probably:
// messageID|sender|reciever|content|containsPicture|pictureFile
public Message(String data) {
synchronized (LOCK) {
String[] info = data.split("\\|");
messageID = Integer.parseInt(info[0]);
sender = info[1];
reciever = info[2];
content = info[3]; // content will be null if there is not text in the message and only a picture
// presumably
containsPicture = Boolean.parseBoolean(info[4]);
if (containsPicture) {
pictureFile = info[5]; // picture file only included if containsPicture
try {
pictureContent = Files.readAllBytes(Paths.get(pictureFile));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// This will be the direct creation of messages
public Message(User sender, User reciever, String content) throws BadDataException {
if (content.contains("|")) {
throw new BadDataException("Message content cannot contain '|'");
}
synchronized (LOCK) {
this.sender = sender.getUsername();
this.reciever = reciever.getUsername();
this.content = content;
try (BufferedReader bfr = new BufferedReader(new FileReader(messageIDFile))) {
messageID = Integer.parseInt(bfr.readLine());
} catch (Exception e) {
e.printStackTrace();
}
try (BufferedWriter bwr = new BufferedWriter(new FileWriter(messageIDFile))) {
String idLocation = String.format("%d", messageID + 1);
bwr.write(idLocation);
} catch (Exception e) {
e.printStackTrace();
}
containsPicture = false;
}
}
public int getMessageID() {
return messageID;
}
@Override
public void setMessageID(int id) {
this.messageID = id;
}
public String getReciever() {
return reciever;
}
public String getSender() {
return sender;
}
@Override
public boolean hasPicture() {
return this.containsPicture;
}
@Override
public byte[] getPicture() {
return this.pictureContent;
}
@Override
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public void editMessage(String newContent) {
synchronized (LOCK) {
this.content = newContent;
}
}
// I removed the send message and delete message functions. I think these would
// be
// better placed in the Message Database file
@Override
public void addPicture(byte[] newPictureContent) {
System.out.println("picture detected");
synchronized (LOCK) {
try (BufferedReader bfr = new BufferedReader(new FileReader(pictureNumbers))) {
pictureLocation = Integer.parseInt(bfr.readLine());
} catch (Exception e) {
System.out.println("Error reading picture number");
}
pictureFile = imageFolder + String.format("%d.jpg", pictureLocation);
try (BufferedWriter bwr = new BufferedWriter(new FileWriter(pictureNumbers))) {
bwr.write(Integer.toString(++pictureLocation));
} catch (Exception e) {
System.out.println("Error writing picture number");
}
try {
ByteArrayInputStream streamObj = new ByteArrayInputStream(newPictureContent);
BufferedImage newImage = ImageIO.read(streamObj);
ImageIO.write(newImage, "jpg", new File(pictureFile));
} catch (Exception e) {
System.out.println("Error writing picture");
}
System.out.println("picture added");
containsPicture = true;
}
}
@Override
public void readMessage() {
// this is intentionally left blank, we have the get content this would be used
// in the
// future for gui's possibly
}
@Override
public void editPicture(byte[] newPictureContent) {
synchronized (LOCK) {
this.pictureContent = newPictureContent;
}
}
@Override
public String toString() {
String result = String.format("%d|%s|%s|%s|%b", messageID, sender, reciever, content,
containsPicture);
if (containsPicture) {
result += "|" + pictureFile;
}
return result;
}
@Override
public boolean equals(Object o) {
if (o instanceof Message) {
Message m = (Message) o;
return m.getMessageID() == this.messageID;
}
return false;
}
}