-
Notifications
You must be signed in to change notification settings - Fork 0
/
VFS.java
62 lines (57 loc) · 1.77 KB
/
VFS.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
import java.util.HashMap;
public class VFS implements Device
{
private static VFS vfs = null;
private int deviceIdGenerator; //Ensures device Ids don't repeat
public HashMap<Integer,DeviceData> devices = new HashMap<Integer,DeviceData>();
private VFS()
{
}
public static VFS getVFS()
{
if(vfs == null)
{
vfs = new VFS();
}
return vfs;
}
public int open(String s)
{
String[] args = s.split(" ");
switch(args[0])
{
case "random":
RandomDevice randomDevice = RandomDevice.getRandomDevice();
devices.put(deviceIdGenerator, new DeviceData(randomDevice, randomDevice.open(args[1])));
break;
case "pipe":
PipeDevice pipeDevice = PipeDevice.getPipeDevice();
devices.put(deviceIdGenerator, new DeviceData(pipeDevice, pipeDevice.open(args[1])));
break;
case "file":
FakeFileSystem fileDevice = FakeFileSystem.getFileSystem();
devices.put(deviceIdGenerator, new DeviceData(fileDevice, fileDevice.open(args[1])));
break;
}
int returnVal = deviceIdGenerator;
deviceIdGenerator++;
return returnVal;
}
public void close(int id)
{
devices.get(id).device.close(devices.get(id).getDeviceId());
devices.remove(id);
}
public byte[] read(int id, int size)
{
return devices.get(id).device.read(devices.get(id).deviceId, size);
}
public void seek(int id, int to)
{
devices.get(id).device.seek(devices.get(id).deviceId, to);
}
public int write(int id, byte[] data)
{
return devices.get(id).device.write(devices.get(id).deviceId, data);
}
}