-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patholdTree2.java
More file actions
296 lines (252 loc) · 8.2 KB
/
oldTree2.java
File metadata and controls
296 lines (252 loc) · 8.2 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.io.BufferedReader;
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.ArrayList;
import java.util.Formatter;
public class oldTree2 {
private File tree;
//might need a hashmap
private ArrayList<String> blobList;
private ArrayList<String> treeList;
public oldTree2 () {
blobList = new ArrayList<String>();
treeList = new ArrayList<String>();
}
public void initialize() throws IOException {
File objects = new File("./objects");
if (!objects.exists())
objects.mkdirs();
//String dirName = "./objects/";
//File dir = new File (dirName);
//dir.mkdir();
tree = new File ("tree");
//tree = new File ("tree");
if(!tree.exists()) {
tree.createNewFile();
}
}
public void saveToObjects() throws IOException {
rename(tree);
}
public void rename(File fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String str = "";
while(br.ready()) {
str += br.readLine()+"\n";
}
str = str.trim();//get rid of extra line
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();
File newFile = new File (dir, sha1);
PrintWriter pw = new PrintWriter(newFile);
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)) {
//PrintWriter pw = new PrintWriter(new FileWriter(tree));
String type = "" + input.substring(0,4);
if(type.equals("blob")) {
blobList.add(input);
}
else if(type.equals("tree")) {
treeList.add(input);
}
printList(tree);
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(tree2));
while(br.ready()) {
String str = br.readLine();
if(str.equals(inputLine)) {
br.close();
return true;
}
}
br.close();
return false;
/*String type = inputLine.substring(0,4);
if(type.equals("blob")) {
for(int i=0; i<blobList.size();i ++) {
String str = blobList.get(i);
if(str.equals(inputLine)) {
return true;
}
}
}
else {
for(int i=0; i<treeList.size();i ++) {
String str = treeList.get(i);
if(str.equals(inputLine)) {
return true;
}
}
}
return false;*/
}
private void printList(File fileName) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter(fileName));
for(int i=0; i<blobList.size(); i++) {
String str = blobList.get(i);
if(treeList.size()!=0 && i==blobList.size()-1) {
pw.println(str);
}
else if(i!=blobList.size()-1) {
pw.println(str);
}
else {
pw.print(str);
}
}
for(int i=0; i<treeList.size(); i++) {
String str = treeList.get(i);
if(i!=treeList.size()-1) {
pw.println(str);
}
else {
pw.print(str);
}
}
pw.close();
}
public boolean deleteTree(String input) throws Exception {
//String dirName = "./objects/";
//File dir = new File (dirName);
File inputFile = new File("tree");
File tempFile = new File("myTempFile.txt");
String lineToRemove = "";
if(!entryExists2(input, tree)) {
return false;
}
lineToRemove = findLine(input,tree);
System.out.println("remove: " + lineToRemove);//test: correct
String type = lineToRemove.substring(0,4);
if(type.equals("blob")) {
blobList.remove(lineToRemove);
}
else if(type.equals("tree")) {
treeList.remove(lineToRemove);
}
printList(tempFile);
//removing line
//BufferedReader br = new BufferedReader(new FileReader(inputFile));
/*PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
for(int i=0; i<blobList.size(); i++) {
String str = blobList.get(i);
if(!str.equals(lineToRemove)) {
if(treeList.size()==0 && i==blobList.size()-2 && blobList.get(i+1).equals(lineToRemove)) {
pw.print(str);
}
else if(i==blobList.size()-1 && treeList.size()==0) {
pw.print(str);
}
else if(i==blobList.size()-1 && treeList.size()>0) {
pw.println(str);
}
else if(i!=blobList.size()-1) {
pw.println(str);
}
}
}
for(int i=0; i<treeList.size(); i++) {
String str = treeList.get(i);
if(!str.equals(lineToRemove)) {
if(i==treeList.size()-2 && treeList.get(i+1).equals(lineToRemove)) {
pw.print(str);
}
else if(i==treeList.size()-1) {
pw.print(str);
}
else {
pw.println(str);
}
}
}
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);
}
}