Example usage for java.io RandomAccessFile seek

List of usage examples for java.io RandomAccessFile seek

Introduction

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

Prototype

public void seek(long pos) throws IOException 

Source Link

Document

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Usage

From source file:org.kalypso.shape.shp.SHPFile.java

/**
 * method: getByRecNo (int RecNo) <BR>
 * returns a ShapeRecord-Geometry by RecorcNumber <BR>
 *///from  w  ww  . j  av a  2  s .  c  o m
public ISHPGeometry getShape(final SHXRecord record) throws IOException {
    final RandomAccessFile raf = getRandomAccessFile();

    final int position = record.getOffset() * 2;
    final int contentLength = record.getLength() * 2;

    final byte[] recBuf = new byte[contentLength];
    raf.seek(position + 8);
    raf.readFully(recBuf);

    final ShapeType shpType = ShapeType.valueOf(ByteUtils.readLEInt(recBuf, 0));
    if (shpType == ShapeType.NULL)
        return new SHPNullShape();

    // create a geometry out of record buffer with shape type
    switch (shpType) {
    case NULL:
        return new SHPNullShape();
    case POINT:
        return new SHPPoint(recBuf);
    case MULTIPOINT:
        return SHPMultiPoint.read(recBuf);
    case POLYLINE:
        return new SHPPolyLine(recBuf);
    case POLYGON:
        return new SHPPolygon(recBuf);
    case POINTZ:
        return new SHPPointz(recBuf);
    case POLYLINEZ:
        return new SHPPolyLinez(recBuf);
    case POLYGONZ:
        return new SHPPolygonz(recBuf);
    case MULTIPOINTZ:
        return SHPMultiPointz.read(recBuf);
    case POINTM:
        return new SHPPointm(recBuf);
    case MULTIPOINTM:
        return SHPMultiPointm.read(recBuf);
    case POLYLINEM:
        return new SHPPolyLinem(recBuf);
    case POLYGONM:
        return new SHPPolyLinem(recBuf);
    }

    throw new UnsupportedOperationException("Unknown shape type: " + shpType);
}

From source file:edu.msu.cme.rdp.readseq.readers.core.SFFCore.java

@Override
public LinkedHashMap<String, Long> scanInternal() throws IOException {
    if (commonHeader.indexOffset > commonHeader.headerLength) {
        return readIndex();
    }//from w w  w  .  j  a va 2s. c o  m

    RandomAccessFile seqFile = super.getRawFile();

    seqFile.seek(commonHeader.headerLength);
    LinkedHashMap<String, Long> seqIndex = new LinkedHashMap(commonHeader.numReads);

    for (int index = 0; index < commonHeader.numReads; index++) {
        long pos = seqFile.getFilePointer();
        ReadBlock block = readReadBlock();
        seqIndex.put(block.name, pos);
    }

    return seqIndex;
}

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

/** 
 * Corrupt an edit log file after the start segment transaction
 *//*from ww  w  .  ja v a  2s .  co  m*/
private void corruptAfterStartSegment(File f) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    raf.seek(0x20); // skip version and first tranaction and a bit of next transaction
    for (int i = 0; i < 1000; i++) {
        raf.writeInt(0xdeadbeef);
    }
    raf.close();
}

From source file:hoot.services.info.ErrorLog.java

public String getErrorlog(long maxLength) throws Exception {

    File file = new File(_errLogPath);
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
    int lines = 0;
    StringBuilder builder = new StringBuilder();
    long length = file.length();
    //length--;/*from   w w  w .j  a v a 2s.  co m*/

    long startOffset = 0;
    if (length > maxLength) {
        startOffset = length - maxLength;
    }
    for (long seek = startOffset; seek < length; seek++) {
        randomAccessFile.seek(seek);
        char c = (char) randomAccessFile.read();
        builder.append(c);
    }

    randomAccessFile.close();

    return builder.toString();
}

From source file:aarddict.Volume.java

String readKey(long pointer) throws IOException {
    Header h = this.header;
    long pos = h.index2Offset + pointer;
    RandomAccessFile f = this.file;
    f.seek(pos);
    int keyLength = (int) f.readSpec(h.keyLengthSpec);
    return f.readUTF8(keyLength);
}

From source file:aarddict.Volume.java

IndexItem readIndexItem(long i) throws IOException {
    Header h = this.header;
    long pos = h.index1Offset + i * h.index1ItemSize;
    RandomAccessFile f = this.file;
    f.seek(pos);
    IndexItem indexItem = new IndexItem();
    indexItem.keyPointer = f.readSpec(h.keyPointerSpec);
    indexItem.articlePointer = f.readSpec(h.articlePointerSpec);
    return indexItem;
}

From source file:org.kalypso.shape.shp.SHPFile.java

@Override
public void close() throws IOException {
    final RandomAccessFile raf = getRandomAccessFile();

    if (isWriting()) {
        // Update header with length
        final long fileLength = raf.length();
        updateHeader((int) fileLength, getMBR());

        /* and write the header */
        raf.seek(0);

        final ShapeHeader header = getHeader();
        header.write(raf);/*w  w w.  j a va2 s.c  o m*/
    }

    raf.close();
}

From source file:com.sky.drovik.player.media.DiskCache.java

public byte[] get(long key, long timestamp) {
    // Look up the record for the given key.
    Record record = null;/*w ww  . j  ava2  s .c  om*/
    synchronized (mIndexMap) {
        record = mIndexMap.get(key);
    }
    if (record != null) {
        // Read the chunk from the file.
        if (record.timestamp < timestamp) {
            Log.i(TAG, "File has been updated to " + timestamp + " since the last time " + record.timestamp
                    + " stored in cache.");
            return null;
        }
        try {
            RandomAccessFile chunkFile = getChunkFile(record.chunk);
            if (chunkFile != null) {
                byte[] data = new byte[record.size];
                chunkFile.seek(record.offset);
                chunkFile.readFully(data);
                return data;
            }
        } catch (Exception e) {
            Log.e(TAG, "Unable to read from chunk file");
        }
    }
    return null;
}

From source file:com.ibm.sbt.test.lib.MockSerializer.java

public void writeData(String data) throws IOException {
    File file = getFile(true);//from   w  w w  . j  a va 2 s  .c  om
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    // Seek to end of file
    System.out.println("Writing Record @" + file.length() + " in " + file.getAbsolutePath());
    raf.seek((file.length() - "\n</responses>".length()));

    raf.write(data.getBytes("UTF-8"));
    raf.write("\n</responses>".getBytes("UTF-8"));

}

From source file:dk.netarkivet.common.utils.FileUtils.java

/**
 * Read the last line in a file. Note this method is not UTF-8 safe.
 *
 * @param file input file to read last line from.
 * @return The last line in the file (ending newline is irrelevant),
 * returns an empty string if file is empty.
 * @throws ArgumentNotValid on null argument, or file is not a readable
 * file.//from w w  w.  ja va  2  s  .c  om
 * @throws IOFailure on IO trouble reading file.
 */
public static String readLastLine(File file) {
    ArgumentNotValid.checkNotNull(file, "File file");
    if (!file.isFile() || !file.canRead()) {
        final String errMsg = "File '" + file.getAbsolutePath() + "' is not a readable file.";
        log.warn(errMsg);
        throw new ArgumentNotValid(errMsg);
    }
    if (file.length() == 0) {
        return "";
    }
    RandomAccessFile rafile = null;
    try {
        rafile = new RandomAccessFile(file, "r");
        //seek to byte one before end of file (remember we know the file is
        // not empty) - this ensures that an ending newline is not read
        rafile.seek(rafile.length() - 2);
        //now search to the last linebreak, or beginning of file
        while (rafile.getFilePointer() != 0 && rafile.read() != '\n') {
            //search back two, because we just searched forward one to find
            //newline
            rafile.seek(rafile.getFilePointer() - 2);
        }
        return rafile.readLine();
    } catch (IOException e) {
        final String errMsg = "Unable to access file '" + file.getAbsolutePath() + "'";
        log.warn(errMsg, e);
        throw new IOFailure(errMsg, e);
    } finally {
        try {
            if (rafile != null) {
                rafile.close();
            }
        } catch (IOException e) {
            log.debug("Unable to close file '" + file.getAbsolutePath() + "' after reading", e);
        }
    }
}