FileSystem fs = FileSystem.getInstance(fsBase);
String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
file.getParentFile().mkdirs();
file.delete();
RandomAccessFile ra = new RandomAccessFile(file, "rw");
fs.delete(s);
FileObject f = fs.openFileObject(s, "rw");
assertThrows(EOFException.class, f).readFully(new byte[1], 0, 1);
f.sync();
Random random = new Random(seed);
int size = getSize(100, 500);
try {
for (int i = 0; i < size; i++) {
trace("op " + i);
int pos = random.nextInt(10000);
switch(random.nextInt(7)) {
case 0: {
pos = (int) Math.min(pos, ra.length());
trace("seek " + pos);
buff.append("seek " + pos + "\n");
f.seek(pos);
ra.seek(pos);
break;
}
case 1: {
byte[] buffer = new byte[random.nextInt(1000)];
random.nextBytes(buffer);
trace("write " + buffer.length);
buff.append("write " + buffer.length + "\n");
f.write(buffer, 0, buffer.length);
ra.write(buffer, 0, buffer.length);
break;
}
case 2: {
trace("setLength " + pos);
f.setFileLength(pos);
ra.setLength(pos);
if (ra.getFilePointer() > pos) {
f.seek(0);
ra.seek(0);
}
buff.append("setLength " + pos + "\n");
break;
}
case 3: {
int len = random.nextInt(1000);
len = (int) Math.min(len, ra.length() - ra.getFilePointer());
byte[] b1 = new byte[len];
byte[] b2 = new byte[len];
trace("readFully " + len);
ra.readFully(b1, 0, len);
f.readFully(b2, 0, len);
buff.append("readFully " + len + "\n");
assertEquals(b1, b2);
break;
}
case 4: {
trace("getFilePointer");
buff.append("getFilePointer\n");
assertEquals(ra.getFilePointer(), f.getFilePointer());
break;
}
case 5: {
trace("length " + ra.length());
buff.append("length " + ra.length() + "\n");
assertEquals(ra.length(), f.length());
break;
}
case 6: {
trace("reopen");
buff.append("reopen\n");
f.close();
ra.close();
ra = new RandomAccessFile(file, "rw");
f = fs.openFileObject(s, "rw");
assertEquals(ra.length(), f.length());
break;
}
default:
}
}
} catch (Throwable e) {
e.printStackTrace();
fail("Exception: " + e + "\n"+ buff.toString());
} finally {
f.close();
ra.close();
file.delete();
fs.delete(s);
}
}