You are on page 1of 5

FileClypeData.

java
1 package data;
2
3 import java.io.BufferedReader;
13
14 @SuppressWarnings("unused")
15 public class FileClypeData extends ClypeData {
16
17 /**
18 *
19 */
20 private static final long serialVersionUID = 4298652678220834814L;
21 private String fileName, fileContents;
22
23 /**
24 *
25 * Constructor that takes a username, filename, and type. Also calls super constructor in
ClypeData.
26 *
27 * @param userName username associated with data
28 * @param fileName file name associated with data
29 * @param type type of the data, defined in ClypeData
30 */
31 public FileClypeData(String userName, String fileName, int type, int clientID) {
32 super(userName,type,clientID);
33 this.fileName = fileName;
34 this.fileContents = null;
35 }
36
37 /**
38 * Default constructor that initializes userName to "Anonymous", the fileName to "", and
the type to 2 by calling
39 * the super default constructor. Also initializes fileName to "" and fileContents to
null.
40 */
41 public FileClypeData(int clientID) {
42 super(clientID);
43 this.fileName = "";
44 this.fileContents = null;
45 }
46
47 /**
48 * Setter method for fileName
49 *
50 * @param fileName name of the file
51 */
52 public void setFileName(String fileName) {
53 this.fileName = fileName;
54 }
55
56 /**
57 *
58 * Getter method for fileName
59 * @return the name of the file
60 */
61 public String getFileName() {
62 return fileName;
63 }
64
65 /**
FileClypeData.java
66 * Overriden method of getData().
67 *
68 * @return contents of the file
69 */
70 @Override
71 public String getData() {
72 return fileContents;
73 }
74
75 /**
76 * Overriden method of getData(String key) which returns the decrypted fileContents
77 *
78 * @param key the String used as a key for encryption and decryption
79 * @return the decrypted fileContents using the key
80 */
81 @Override
82 public String getData(String key) {
83 return decrypt(fileContents, key);
84 }
85
86 /**
87 *
88 * Reads content of a file
89 *
90 * @throws IOException error that may occur
91 */
92 public void readFileContents() throws IOException {
93
94 try {
95 BufferedReader bufferedReader = new BufferedReader(new FileReader(this.fileName));
96 String line = "";
97
98 if(fileContents == null)
99 fileContents = "";
100
101 while((line = bufferedReader.readLine()) != null) {
102
103 if(fileContents != "")
104 fileContents += "\n";
105
106 fileContents += line;
107 //System.out.println(fileContents);
108 }
109
110 bufferedReader.close();
111 } catch(FileNotFoundException e) {
112 throw e;
113 } catch(IOException e) {
114 throw e;
115 }
116
117 }
118
119
120 /**
121 *
122 * Reads and then encrypts content of file
123 *
124 * @param key the key used for encryption
FileClypeData.java
125 * @throws IOException error that may occur
126 */
127 public void readFileContents(String key) throws IOException {
128
129 try {
130 BufferedReader bufferedReader = new BufferedReader(new FileReader(this.fileName));
131 String line = "";
132
133 if(fileContents == null)
134 fileContents = "";
135
136 while((line = bufferedReader.readLine()) != null) {
137
138 if(fileContents != "")
139 fileContents += "\n";
140
141 fileContents += line;
142 //System.out.println(fileContents);
143 }
144
145 bufferedReader.close();
146 } catch(FileNotFoundException e) {
147 throw e;
148 } catch(IOException e) {
149 throw e;
150 }
151
152 //encryption
153 fileContents = encrypt(fileContents, key);
154
155 }
156
157
158 /**
159 *
160 * Writes file contents to file
161 *
162 * @throws IOException excpetion that could be thrown
163 */
164 public void writeFileContents() throws IOException {
165
166 try {
167 BufferedWriter bw = new BufferedWriter(new FileWriter(this.fileName));
168 bw.write(fileContents);
169 bw.close();
170 } catch(FileNotFoundException e) {
171 throw e;
172 } catch(IOException e) {
173 throw e;
174 }
175
176 }
177
178 /**
179 *
180 * Decrypts and then writes file contents to file
181 *
182 * @param key key used for decryption
183 * @throws IOException excpetion that could be thrown
FileClypeData.java
184 */
185 public void writeFileContents(String key) throws IOException {
186
187 try {
188 BufferedWriter bw = new BufferedWriter(new FileWriter(this.fileName));
189 bw.write(decrypt(fileContents, key));
190 bw.close();
191 } catch(FileNotFoundException e) {
192 throw e;
193 } catch(IOException e) {
194 throw e;
195 }
196
197 }
198
199 /**
200 * Overriden method of equals(Object obj).
201 */
202 @Override
203 public boolean equals(Object obj) {
204 if(this == obj)
205 return true;
206 if(obj == null || !(obj instanceof FileClypeData))
207 return false;
208
209 FileClypeData f = (FileClypeData) obj;
210 if(f.getUserName() == this.getUserName() && f.getData() == this.getData() && f.getType
() == this.getType()
211 && f.getFileName() == this.getFileName())
212 return true;
213 else
214 return false;
215 }
216
217 /**
218 * Overriden hashCode() method.
219 */
220 @SuppressWarnings("deprecation")
221 @Override
222 public int hashCode() {
223 //from lecture notes
224 int result = 17;
225 if(this.getUserName() != null)
226 result = 37*result + this.getUserName().hashCode();
227 if(this.getData() != null)
228 result = 37*result + this.getData().hashCode();
229 if(this.getFileName() != null)
230 result = 37*result + this.getFileName().hashCode();
231 result = 37*result + this.getType();
232 result = 37*result + this.getDate().getDate();
233 return result;
234 }
235
236 /**
237 * @return useful information that will be printed out to the console for the user to see
238 */
239 public String getDataToPrint() {
240 return "File Name: " + this.getFileName() + "\n"
241 + "File Contents: " + this.getData();
FileClypeData.java
242 }
243
244 /**
245 * Overriden toString() method that returns/prints the following:
246 * userName, fileName, fileContents, type, and the date of creation
247 */
248 @Override
249 public String toString() {
250
251 return "Username: " + this.getUserName() + "\n"
252 + "File Name: " + this.getFileName() + "\n"
253 + "File Contents: " + this.getData() + "\n"
254 + "Type: File (" + this.getType() + ")\n"
255 + "Date Created: " + this.getDate() + "\n";
256
257 }
258
259 }
260

You might also like