Java Code Examples for java.io.RandomAccessFile#writeLong()

The following examples show how to use java.io.RandomAccessFile#writeLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRecordingFile.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: TestRecordingFile.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 3
Source File: TestEventQueueBackingStoreFactory.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 4
Source File: CacheUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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( ) );
	}
}
 
Example 5
Source File: TestEventQueueBackingStoreFactory.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 6
Source File: TestEventQueueBackingStoreFactory.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 7
Source File: TestEventQueueBackingStoreFactory.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 8
Source File: StringArrayFile.java    From learntosolveit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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();
}
 
Example 9
Source File: TreeLeaf.java    From BPlusTree with Apache License 2.0 5 votes vote down vote up
/**
 *
 * 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();
}
 
Example 10
Source File: BTree.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void serialize(RandomAccessFile randomAccessFile) throws IOException {
    super.serialize(randomAccessFile);

    randomAccessFile.writeLong(fRoot);
    randomAccessFile.writeInt(fSubVersion);
}
 
Example 11
Source File: TestFileChannelRestart.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: RandomAccessFileTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 13
Source File: FeatureVector.java    From sgdtk with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: LogFileV2.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
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);

}
 
Example 15
Source File: TestFSEditLogLoader.java    From big-c with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 16
Source File: Paged.java    From btree4j with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: LogFileV2.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@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);

}
 
Example 18
Source File: TestFSEditLogLoader.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 19
Source File: EventQueueBackingStoreFile.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
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;
  }
}
 
Example 20
Source File: PackChecksum.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
        }
    }
}