-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlob.java
70 lines (57 loc) · 1.92 KB
/
Blob.java
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
package gitlet;
import java.io.File;
import java.io.Serializable;
/** This class simply is the representation for the content within a file.
* @author Parth Shisode */
public class Blob implements Serializable {
/** BLOB_FOLDER is a folder holding Blobs in /.gitlet. */
static final File BLOB_FOLDER = Utils.join(Main.DOT_GITLET, "blobs");
/** Create a Blob with these parameters.
* @param f FILE */
public Blob(File f) {
_info = Utils.readContents(f);
_bSha = Utils.sha1(_info);
_blobFile = f;
boolean c1 = Utils.plainFilenamesIn(BLOB_FOLDER) == null;
boolean saveCond = true;
if (c1) {
for (String blobID : Utils.plainFilenamesIn(BLOB_FOLDER)) {
Blob currB = Utils.readObject(Utils.join(BLOB_FOLDER,
blobID), Blob.class);
if (currB.getSha().equals(_bSha)
&& currB.getBlobFile().equals(_blobFile)) {
saveCond = false;
}
}
}
if (saveCond) {
saveBlob();
}
}
/** Creates a Blob within BLOB_FOLDER. */
public void saveBlob() {
File blobFile = Utils.join(BLOB_FOLDER, _bSha);
Utils.writeObject(blobFile, this);
}
/** @return byte[]
* Getter method for _info. */
public byte[] getInfo() {
return _info;
}
/** @return STRING
* Getter method for _bSha. */
public String getSha() {
return _bSha;
}
/** @return FILE
* Getter method for _blobFile. */
public File getBlobFile() {
return _blobFile;
}
/** Represents the SHA1 hash string associated with the Blob's file. */
private final String _bSha;
/** Represents the actual contents of the Blob's file as a byte[]. */
private final byte[] _info;
/** Represents the File this Blob is associated with. */
private final File _blobFile;
}