-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayFile.java
executable file
·44 lines (33 loc) · 942 Bytes
/
ArrayFile.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
// PART OF THE MACHINE SIMULATION. DO NOT CHANGE.
package nachos.machine;
/**
* A read-only <tt>OpenFile</tt> backed by a byte array.
*/
public class ArrayFile extends OpenFileWithPosition {
/**
* Allocate a new <tt>ArrayFile</tt>.
*
* @param array the array backing this file.
*/
public ArrayFile(byte[] array) {
this.array = array;
}
public int length() {
return array.length;
}
public void close() {
array = null;
}
public int read(int position, byte[] buf, int offset, int length) {
Lib.assertTrue(offset >= 0 && length >= 0 && offset+length <= buf.length);
if (position < 0 || position >= array.length)
return 0;
length = Math.min(length, array.length-position);
System.arraycopy(array, position, buf, offset, length);
return length;
}
public int write(int position, byte[] buf, int offset, int length) {
return 0;
}
private byte[] array;
}