Example usage for java.io RandomAccessFile writeLong

List of usage examples for java.io RandomAccessFile writeLong

Introduction

In this page you can find the example usage for java.io RandomAccessFile writeLong.

Prototype

public final void writeLong(long v) throws IOException 

Source Link

Document

Writes a long to the file as eight bytes, high byte first.

Usage

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

private void doTestBadCheckpointVersion(boolean backup) throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    overrides.put(FileChannelConfiguration.USE_DUAL_CHECKPOINTS, String.valueOf(backup));
    channel = createFileChannel(overrides);
    channel.start();// ww w .j a  v a 2s  .  c o  m
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    if (backup) {
        Thread.sleep(2000);
    }
    channel.stop();
    File checkpoint = new File(checkpointDir, "checkpoint");
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    writer.seek(EventQueueBackingStoreFile.INDEX_VERSION * Serialization.SIZE_OF_LONG);
    writer.writeLong(2L);
    writer.getFD().sync();
    writer.close();
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(!backup || channel.checkpointBackupRestored());
    Set<String> out = consumeChannel(channel);
    compareInputAndOut(in, out);
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

private void doTestIncompleteCheckpoint(boolean backup) throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    overrides.put(FileChannelConfiguration.USE_DUAL_CHECKPOINTS, String.valueOf(backup));
    channel = createFileChannel(overrides);
    channel.start();//from  w w w . ja  va2 s .  c o  m
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    if (backup) {
        Thread.sleep(2000);
    }
    channel.stop();
    File checkpoint = new File(checkpointDir, "checkpoint");
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    writer.seek(EventQueueBackingStoreFile.INDEX_CHECKPOINT_MARKER * Serialization.SIZE_OF_LONG);
    writer.writeLong(EventQueueBackingStoreFile.CHECKPOINT_INCOMPLETE);
    writer.getFD().sync();
    writer.close();
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(!backup || channel.checkpointBackupRestored());
    Set<String> out = consumeChannel(channel);
    compareInputAndOut(in, out);
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

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();/*w w w. ja v  a  2  s .  c  o m*/
    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);
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

private void doTestCorruptCheckpointMeta(boolean backup) throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    overrides.put(FileChannelConfiguration.USE_DUAL_CHECKPOINTS, String.valueOf(backup));
    channel = createFileChannel(overrides);
    channel.start();//from  w  w  w. j a v  a 2 s. c  o m
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    if (backup) {
        Thread.sleep(2000);
    }
    channel.stop();
    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());
    Assert.assertTrue(!backup || channel.checkpointBackupRestored());
    Set<String> out = consumeChannel(channel);
    compareInputAndOut(in, out);
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageTestUtil.java

/**
 * Calculate the md5sum of an image after zeroing out the transaction ID
 * field in the header. This is useful for tests that want to verify
 * that two checkpoints have identical namespaces.
 *///from   w  ww . j  a  va2  s .c  om
public static String getImageFileMD5IgnoringTxId(File imageFile) throws IOException {
    File tmpFile = File.createTempFile("hadoop_imagefile_tmp", "fsimage");
    tmpFile.deleteOnExit();
    try {
        Files.copy(imageFile, tmpFile);
        RandomAccessFile raf = new RandomAccessFile(tmpFile, "rw");
        try {
            raf.seek(IMAGE_TXID_POS);
            raf.writeLong(0);
        } finally {
            IOUtils.closeStream(raf);
        }
        return getFileMD5(tmpFile);
    } finally {
        tmpFile.delete();
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestFSEditLogLoader.java

@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 {/*from ww  w .  jav a2s .  co m*/
        rwf.seek(0);
        rwf.writeLong(42); // corrupt header
    } finally {
        rwf.close();
    }
    EditLogValidation validation = EditLogFileInputStream.validateEditLog(logFile);
    assertTrue(validation.hasCorruptHeader());
}

From source file:org.commoncrawl.service.crawler.CrawlList.java

private static void appendTargetsToLogFile(File logFileName, IntrusiveList<CrawlTarget> list)
        throws IOException {

    LogFileHeader header = new LogFileHeader();

    boolean preExistingHeader = logFileName.exists();

    RandomAccessFile file = new RandomAccessFile(logFileName, "rw");

    try {/*from  ww  w .ja va2  s.com*/
        long headerOffset = 0;

        if (preExistingHeader) {

            headerOffset = readLogFileHeader(file, header);

            if (header._writePos == 0) {
                file.seek(headerOffset);
            } else {
                // seelk to appropriate write position 
                file.seek(header._writePos);
            }
        } else {
            headerOffset = writeLogFileHeader(file, header);
        }

        CustomByteArrayOutputStream bufferOutputStream = new CustomByteArrayOutputStream(1 << 17);
        DataOutputStream dataOutputStream = new DataOutputStream(bufferOutputStream);
        CRC32 crc = new CRC32();

        for (CrawlTarget target : list) {

            PersistentCrawlTarget persistentTarget = target.createPersistentTarget();

            bufferOutputStream.reset();
            // write to intermediate stream ... 
            persistentTarget.write(dataOutputStream);
            // and crc the data ... 
            crc.reset();
            crc.update(bufferOutputStream.getBuffer(), 0, bufferOutputStream.size());
            // write out length first 
            file.writeInt(bufferOutputStream.size());
            //crc next
            long computedValue = crc.getValue();
            //TODO: waste of space - write 32 bit values as long because having problems with java sign promotion rules during read...
            file.writeLong(computedValue);
            // and then the data 
            file.write(bufferOutputStream.getBuffer(), 0, bufferOutputStream.size());
        }

        // now update header ... 
        header._itemCount += list.size();
        header._writePos = file.getFilePointer();

        // now write out header anew ... 
        writeLogFileHeader(file, header);

    } finally {
        if (file != null) {
            file.close();
        }
    }
}

From source file:VASSAL.launch.ModuleManager.java

public static void main(String[] args) {
    // FIXME: We need to catch more exceptions in main() and then exit in
    // order to avoid situations where the main thread ends due to an uncaught
    // exception, but there are other threads still running, and so VASSAL
    // does not quit. For example, this can happen if an IllegalArgumentException
    // is thrown here...

    // parse command-line arguments
    LaunchRequest lr = null;//  w w w. j  a  va 2s  .co  m
    try {
        lr = LaunchRequest.parseArgs(args);
    } catch (LaunchRequestException e) {
        // FIXME: should be a dialog...
        System.err.println("VASSAL: " + e.getMessage());
        System.exit(1);
    }

    // do this before the graphics subsystem fires up or it won't stick
    System.setProperty("swing.boldMetal", "false");

    if (lr.mode == LaunchRequest.Mode.TRANSLATE) {
        // show the translation window in translation mode
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // FIXME: does this window exit on close?
                new TranslateVassalWindow(null).setVisible(true);
            }
        });
        return;
    }

    //
    // How we start exactly one request server:
    //
    // To ensure that exactly one process acts as the request server, we
    // acquire a lock on the ~/VASSAL/key file, and then attempt to acquire
    // a lock on the ~/VASSAL/lock file. If we cannot lock ~/VASSAL/lock,
    // then there is already a server running; in that case, we read the
    // port number and security key from ~/VASSAL/key. If we can lock
    // ~/VASSAL/lock, then we start the server, write the port number and
    // key to ~/VASSAL/key, and continue to hold the lock on ~/VASSAL/lock.
    // Finally, we unlock ~/VASSAL/key and proceed to act as a client,
    // sending requests over localhost:port using the security key.
    //
    // The advantages of this method are:
    //
    // (1) No race conditions between processes started at the same time.
    // (2) No port collisions, because we don't use a predetermined port.
    //

    final File keyfile = new File(Info.getConfDir(), "key");
    final File lockfile = new File(Info.getConfDir(), "lock");

    int port = 0;
    long key = 0;

    RandomAccessFile kraf = null;
    FileLock klock = null;
    try {
        // acquire an exclusive lock on the key file
        kraf = new RandomAccessFile(keyfile, "rw");

        try {
            klock = kraf.getChannel().lock();
        } catch (OverlappingFileLockException e) {
            throw (IOException) new IOException().initCause(e);
        }

        // determine whether we are the server or a client

        // Note: We purposely keep lout open in the case where we are the
        // server, because closing lout will release the lock.
        FileLock lock = null;
        final FileOutputStream lout = new FileOutputStream(lockfile);
        try {
            lock = lout.getChannel().tryLock();
        } catch (OverlappingFileLockException e) {
            throw (IOException) new IOException().initCause(e);
        }

        if (lock != null) {
            // we have the lock, so we will be the request server

            // bind to an available port on the loopback device
            final ServerSocket serverSocket = new ServerSocket(0, 0, InetAddress.getByName(null));

            // write the port number where we listen to the key file
            port = serverSocket.getLocalPort();
            kraf.writeInt(port);

            // create new security key and write it to the key file
            key = (long) (Math.random() * Long.MAX_VALUE);
            kraf.writeLong(key);

            // create a new Module Manager
            new ModuleManager(serverSocket, key, lout, lock);
        } else {
            // we do not have the lock, so we will be a request client
            lout.close();

            // read the port number we will connect to from the key file
            port = kraf.readInt();

            // read the security key from the key file
            key = kraf.readLong();
        }

        kraf.close();
    } catch (IOException e) {
        // FIXME: should be a dialog...
        System.err.println("VASSAL: IO error");
        e.printStackTrace();
        System.exit(1);
    } finally {
        // this will also release the lock on the key file
        IOUtils.closeQuietly(kraf);
    }

    lr.key = key;

    // pass launch parameters on to the ModuleManager via the socket
    Socket clientSocket = null;
    ObjectOutputStream out = null;
    InputStream in = null;
    try {
        clientSocket = new Socket((String) null, port);

        out = new ObjectOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
        out.writeObject(lr);
        out.flush();

        in = clientSocket.getInputStream();
        IOUtils.copy(in, System.err);
    } catch (IOException e) {
        // FIXME: should be a dialog...
        System.err.println("VASSAL: Problem with socket on port " + port);
        e.printStackTrace();
        System.exit(1);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly((Closeable) out);
        IOUtils.closeQuietly(clientSocket);
    }
}