-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeFileSystem.java
More file actions
82 lines (76 loc) · 2.01 KB
/
FakeFileSystem.java
File metadata and controls
82 lines (76 loc) · 2.01 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
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class FakeFileSystem implements Device{
private RandomAccessFile[] files = new RandomAccessFile[10];
// Open goes through files until the next null is found, initializes it with string s and "rw", and then returns the
// index of which it belongs to.
@Override
public int Open(String s){
try{
for(int i = 0; i < files.length; i++){
if(files[i] == null)
files[i] = new RandomAccessFile(s,"rw");
return i;
}
}
catch(Exception e){
e.printStackTrace();
}
return -1;
}
@Override
public void Close(int id) {
try {
if(files[id] != null){
files[id].close();
files[id] = null;
}
} catch(Exception e){
e.printStackTrace();
}
}
@Override
public byte[] Read(int id, int size) {
try {
byte[] finList = new byte[size];
files[id].read(finList,0,size);
return finList;
}
catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
@Override
public void Seek(int id, int to) {
try{
files[id].seek(to);
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public int Write(int id, byte[] data) {
try{
files[id].write(data);
return data.length;
}
catch(Exception e){
e.printStackTrace();
}
return 0;
}
public void clear() {
for(int i = 0; i < files.length; i++){
if(files[i] != null){
try{
files[i].close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}
}