-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patholdTree.java
More file actions
255 lines (212 loc) · 7.1 KB
/
oldTree.java
File metadata and controls
255 lines (212 loc) · 7.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
public class oldTree {
private File tree;
//might need a hashmap
public oldTree () {
}
public void initialize() throws IOException {
String dirName = "./objects/";
File dir = new File (dirName);
dir.mkdir();
tree = new File (dir, "tree");
//tree = new File ("tree");
if(!tree.exists()) {
tree.createNewFile();
}
}
/*public void initialize() throws IOException {
String dirName = "./objects/";
File dir = new File (dirName);
dir.mkdir();
tree = new File (dir, "tree");
rename(tree);
}
private void rename(File fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String str = "";
while(br.ready()) {
str += br.readLine();
}
br.close();
//converting to sha1
String sha1 = encryptPassword(str);
//System.out.println("TEST BYTE: " + str);
//System.out.println("SHA : " + sha1);
//printing to objects folder
String dirName = "./objects/";
File dir = new File (dirName);//create this directory (File class java)
//dir.mkdir();
tree = new File (dir, sha1);
PrintWriter pw = new PrintWriter(tree);
pw.print(str);
pw.close();
}
private String encryptPassword(String password)
{
String sha1 = "";
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(password.getBytes("UTF-8"));
sha1 = byteToHex(crypt.digest());
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
return sha1;
}
private String byteToHex(final byte[] hash)
{
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}*/
public boolean addTree(String input) throws IOException {
if(!entryExists(input, tree)) { //is the input just the whole string? or do u need to create something?
/*if(checkContents(tree).equals("")){
PrintWriter pw = new PrintWriter(new FileWriter(tree, true));
pw.append(input);//need some way to not have this extra \n
pw.close();
return true;
}
else {
PrintWriter pw = new PrintWriter(new FileWriter(tree, true));
pw.append("\n" + input);
pw.close();
return true;
}*/
/*PrintWriter pw = new PrintWriter(new FileWriter(tree, true));
pw.append(input+"\n");
pw.close();
return true;*/
PrintWriter pw = new PrintWriter(new FileWriter(tree, true));
if(checkContents(tree).equals("")){
pw.append(input);//need some way to not have this extra \n
pw.close();
return true;
}
else {
pw.append("\n" + input);
pw.close();
return true;
}
}
return false;
}
private String checkContents(File tree2) throws IOException {
String contents = "";
BufferedReader br = new BufferedReader(new FileReader(tree2));
while(br.ready()) {
contents += "" + br.readLine();
}
br.close();
return contents;
}
//checks if input line appears in a file
private boolean entryExists(String inputLine, File tree2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(tree));
while(br.ready()) {
String str = br.readLine();
if(str.equals(inputLine)) {
br.close();
return true;
}
}
br.close();
return false;
}
public boolean deleteTree(String input) throws Exception {
/*Blob bl = new Blob(origFileName);
String newFileName = bl.getSha1();
String newEntry = origFileName + " : " + newFileName;*/
String dirName = "./objects/";
File dir = new File (dirName);
File inputFile = new File(dir,"tree");
File tempFile = new File(dir,"myTempFile.txt");
String lineToRemove = "";
if(!entryExists2(input, tree)) {
return false;
}
lineToRemove = findLine(input,tree);
//removing line
BufferedReader br = new BufferedReader(new FileReader(inputFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
/*while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}*/
String prevLine = br.readLine();
if(prevLine.equals(lineToRemove)) {
pw.print("");
}
else {
pw.print(prevLine);
}
while(br.ready()) {
if(!prevLine.equals(lineToRemove)) {
if(checkContents(tempFile).equals("")){
pw.print(prevLine);
}
else {
pw.print("\n" + prevLine);
}
}
prevLine = br.readLine();
}
if(!prevLine.equals(lineToRemove)) {
pw.print("\n" + prevLine);
}//what if prevLine = lineToRemove?
pw.close();
br.close();
boolean successful = tempFile.renameTo(inputFile);
return successful;
}
//checks if input appears in file
private boolean entryExists2(String input, File tree2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(tree));
while(br.ready()) {
String str = br.readLine();
if(str.contains(input)) {
br.close();
return true;
}
}
br.close();
return false;
}
private String findLine(String input, File tree2) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(tree));
while(br.ready()) {
String str = br.readLine();
if(str.contains(input)) {
br.close();
return str;
}
}
br.close();
throw new Exception("line not found", null);
}
}