-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAT.java
More file actions
113 lines (94 loc) · 2.54 KB
/
FAT.java
File metadata and controls
113 lines (94 loc) · 2.54 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
package OperatingSystem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
public class FAT {
private String DIVIDER = "~";
private int[] arrayFAT;
private int FREE = -2;
private java.io.File file;
public FAT(int size) {
arrayFAT = new int[size];
for (int i = 0; i < arrayFAT.length; i++)
arrayFAT[i] = FREE;
file = new java.io.File("FILE_SYSTEM/persistentFAT.txt");
try {
if (!file.exists()) {
file.createNewFile();
} else {
String content = FileUtils.readFileToString(file);
for (int i = 0; i < content.split(DIVIDER).length; i++)
arrayFAT[i] = Integer.parseInt(content.split(DIVIDER)[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/* Get the first free block in our mainMemory */
public int getFreeBlock() {
for (int i = 0; i < arrayFAT.length; i++) {
if (arrayFAT[i] == FREE) {
return i;
}
}
return -1;
}
/* Get the second free block in our mainMemory */
public int getNextFreeBlock() {
boolean found = false;
for (int i = 0; i < arrayFAT.length; i++) {
if ((arrayFAT[i] == FREE) && (found)) {
return i;
} else if (arrayFAT[i] == FREE && (!found)) {
found = true;
}
}
return -1;
}
public void setFree(int position) {
setElement(position, FREE);
}
public void setElement(int position, int pointer) {
arrayFAT[position] = pointer;
StringBuilder builder = new StringBuilder();
for (int s : arrayFAT)
builder.append(s + DIVIDER);
builder.deleteCharAt(builder.length() - 1); /* delete last DIVIDER */
try {
FileUtils.write(file, builder.toString(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
public int getElement(int position) {
return arrayFAT[position];
}
public int getTotalFreeSpace() {
int count = 0;
for (int i = 0; i < arrayFAT.length; i++) {
if (arrayFAT[i] == FREE)
count++;
}
return count;
}
public void drawFAT() {
for (int i = 0; i < arrayFAT.length; i++) {
System.out.print("[" + arrayFAT[i] + "] ");
}
System.out.println();
}
public void drawFAT(ArrayList<File> files) {
String print[] = new String[arrayFAT.length];
for (int i = 0; i < print.length; i++)
print[i] = "-";
for (File file : files) {
int next = file.getStartingAddress();
while (next != -1) {
print[next] = file.getName();
next = getElement(next);
}
}
System.out.println(Arrays.toString(print));
}
}