-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExt2File.java
More file actions
91 lines (76 loc) · 2.66 KB
/
Copy pathExt2File.java
File metadata and controls
91 lines (76 loc) · 2.66 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
import java.io.IOException;
public class Ext2File {
private long position; // the current position in the file
private Volume volume; // the volume
public Ext2File(Volume volume) {
this.volume = volume;
position = 0;
}
/**
* Reads the most length bytes starting at byte offset startByte from start of
* file. Byte 0 is the first byte in the file. StartByte must be such that, 0 <=
* startByte < file.size or an exception should be raised. If there are fewer
* than length bytes remaining these will be read and a smaller number of bytes
* than requested will be returned.
*/
public byte[] read(long startByte, long length) {
byte[] bytes = new byte[(int) length];
try {
volume.getRandomAccessFile().seek(startByte);
volume.getRandomAccessFile().read(bytes, 0, (int) length);
return bytes;
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Reads at most length bytes starting at current position in the file. If the
* current position is set beyond the end of the file, an exception should be
* raised. If there are fewer than length bytes remaining these will be read and
* a smaller number of bytes than requested will be returned.
*/
public byte[] read(long length) {
byte[] bytes = new byte[(int) length];
try {
volume.getRandomAccessFile().seek(position());
volume.getRandomAccessFile().read(bytes, 0, (int) length);
return bytes;
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Move to byte position in file. Setting position to 0: will move to the start
* of the file. Note, it is legal to seek beyond the end of the file; If writing
* were supported this is how holes are created.
*/
public void seek(long position) {
this.position = position;
}
/**
* Returns current position in file, i.e. the byte offset from the start of the
* file. The file position will be zero when the file is first opened and will
* advance by the number of bytes read with every call to one of the read()
* routines.
*
* @return long the current position in the file
*/
public long position() {
return position;
}
/**
* Returns the size of file as specified in filesystem
*
* @throws IOException
*/
public long size() throws IOException {
return volume.getRandomAccessFile().length();
}
public Volume getVolume() {
return volume;
}
}