forked from m4rkyma/Git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlob.java
More file actions
73 lines (72 loc) · 2.37 KB
/
Blob.java
File metadata and controls
73 lines (72 loc) · 2.37 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Blob{
public static String Sha1(String input)
{
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void writeFile (String str, String fileName) throws IOException{
FileWriter fw = new FileWriter (fileName);
fw.write(str);
fw.close();
}
public static String readFile (String fileName) throws IOException{
FileReader fr = new FileReader (fileName);
StringBuilder s = new StringBuilder ();
while (fr.ready())
{
s.append((char)fr.read());
}
fr.close();
return s.toString();
}
public Blob (String fileName) throws IOException
{
String contents = readFile(fileName);
File f = new File ("objects/"+Sha1(contents));
f.createNewFile();
FileWriter fw = new FileWriter (f);
fw.write(contents);
fw.close();
// BufferedWriter writer = new BufferedWriter(f);
// writer.append("poop");
// writeFile (contents, Sha1(contents));
// File objects = new File("objects");
// objects.mkdir();
// File file = new File (objects, fileName);
}
public static String toSha1 (String fileName) throws IOException
{
String contents = readFile(fileName);
return Sha1(contents);
}
public static String getContents (String fileName) throws IOException
{
String contents = readFile(fileName);
return contents;
}
public static void main(String[] args) throws IOException {
System.out.println(Sha1("10101"));
// convertToBlob("doesn'tmatter.txt");
System.out.println(toSha1("doesn'tmatter.txt"));
}
}