RandomAccessFile raf = new RandomAccessFile(blah, "rw");
FileChannel c = raf.getChannel();
for (int x=0; x<100; x++) { long offset = generator.nextInt(1000);
ByteBuffer bleck = ByteBuffer.allocateDirect(4);
// Write known sequence out for (byte i=0; i<4; i++) {
bleck.put(i);
}
bleck.flip(); long originalPosition = c.position(); int totalWritten = 0; while (totalWritten < 4) { int written = c.write(bleck, offset); if (written < 0) thrownew Exception("Read failed");
totalWritten += written;
}
long newPosition = c.position();
// Ensure that file pointer position has not changed if (originalPosition != newPosition) thrownew Exception("File position modified");
// Attempt to read sequence back in
bleck = ByteBuffer.allocateDirect(4);
originalPosition = c.position(); int totalRead = 0; while (totalRead < 4) { int read = c.read(bleck, offset); if (read < 0) thrownew Exception("Read failed");
totalRead += read;
}
newPosition = c.position();
// Ensure that file pointer position has not changed if (originalPosition != newPosition) thrownew Exception("File position modified");
for (byte i=0; i<4; i++) { if (bleck.get(i) != i) thrownew Exception("Write test failed");
}
}
c.close();
raf.close();
blah.delete();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.