Example usage for java.io RandomAccessFile readLong

List of usage examples for java.io RandomAccessFile readLong

Introduction

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

Prototype

public final long readLong() throws IOException 

Source Link

Document

Reads a signed 64-bit integer from this file.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from  w w  w.  j  a  va2 s .com
        long f = 123456789909876L;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeLong(f);

        raf.seek(0);

        System.out.println(raf.readLong());

        raf.seek(0);

        raf.writeLong(20000000000l);

        raf.seek(0);

        System.out.println(raf.readLong());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w ww .  jav a 2s .  c  o m
        long l = 12345676789098L;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeLong(l);

        // set the file pointer at 0 position
        raf.seek(0);

        System.out.println(raf.readLong());

        // set the file pointer at 0 position
        raf.seek(0);

        // write something in the file
        raf.writeLong(12345676789099L);

        raf.seek(0);
        System.out.println(raf.readLong());
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:cn.edu.xmu.tidems.control.support.TideMSUtil.java

public static boolean isHDF5File(final File file) {

    if ((!file.exists()) || (file.isDirectory()) || (file.length() < 1024)) {
        return false;
    }/*from  w  w  w  .  j  av  a  2  s  . c  o m*/
    try {
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        final long sig = raf.readLong(); // The first 8 byte are format signatures of hdf5 file
        raf.close();
        return sig == -8554512424533091830L; // 0x89 48 44 46 0d 0a 1a 0a
    } catch (final Exception e) {
        return false;
    }
}

From source file:com.sangupta.snowpack.SnowpackRecover.java

/**
 * Try and recover from a chunk./*  www  .  ja va  2s. c  o  m*/
 * 
 * @param chunkID
 * @param chunkFile
 * @param metadataDB 
 * @return
 * @throws IOException 
 */
private static ChunkInfo recoverChunkInfo(final int chunkID, final File chunkFile,
        SnowpackMetadataDB metadataDB) throws IOException {
    // open the file for reading
    RandomAccessFile raf = new RandomAccessFile(chunkFile, "r");

    // read the length first
    int nameLength, length, terminator, headerLength, numFiles = 0;
    long offset;

    List<FlakeMetadata> metas = new ArrayList<FlakeMetadata>();

    try {
        while (raf.getFilePointer() < raf.length()) {
            offset = raf.getFilePointer();

            nameLength = raf.readInt();
            byte[] name = new byte[nameLength];
            raf.readFully(name);

            length = raf.readInt();
            raf.readLong();
            raf.skipBytes((int) length);

            terminator = raf.readByte();
            if (terminator != 0) {
                System.out.print(" invalid descriptor found...");
                return null;
            }

            headerLength = 4 + name.length + 4 + 8;

            numFiles++;

            metas.add(new FlakeMetadata(new String(name), nameLength, chunkID, offset, headerLength));
        }
    } finally {
        raf.close();
    }

    // all clear for recovery

    // save all metadata in new DB
    for (FlakeMetadata meta : metas) {
        metadataDB.save(meta);
    }

    // return chunk info
    ChunkInfo info = new ChunkInfo();
    info.chunkID = chunkID;
    info.numFiles = numFiles;
    info.writePointer = -1;

    return info;
}

From source file:com.btoddb.fastpersitentqueue.JournalFileTest.java

@Test
public void testInitForWritingThenClose() throws IOException {
    JournalFile jf1 = new JournalFile(theFile);
    jf1.initForWriting(new UUID());
    assertThat(jf1.isWriteMode(), is(true));
    assertThat(jf1.isOpen(), is(true));//from   ww  w .  j  av a  2s.co  m
    assertThat(jf1.getFilePosition(), is((long) JournalFile.HEADER_SIZE));
    jf1.close();

    assertThat(jf1.isOpen(), is(false));

    RandomAccessFile raFile = new RandomAccessFile(theFile, "rw");
    assertThat(raFile.readInt(), is(JournalFile.VERSION));
    assertThat(Utils.readUuidFromFile(raFile), is(jf1.getId()));
    assertThat(raFile.readLong(), is(0L));
    raFile.close();
}

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

private static int readTargetsFromLogFile(CrawlList domain, File logFileName, int desiredReadAmount,
        IntrusiveList<CrawlTarget> targetsOut) throws IOException {

    int itemsRead = 0;

    if (logFileName.exists()) {

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

        LogFileHeader header = new LogFileHeader();

        try {//from   w  w  w .  jav a2s.  c o m

            long headerOffset = readLogFileHeader(file, header);

            // seelk to appropriate write position 
            if (header._readPos != 0)
                file.seek(header._readPos);

            int itemsToRead = Math.min(desiredReadAmount, header._itemCount);

            PersistentCrawlTarget persistentTarget = new PersistentCrawlTarget();
            CRC32 crc = new CRC32();
            CustomByteArrayOutputStream buffer = new CustomByteArrayOutputStream(1 << 16);

            for (int i = 0; i < itemsToRead; ++i) {
                // read length ... 
                int urlDataLen = file.readInt();
                long urlDataCRC = file.readLong();

                buffer.reset();

                if (urlDataLen > buffer.getBuffer().length) {
                    buffer = new CustomByteArrayOutputStream(((urlDataLen / 65536) + 1) * 65536);
                }
                file.read(buffer.getBuffer(), 0, urlDataLen);
                crc.reset();
                crc.update(buffer.getBuffer(), 0, urlDataLen);

                long computedValue = crc.getValue();

                // validate crc values ... 
                if (computedValue != urlDataCRC) {
                    throw new IOException("Crawl Target Log File Corrupt");
                } else {
                    //populate a persistentTarget from the (in memory) data stream
                    DataInputStream bufferReader = new DataInputStream(
                            new ByteArrayInputStream(buffer.getBuffer(), 0, urlDataLen));

                    persistentTarget.clear();
                    persistentTarget.readFields(bufferReader);

                    //populate a new crawl target structure ... 
                    CrawlTarget newTarget = new CrawlTarget(domain, persistentTarget);

                    targetsOut.addTail(newTarget);
                }
            }

            itemsRead = itemsToRead;

            // now update header ... 
            header._itemCount -= itemsRead;
            // now if item count is non zero ... 
            if (header._itemCount != 0) {
                // set read cursor to next record location 
                header._readPos = file.getFilePointer();
            }
            // otherwise ... 
            else {
                // reset both cursors ... 
                header._readPos = 0;
                header._writePos = 0;
            }

            // now write out header anew ... 
            writeLogFileHeader(file, header);
        } finally {
            if (file != null) {
                file.close();
            }
        }
    }
    return itemsRead;
}

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

private static void transferLocalCheckpointLog(File crawlLogPath, HDFSCrawlURLWriter writer, long checkpointId)
        throws IOException {

    // and open the crawl log file ...
    RandomAccessFile inputStream = null;

    IOException exception = null;

    CRC32 crc = new CRC32();
    CustomByteArrayOutputStream buffer = new CustomByteArrayOutputStream(1 << 17);
    byte[] syncBytesBuffer = new byte[SYNC_BYTES_SIZE];

    // save position for potential debug output.
    long lastReadPosition = 0;

    try {//from   w w w . j a  va2 s.co m
        inputStream = new RandomAccessFile(crawlLogPath, "rw");
        // and a data input stream ...
        RandomAccessFile reader = inputStream;
        // seek to zero
        reader.seek(0L);

        // read the header ...
        LogFileHeader header = readLogFileHeader(reader);

        // read a crawl url from the stream...

        while (inputStream.getFilePointer() < header._fileSize) {

            if (seekToNextSyncBytesPos(syncBytesBuffer, reader, header._fileSize)) {

                try {
                    lastReadPosition = inputStream.getFilePointer();

                    // skip sync
                    inputStream.skipBytes(SYNC_BYTES_SIZE);

                    // read length ...
                    int urlDataLen = reader.readInt();
                    long urlDataCRC = reader.readLong();

                    if (urlDataLen > buffer.getBuffer().length) {
                        buffer = new CustomByteArrayOutputStream(((urlDataLen / 65536) + 1) * 65536);
                    }
                    reader.read(buffer.getBuffer(), 0, urlDataLen);
                    crc.reset();
                    crc.update(buffer.getBuffer(), 0, urlDataLen);

                    long computedValue = crc.getValue();

                    // validate crc values ...
                    if (computedValue != urlDataCRC) {
                        LOG.error("CRC Mismatch Detected during HDFS transfer in CrawlLog:"
                                + crawlLogPath.getAbsolutePath() + " Checkpoint Id:" + checkpointId
                                + " FilePosition:" + lastReadPosition);
                        inputStream.seek(lastReadPosition + 1);
                    } else {
                        // allocate a crawl url data structure
                        CrawlURL url = new CrawlURL();
                        DataInputStream bufferReader = new DataInputStream(
                                new ByteArrayInputStream(buffer.getBuffer(), 0, urlDataLen));
                        // populate it from the (in memory) data stream
                        url.readFields(bufferReader);
                        try {
                            // and write out appropriate sequence file entries ...
                            writer.writeCrawlURLItem(new Text(url.getUrl()), url);
                        } catch (IOException e) {
                            LOG.error("Failed to write CrawlURL to SequenceFileWriter with Exception:"
                                    + CCStringUtils.stringifyException(e));
                            throw new URLWriterException();
                        }
                    }
                } catch (URLWriterException e) {
                    LOG.error("Caught URLRewriter Exception! - Throwing to outer layer!");
                    throw e;
                } catch (Exception e) {
                    LOG.error("Ignoring Error Processing CrawlLog Entry at Position:" + lastReadPosition
                            + " Exception:" + CCStringUtils.stringifyException(e));
                }
            } else {
                break;
            }
        }
    } catch (EOFException e) {
        LOG.error("Caught EOF Exception during read of local CrawlLog:" + crawlLogPath.getAbsolutePath()
                + " Checkpoint Id:" + checkpointId + " FilePosition:" + lastReadPosition);
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        exception = e;
        throw e;
    } finally {
        if (inputStream != null)
            inputStream.close();
    }
}

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

public static void walkCrawlLogFile(File crawlLogPath, long startOffset) throws IOException {

    // and open the crawl log file ...
    RandomAccessFile inputStream = null;

    IOException exception = null;

    CRC32 crc = new CRC32();
    CustomByteArrayOutputStream buffer = new CustomByteArrayOutputStream(1 << 17);
    byte[] syncBytesBuffer = new byte[SYNC_BYTES_SIZE];

    // save position for potential debug output.
    long lastReadPosition = 0;

    try {/*  w w w .j  a v a2 s  . co m*/
        inputStream = new RandomAccessFile(crawlLogPath, "rw");

        // and a data input stream ...
        RandomAccessFile reader = inputStream;
        // seek to zero
        reader.seek(0L);

        // read the header ...
        LogFileHeader header = readLogFileHeader(reader);

        System.out.println("Header ItemCount:" + header._itemCount + " FileSize:" + header._fileSize);

        if (startOffset != 0L) {
            System.out.println("Preseeking to:" + startOffset);
            reader.seek(startOffset);
        }

        Configuration conf = new Configuration();

        // read a crawl url from the stream...

        long recordCount = 0;
        while (inputStream.getFilePointer() < header._fileSize) {

            // System.out.println("PRE-SYNC SeekPos:"+
            // inputStream.getFilePointer());
            if (seekToNextSyncBytesPos(syncBytesBuffer, reader, header._fileSize)) {

                // System.out.println("POST-SYNC SeekPos:"+
                // inputStream.getFilePointer());

                lastReadPosition = inputStream.getFilePointer();

                // skip sync
                inputStream.skipBytes(SYNC_BYTES_SIZE);

                // read length ...
                int urlDataLen = reader.readInt();
                long urlDataCRC = reader.readLong();

                if (urlDataLen > buffer.getBuffer().length) {
                    buffer = new CustomByteArrayOutputStream(((urlDataLen / 65536) + 1) * 65536);
                }
                reader.read(buffer.getBuffer(), 0, urlDataLen);
                crc.reset();
                crc.update(buffer.getBuffer(), 0, urlDataLen);

                long computedValue = crc.getValue();

                // validate crc values ...
                if (computedValue != urlDataCRC) {
                    LOG.error("CRC Mismatch Detected during HDFS transfer in CrawlLog:"
                            + crawlLogPath.getAbsolutePath() + " FilePosition:" + lastReadPosition);
                    inputStream.seek(lastReadPosition + 1);
                } else {
                    if (recordCount++ % 10000 == 0) {
                        // allocate a crawl url data structure
                        CrawlURL url = new CrawlURL();
                        DataInputStream bufferReader = new DataInputStream(
                                new ByteArrayInputStream(buffer.getBuffer(), 0, urlDataLen));
                        // populate it from the (in memory) data stream
                        url.readFields(bufferReader);

                        System.out.println("Record:" + recordCount + " At:" + lastReadPosition + " URL:"
                                + url.getUrl() + " BuffSize:" + urlDataLen + " ContentLen:"
                                + url.getContentRaw().getCount() + " LastModified:"
                                + new Date(url.getLastAttemptTime()).toString());
                    }
                }
            } else {
                break;
            }
        }
    } catch (EOFException e) {
        LOG.error("Caught EOF Exception during read of local CrawlLog:" + crawlLogPath.getAbsolutePath()
                + " FilePosition:" + lastReadPosition);
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        exception = e;
        throw e;
    } finally {
        if (inputStream != null)
            inputStream.close();
    }
}

From source file:org.mhisoft.wallet.service.AttachmentService.java

public FileAccessTable read(String dataFile, final PBEEncryptor encryptor) {
    logger.fine("\n\nread attachment file:" + dataFile);
    FileAccessTable t = null;// w  w w. java 2s.c om
    RandomAccessFile fileIn = null;
    try {
        File fIn = new File(dataFile);
        if (!fIn.exists()) {
            return t;
        }

        fileIn = new RandomAccessFile(dataFile, "rw");
        int numberOfEntries = fileIn.readInt();
        t = new FileAccessTable();
        long pos = 4;
        int readBytes = 0;
        for (int i = 0; i < numberOfEntries; i++) {

            fileIn.seek(pos);

            /* UUIID and position */
            String UUID = FileUtils.readString(fileIn);
            FileAccessEntry fileAccessEntry = new FileAccessEntry(UUID);
            pos += 40;
            logger.fine("Read entry, UUID:" + UUID);

            /*  accessflag */
            fileAccessEntry.setAccessFlag(FileAccessFlag.values[fileIn.readInt()]);
            pos += 4;
            logger.fine("\t access flag:" + fileAccessEntry.getAccessFlag());

            /*  pos */
            fileAccessEntry.setPosition(fileIn.readLong());
            pos += 8;
            logger.fine("\t position:" + fileAccessEntry.getPosition());

            /* read filename */
            ReadContentVO vo = readCipherParameter(fileIn, pos);
            pos = vo.pos;
            int encSize_FileName = fileIn.readInt();
            pos += 4;
            byte[] _encedBytes = new byte[encSize_FileName];
            fileIn.readFully(_encedBytes);
            pos += encSize_FileName;
            byte[] byte_filename = encryptor.decrypt(_encedBytes, vo.algorithmParameters);
            fileAccessEntry.setFileName(StringUtils.bytesToString(byte_filename));
            logger.fine("\t file name:" + fileAccessEntry.getFileName());

            /* attachment */
            vo = readCipherParameter(fileIn, pos);
            pos = vo.pos;
            fileAccessEntry.setAlgorithmParameters(vo.algorithmParameters);

            /*#5  size of the content (int): 4 bytes */
            int encSize = fileIn.readInt();
            pos += 4;

            fileAccessEntry.setPosOfContent(pos);
            fileAccessEntry.setEncSize(encSize);
            logger.fine("\t encSize:" + encSize);

            if (fileAccessEntry.getAccessFlag() != FileAccessFlag.Delete)
                t.addEntry(fileAccessEntry);
            else {
                logger.fine("\tentries is marked as deleted.");
                t.deletedEntries++;
            }

            /* #6 file content */
            pos += encSize;

            /* delay read it on demand
            byte[] _encBytes =readFileContent(dataFile, pos, encSize ) ;
            byte[] fileContent = encryptor.decrypt(_encBytes, algorithmParameters);
            pos +=  fileContent.length;
                    
            fileAccessEntry.setFileContent(fileContent);
            fileAccessEntry.setSize(fileContent.length); //decrypted size.
            */

        }

        fileIn.close();
        fileIn = null;

    } catch (Exception e) {
        e.printStackTrace();
        DialogUtils.getInstance().error("Error occurred in reading attachments.", e.getMessage());
    } finally {
        if (fileIn != null)
            try {
                fileIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    DebugUtil.append(
            "Attachment Store total entries:" + t.getSize() + "\n" + "Orphan records :" + t.deletedEntries);

    return t;
}

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;/*from ww w  .j  a va 2 s  .  c om*/
    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);
    }
}