-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomDevice.java
More file actions
54 lines (48 loc) · 1.75 KB
/
RandomDevice.java
File metadata and controls
54 lines (48 loc) · 1.75 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
import java.util.Random;
public class RandomDevice implements Device{
// Stores the random objects within the device
private Random[] devices = new Random[10];
// Open parses a seed from the incoming string, if the string is not valid then it just calls currentTimeMillis as a seed for the next
// available null position suitable for a random object.
@Override
public int Open(String s) {
for(int i = 0; i < devices.length; i++){
if(devices[i] == null){
int seed = (s != null && !s.isEmpty()) ? Integer.parseInt(s) : (int) System.currentTimeMillis();
if (s != null && !s.isEmpty())
seed = Integer.parseInt(s);
else
seed = (int) System.currentTimeMillis();
devices[i] = new Random(seed);
return i;
}
}
return 0;
}
// Close nulls id of given index.
@Override
public void Close(int id) {
devices[id] = null;
}
// Read returns a byte[] of size utilizing r.nextBytes to fill the array with random values (all dependent off a seed).
@Override
public byte[] Read(int id, int size) {
if(size < 1)
return new byte[0];
Random r = devices[id];
byte[] bytes = new byte[size];
r.nextBytes(bytes);
return bytes;
}
// Seek goes to the random iteration and reads from the index of to within the random list.
@Override
public void Seek(int id, int to) {
byte[] bytes = Read(id, to);
System.out.println(bytes.toString());
}
// Do nothing.
@Override
public int Write(int id, byte[] data) {
return 0;
}
}