下面列出了java.io.RandomAccessFile#writeLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(expected = BadCheckpointException.class)
public void testCheckpointVersionNotEqualToMeta() throws Exception {
RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
try {
EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.
get(checkpoint, 10, "test");
backingStore.close();
writer.seek(
EventQueueBackingStoreFile.INDEX_VERSION
* Serialization.SIZE_OF_LONG);
writer.writeLong(2L);
writer.getFD().sync();
backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
} finally {
writer.close();
}
}
private static Path createBrokenMetadata(Path valid) throws Exception {
try {
Path broken = Utils.createTempFile("broken-metadata", ".jfr");
Files.delete(broken);
Files.copy(valid, broken);
RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
raf.seek(METADATA_OFFSET);
long metadataOffset = raf.readLong();
raf.seek(metadataOffset);
raf.writeLong(Long.MAX_VALUE);
raf.writeLong(Long.MAX_VALUE);
raf.close();
return broken;
} catch (IOException ioe) {
throw new Exception("Could not produce a broken EventSet from file " + valid, ioe);
}
}
private static Path createBrokenMetadata(Path valid) throws Exception {
try {
Path broken = Utils.createTempFile("broken-metadata", ".jfr");
Files.delete(broken);
Files.copy(valid, broken);
RandomAccessFile raf = new RandomAccessFile(broken.toFile(), "rw");
raf.seek(METADATA_OFFSET);
long metadataOffset = raf.readLong();
raf.seek(metadataOffset);
raf.writeLong(Long.MAX_VALUE);
raf.writeLong(Long.MAX_VALUE);
raf.close();
return broken;
} catch (IOException ioe) {
throw new Exception("Could not produce a broken EventSet from file " + valid, ioe);
}
}
@Test (expected = InvalidProtocolBufferException.class)
public void testCorruptMeta() throws Throwable {
EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.
get(checkpoint, 10, "test");
backingStore.close();
Assert.assertTrue(checkpoint.exists());
File metaFile = Serialization.getMetaDataFile(checkpoint);
Assert.assertTrue(metaFile.length() != 0);
RandomAccessFile writer = new RandomAccessFile(metaFile, "rw");
writer.seek(10);
writer.writeLong(new Random().nextLong());
writer.getFD().sync();
writer.close();
try {
backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
} catch (BadCheckpointException ex) {
throw ex.getCause();
}
}
@Test (expected = BadCheckpointException.class)
public void testCheckpointBadVersion() throws Exception {
RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
try {
EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.
get(checkpoint, 10, "test");
backingStore.close();
writer.seek(
EventQueueBackingStoreFile.INDEX_VERSION * Serialization.SIZE_OF_LONG);
writer.writeLong(94L);
writer.getFD().sync();
backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
} finally {
writer.close();
}
}
@Test(expected = BadCheckpointException.class)
public void testIncompleteCheckpoint() throws Exception {
RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
try {
EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.
get(checkpoint, 10, "test");
backingStore.close();
writer.seek(
EventQueueBackingStoreFile.INDEX_CHECKPOINT_MARKER *
Serialization.SIZE_OF_LONG);
writer.writeLong(EventQueueBackingStoreFile.CHECKPOINT_INCOMPLETE);
writer.getFD().sync();
backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
} finally {
writer.close();
}
}
/**
* To save the current timestamp in incremental cache.
*
* @param folder
*/
public static void saveCurrentTimestamp( String folder )
throws DataException
{
try
{
RandomAccessFile raf = FileSecurity.createRandomAccessFile( folder
+ PATH_SEP + IncreDataSetCacheObject.TIMESTAMP_DATA, "rw" );
Calendar calendar = Calendar.getInstance( );
raf.writeLong( calendar.getTimeInMillis( ) );
raf.close( );
}
catch ( Exception e )
{
throw new DataException( e.getMessage( ) );
}
}
@Test
public void testValidateEditLogWithCorruptHeader() throws IOException {
File testDir = new File(TEST_DIR, "testValidateEditLogWithCorruptHeader");
SortedMap<Long, Long> offsetToTxId = Maps.newTreeMap();
File logFile = prepareUnfinalizedTestEditLog(testDir, 2, offsetToTxId);
RandomAccessFile rwf = new RandomAccessFile(logFile, "rw");
try {
rwf.seek(0);
rwf.writeLong(42); // corrupt header
} finally {
rwf.close();
}
EditLogValidation validation = EditLogFileInputStream.validateEditLog(logFile);
assertTrue(validation.hasCorruptHeader());
}
@Override
void markCheckpoint(long currentPosition, long logWriteOrderID)
throws IOException {
RandomAccessFile writeFileHandle = getFileHandle();
writeFileHandle.seek(OFFSET_CHECKPOINT);
writeFileHandle.writeLong(currentPosition);
writeFileHandle.writeLong(logWriteOrderID);
writeFileHandle.getChannel().force(true);
LOGGER.info("Noted checkpoint for file: " + getFile() + ", id: "
+ getLogFileID() + ", checkpoint position: " + currentPosition
+ ", logWriteOrderID: " + logWriteOrderID);
}
protected void write(RandomAccessFile raf) throws IOException {
raf.writeShort(_fhSize);
raf.writeInt(_pageSize);
raf.writeLong(_totalPageCount);
raf.writeLong(_firstFreePage);
raf.writeLong(_lastFreePage);
raf.writeByte(_pageHeaderSize);
}
@Test
public void testValidateEditLogWithCorruptHeader() throws IOException {
File testDir = new File(TEST_DIR, "testValidateEditLogWithCorruptHeader");
SortedMap<Long, Long> offsetToTxId = Maps.newTreeMap();
File logFile = prepareUnfinalizedTestEditLog(testDir, 2, offsetToTxId);
RandomAccessFile rwf = new RandomAccessFile(logFile, "rw");
try {
rwf.seek(0);
rwf.writeLong(42); // corrupt header
} finally {
rwf.close();
}
EditLogValidation validation = EditLogFileInputStream.validateEditLog(logFile);
assertTrue(validation.hasCorruptHeader());
}
Writer(File file, int logFileID, long maxFileSize,
long usableSpaceRefreshInterval)
throws IOException {
super(file, logFileID, maxFileSize, null, usableSpaceRefreshInterval);
RandomAccessFile writeFileHandle = getFileHandle();
writeFileHandle.writeInt(getVersion());
writeFileHandle.writeInt(logFileID);
// checkpoint marker
writeFileHandle.writeLong(0L);
// timestamp placeholder
writeFileHandle.writeLong(0L);
getFileChannel().force(true);
}
/**
* Write out the FeatureVector to a file at the current offset, using the working buffer. The buffer
* should be pre-sized using getSerializationSize(), or it can be null, in which case it will be allocated
*
* @param output A file open at the desired write offset
* @param buffer A working buffer of at least the required number of bytes or null
* @throws IOException
*/
public void serializeTo(RandomAccessFile output, byte[] buffer) throws IOException
{
UnsafeMemory memory = serialize(buffer);
// Write bytes out
long sz = memory.getPos();
output.writeLong(sz);
output.write(memory.getBuffer(), 0, (int) sz);
}
/**
* java.io.RandomAccessFile#writeLong(long)
*/
public void test_writeLongJ() throws IOException {
// Test for method void java.io.RandomAccessFile.writeLong(long)
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeLong(Long.MAX_VALUE);
raf.seek(0);
assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
.readLong());
raf.close();
}
private void testFastReplay(boolean shouldCorruptCheckpoint,
boolean useFastReplay) throws Exception{
Map<String, String> overrides = Maps.newHashMap();
overrides.put(FileChannelConfiguration.USE_FAST_REPLAY,
String.valueOf(useFastReplay));
channel = createFileChannel(overrides);
channel.start();
Assert.assertTrue(channel.isOpen());
Set<String> in = putEvents(channel, "restart", 10, 100);
Assert.assertEquals(100, in.size());
forceCheckpoint(channel);
channel.stop();
if (shouldCorruptCheckpoint) {
File checkpoint = new File(checkpointDir, "checkpoint");
RandomAccessFile writer = new RandomAccessFile(
Serialization.getMetaDataFile(checkpoint), "rw");
writer.seek(10);
writer.writeLong(new Random().nextLong());
writer.getFD().sync();
writer.close();
}
channel = createFileChannel(overrides);
channel.start();
Assert.assertTrue(channel.isOpen());
Set<String> out = consumeChannel(channel);
if (useFastReplay && shouldCorruptCheckpoint) {
Assert.assertTrue(channel.didFastReplay());
} else {
Assert.assertFalse(channel.didFastReplay());
}
compareInputAndOut(in, out);
}
@Override
public void serialize(RandomAccessFile randomAccessFile) throws IOException {
super.serialize(randomAccessFile);
randomAccessFile.writeLong(fRoot);
randomAccessFile.writeInt(fSubVersion);
}
/**
*
* Leaf node write structure is as follows:
*
* -- node type -- (2 bytes)
* -- next pointer -- (8 bytes)
* -- prev pointer -- (8 bytes)
* -- key/value pairs -- (max size * (key size + satellite size))
*
* @param r pointer to *opened* B+ tree file
* @param conf configuration parameter
* @throws IOException is thrown when an I/O operation fails
*/
@Override
public void writeNode(RandomAccessFile r, BPlusConfiguration conf,
BPlusTreePerformanceCounter bPerf)
throws IOException {
// update root index in the file
if(this.isRoot()) {
r.seek(conf.getHeaderSize()-16L);
r.writeLong(getPageIndex());
}
// account for the header page as well.
r.seek(getPageIndex());
// now write the node type
r.writeShort(getPageType());
// write the next pointer
r.writeLong(nextPagePointer);
// write the prev pointer
r.writeLong(prevPagePointer);
// then write the current capacity
r.writeInt(getCurrentCapacity());
// now write the Key/Value pairs
for(int i = 0; i < getCurrentCapacity(); i++) {
r.writeLong(getKeyAt(i));
r.writeLong(getOverflowPointerAt(i));
r.write(valueList.get(i).getBytes(StandardCharsets.UTF_8));
}
// annoying correction
if(r.length() < getPageIndex()+conf.getPageSize())
{r.setLength(getPageIndex()+conf.getPageSize());}
bPerf.incrementTotalLeafNodeWrites();
}
static void writeStrings(String filename, Iterable<String> strGenerator) throws IOException {
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
raf.setLength(0);
ArrayList<Long> offsetttable = new ArrayList<Long>();
for(String s: strGenerator) {
offsetttable.add(raf.getFilePointer());
raf.writeUTF(s);
}
for(long offset: offsetttable)
raf.writeLong(offset);
raf.writeInt(offsetttable.size());
raf.close();
}
void testBrokenTrailer(Case type) throws Exception {
System.out.println("Testing: case " + type);
// Create a fresh .jar file
File testFile = new File("src_tools.jar");
File testPack = new File("src_tools.pack.gz");
generateJar(testFile);
cmdsList.clear();
// Create .pack file
cmdsList.add(Utils.getPack200Cmd());
cmdsList.add(testPack.getName());
cmdsList.add(testFile.getName());
Utils.runExec(cmdsList);
// mutate the checksum of the packed file
RandomAccessFile raf = new RandomAccessFile(testPack, "rw");
switch (type) {
case CRC32:
raf.seek(raf.length() - TRAILER_LEN);
raf.writeInt(0x0dea0a0d);
break;
case ISIZE:
raf.seek(raf.length() - (TRAILER_LEN/2));
raf.writeInt(0x0b0e0e0f);
break;
default:
raf.seek(raf.length() - (TRAILER_LEN));
raf.writeLong(0x0dea0a0d0b0e0e0fL);
break;
}
raf.close();
File dstFile = new File("dst_tools.jar");
if (dstFile.exists()) {
dstFile.delete();
}
cmdsList.clear();
cmdsList.add(Utils.getUnpack200Cmd());
cmdsList.add(testPack.getName());
cmdsList.add(dstFile.getName());
boolean processFailed = false;
try {
Utils.runExec(cmdsList);
} catch (RuntimeException re) {
// unpack200 should exit with non-zero exit code
processFailed = true;
} finally {
// tidy up
if (testFile.exists())
testFile.delete();
if (testPack.exists())
testPack.delete();
if (!processFailed) {
throw new Exception("case " + type +
": file with incorrect CRC, unpacked without the error.");
}
if (dstFile.exists()) {
dstFile.delete();
throw new Exception("case " + type +
": file exists: " + dstFile);
}
}
}
protected EventQueueBackingStoreFile(int capacity, String name,
File checkpointFile, File checkpointBackupDir,
boolean backupCheckpoint) throws IOException,
BadCheckpointException {
super(capacity, name);
this.checkpointFile = checkpointFile;
this.shouldBackup = backupCheckpoint;
this.backupDir = checkpointBackupDir;
checkpointFileHandle = new RandomAccessFile(checkpointFile, "rw");
long totalBytes = (capacity + HEADER_SIZE) * Serialization.SIZE_OF_LONG;
if(checkpointFileHandle.length() == 0) {
allocate(checkpointFile, totalBytes);
checkpointFileHandle.seek(INDEX_VERSION * Serialization.SIZE_OF_LONG);
checkpointFileHandle.writeLong(getVersion());
checkpointFileHandle.getChannel().force(true);
LOG.info("Preallocated " + checkpointFile + " to " + checkpointFileHandle.length()
+ " for capacity " + capacity);
}
if(checkpointFile.length() != totalBytes) {
String msg = "Configured capacity is " + capacity + " but the "
+ " checkpoint file capacity is " +
((checkpointFile.length() / Serialization.SIZE_OF_LONG) - HEADER_SIZE)
+ ". See FileChannel documentation on how to change a channels" +
" capacity.";
throw new BadCheckpointException(msg);
}
mappedBuffer = checkpointFileHandle.getChannel().map(MapMode.READ_WRITE, 0,
checkpointFile.length());
elementsBuffer = mappedBuffer.asLongBuffer();
long version = elementsBuffer.get(INDEX_VERSION);
if(version != (long) getVersion()) {
throw new BadCheckpointException("Invalid version: " + version + " " +
name + ", expected " + getVersion());
}
long checkpointComplete = elementsBuffer.get(INDEX_CHECKPOINT_MARKER);
if(checkpointComplete != (long) CHECKPOINT_COMPLETE) {
throw new BadCheckpointException("Checkpoint was not completed correctly,"
+ " probably because the agent stopped while the channel was"
+ " checkpointing.");
}
if (shouldBackup) {
checkpointBackUpExecutor = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat(
getName() + " - CheckpointBackUpThread").build());
} else {
checkpointBackUpExecutor = null;
}
}