-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.java
More file actions
191 lines (152 loc) · 6.42 KB
/
Sample.java
File metadata and controls
191 lines (152 loc) · 6.42 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.io.File;
public class Sample extends Utility {
public Sample(Editor e) {
super(e);
set_username_password();
}
@Override
public void create(String file_name, String user_name, String password) throws Exception {
dir = new File(file_name);
dir.mkdirs();
File meta = new File(dir, "0");
String toWrite = "";
toWrite = "0\n"; //length of the file
toWrite += user_name; //add username
//padding
while (toWrite.length() < Config.BLOCK_SIZE) {
toWrite += '\0';
}
save_to_file(toWrite.getBytes(), meta);
return;
}
@Override
public String findUser(String file_name) throws Exception {
File file = new File(file_name);
File meta = new File(file, "0");
String s = byteArray2String(read_from_file(meta));
String[] strs = s.split("\n");
return strs[1];
}
@Override
public int length(String file_name, String password) throws Exception {
File file = new File(file_name);
File meta = new File(file, "0");
String s = byteArray2String(read_from_file(meta));
String[] strs = s.split("\n");
return Integer.parseInt(strs[0]);
}
@Override
public byte[] read(String file_name, int starting_position, int len, String password) throws Exception {
File root = new File(file_name);
int file_length = length(file_name, password);
if (starting_position + len > file_length) {
throw new Exception();
}
int start_block = starting_position / Config.BLOCK_SIZE;
int end_block = (starting_position + len) / Config.BLOCK_SIZE;
String toReturn = "";
for (int i = start_block + 1; i <= end_block + 1; i++) {
String temp = byteArray2String(read_from_file(new File(root, Integer.toString(i))));
if (i == end_block + 1) {
temp = temp.substring(0, starting_position + len - end_block * Config.BLOCK_SIZE);
}
if (i == start_block + 1) {
temp = temp.substring(starting_position - start_block * Config.BLOCK_SIZE);
}
toReturn += temp;
}
return toReturn.getBytes("UTF-8");
}
@Override
public void write(String file_name, int starting_position, byte[] content, String password) throws Exception {
String str_content = byteArray2String(content);
File root = new File(file_name);
int file_length = length(file_name, password);
if (starting_position > file_length) {
throw new Exception();
}
int len = str_content.length();
int start_block = starting_position / Config.BLOCK_SIZE;
int end_block = (starting_position + len) / Config.BLOCK_SIZE;
for (int i = start_block + 1; i <= end_block + 1; i++) {
int sp = (i - 1) * Config.BLOCK_SIZE - starting_position;
int ep = (i) * Config.BLOCK_SIZE - starting_position;
String prefix = "";
String postfix = "";
if (i == start_block + 1 && starting_position != start_block * Config.BLOCK_SIZE) {
prefix = byteArray2String(read_from_file(new File(root, Integer.toString(i))));
prefix = prefix.substring(0, starting_position - start_block * Config.BLOCK_SIZE);
sp = Math.max(sp, 0);
}
if (i == end_block + 1) {
File end = new File(root, Integer.toString(i));
if (end.exists()) {
postfix = byteArray2String(read_from_file(new File(root, Integer.toString(i))));
if (postfix.length() > starting_position + len - end_block * Config.BLOCK_SIZE) {
postfix = postfix.substring(starting_position + len - end_block * Config.BLOCK_SIZE);
} else {
postfix = "";
}
}
ep = Math.min(ep, len);
}
String toWrite = prefix + str_content.substring(sp, ep) + postfix;
while (toWrite.length() < Config.BLOCK_SIZE) {
toWrite += '\0';
}
save_to_file(toWrite.getBytes(), new File(root, Integer.toString(i)));
}
//update meta data
if (content.length + starting_position > length(file_name, password)) {
String s = byteArray2String(read_from_file(new File(root, "0")));
String[] strs = s.split("\n");
strs[0] = Integer.toString(content.length + starting_position);
String toWrite = "";
for (String t : strs) {
toWrite += t + "\n";
}
while (toWrite.length() < Config.BLOCK_SIZE) {
toWrite += '\0';
}
save_to_file(toWrite.getBytes(), new File(root, "0"));
}
}
@Override
public boolean check_integrity(String file_name, String password) {
return false;
}
@Override
public void cut(String file_name, int len, String password) throws Exception {
File root = new File(file_name);
int file_length = length(file_name, password);
if (len > file_length) {
throw new Exception();
}
int end_block = (len) / Config.BLOCK_SIZE;
File file = new File(root, Integer.toString(end_block + 1));
String str = byteArray2String(read_from_file(file));
str = str.substring(0, len - end_block * Config.BLOCK_SIZE);
while (str.length() < Config.BLOCK_SIZE) {
str += '\0';
}
save_to_file(str.getBytes(), file);
int cur = end_block + 2;
file = new File(root, Integer.toString(cur));
while (file.exists()) {
file.delete();
cur++;
}
//update meta data
String s = byteArray2String(read_from_file(new File(root, "0")));
String[] strs = s.split("\n");
strs[0] = Integer.toString(len);
String toWrite = "";
for (String t : strs) {
toWrite += t + "\n";
}
while (toWrite.length() < Config.BLOCK_SIZE) {
toWrite += '\0';
}
save_to_file(toWrite.getBytes(), new File(root, "0"));
}
}