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:Main.java

public static byte[] readFromFile(String fileName, int offset, int len) {
    if (fileName == null) {
        return null;
    }/*from  w  w w.j av  a2 s  .c  o m*/

    File file = new File(fileName);
    if (!file.exists()) {
        //WLog.i(FileUtils.class, "readFromFile: file not found");
        return null;
    }

    if (len == -1) {
        len = (int) file.length();
    }

    if (offset < 0) {
        //WLog.e(FileUtils.class, "readFromFile invalid offset:" + offset);
        return null;
    }
    if (len <= 0) {
        //WLog.e(FileUtils.class, "readFromFile invalid len:" + len);
        return null;
    }
    if (offset + len > (int) file.length()) {
        //WLog.e(FileUtils.class, "readFromFile invalid file len:" + file.length());
        return null;
    }

    byte[] b = null;
    try {
        RandomAccessFile in = new RandomAccessFile(fileName, "r");
        b = new byte[len];
        in.seek(offset);
        in.readFully(b);
        in.close();

    } catch (Exception e) {
        // WLog.e(FileUtils.class, "readFromFile : errMsg = " + e.getMessage());
        e.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] readFromFile(String fileName, int offset, int len) {
    if (fileName == null) {
        return null;
    }//from   www. j av a2 s . c  o  m

    File file = new File(fileName);
    if (!file.exists()) {
        Log.i(TAG, "readFromFile: file not found");
        return null;
    }

    if (len == -1) {
        len = (int) file.length();
    }

    Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

    if (offset < 0) {
        Log.e(TAG, "readFromFile invalid offset:" + offset);
        return null;
    }
    if (len <= 0) {
        Log.e(TAG, "readFromFile invalid len:" + len);
        return null;
    }
    if (offset + len > (int) file.length()) {
        Log.e(TAG, "readFromFile invalid file len:" + file.length());
        return null;
    }

    byte[] b = null;
    try {
        RandomAccessFile in = new RandomAccessFile(fileName, "r");
        b = new byte[len];
        in.seek(offset);
        in.readFully(b);
        in.close();

    } catch (Exception e) {
        Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
        e.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] readFromFile(String fileName, int offset, int len) {
    if (fileName == null) {
        return null;
    }/* www . j a  v  a 2s  .  c  om*/

    File file = new File(fileName);
    if (!file.exists()) {
        Log.i(TAG, "readFromFile: file not found");
        return null;
    }

    if (len == -1) {
        len = (int) file.length();
    }

    Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

    if (offset < 0) {
        Log.e(TAG, "readFromFile invalid offset:" + offset);
        return null;
    }
    if (len <= 0) {
        Log.e(TAG, "readFromFile invalid len:" + len);
        return null;
    }
    if (offset + len > (int) file.length()) {
        Log.e(TAG, "readFromFile invalid file len:" + file.length());
        return null;
    }

    byte[] b = null;
    try {
        RandomAccessFile in = new RandomAccessFile(fileName, "r");
        b = new byte[len]; //
        in.seek(offset);
        in.readFully(b);
        in.close();

    } catch (Exception e) {
        Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
        e.printStackTrace();
    }
    return b;
}

From source file:org.movsim.MovsimCoreMainWithExperiments.java

private static void appendFiles(File src, File dst, boolean writeFirstLine) throws IOException {

    LineIterator lIter = FileUtils.lineIterator(src);
    RandomAccessFile rFile = new RandomAccessFile(dst, "rw");

    rFile.seek(dst.length());

    long lineCount = 1;
    while (lIter.hasNext()) {
        String line = lIter.next();

        if (lineCount > 1 || writeFirstLine) {
            rFile.write((line + "\n").getBytes());
        }//from www .  j  a v a2 s . co m
        lineCount++;
    }
    lIter.close();
    rFile.close();
}

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

public static FileSummary loadSummary(RandomAccessFile file) throws IOException {
    final int FILE_LENGTH_FIELD_SIZE = 4;
    long fileLength = file.length();
    file.seek(fileLength - FILE_LENGTH_FIELD_SIZE);
    int summaryLength = file.readInt();

    if (summaryLength <= 0) {
        throw new IOException("Negative length of the file");
    }/*from   w w w.  ja v  a 2  s.c om*/
    file.seek(fileLength - FILE_LENGTH_FIELD_SIZE - summaryLength);

    byte[] summaryBytes = new byte[summaryLength];
    file.readFully(summaryBytes);

    FileSummary summary = FileSummary.parseDelimitedFrom(new ByteArrayInputStream(summaryBytes));
    if (summary.getOndiskVersion() != FILE_VERSION) {
        throw new IOException("Unsupported file version " + summary.getOndiskVersion());
    }

    if (!NameNodeLayoutVersion.supports(Feature.PROTOBUF_FORMAT, summary.getLayoutVersion())) {
        throw new IOException("Unsupported layout version " + summary.getLayoutVersion());
    }
    return summary;
}

From source file:net.yacy.document.parser.tarParser.java

public final static boolean isTar(File f) {
    if (!f.exists() || f.length() < 0x105)
        return false;
    RandomAccessFile raf = null;
    try {/* w w w .  java2s  .com*/
        raf = new RandomAccessFile(f, "r");
        raf.seek(0x101);
        byte[] b = new byte[5];
        raf.read(b);
        return MAGIC.equals(UTF8.String(b));
    } catch (final FileNotFoundException e) {
        return false;
    } catch (final IOException e) {
        return false;
    } finally {
        if (raf != null)
            try {
                raf.close();
            } catch (final IOException e) {
            }
    }
}

From source file:shootersubdownloader.Shootersubdownloader.java

static String computefilehash(File f) throws Exception {
    if (!f.exists() || f.length() < 8 * 1024) {
        return null;
    }//from   w  w w  . ja va 2  s.  c o  m
    long l = f.length();
    long[] offset = new long[4];
    offset[3] = l - 8 * 1024;
    offset[2] = l / 3;
    offset[1] = l / 3 * 2;
    offset[0] = 4 * 1024;
    byte[] bBuf = new byte[1024 * 4];
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 4; i++) {
        raf.seek(offset[i]);
        int readlen = raf.read(bBuf, 0, 4 * 1024);
        String md5 = md5(bBuf);
        if (sb.length() != 0) {
            sb.append("%3B");
        }
        sb.append(md5);
    }
    raf.close();
    return sb.toString();
}

From source file:org.mycontroller.standalone.api.jaxrs.utils.McServerLogFile.java

public static LogFileJson getLogUpdate(Long lastKnownPosition, Long lastNPosition) {
    if (lastNPosition != null && appLogFile.length() > lastNPosition) {
        lastKnownPosition = appLogFile.length() - lastNPosition;
    } else if (lastKnownPosition != null && appLogFile.length() <= lastKnownPosition) {
        return LogFileJson.builder().lastKnownPosition(lastKnownPosition).build();
    }//from  w ww.  j ava2 s  . c o  m
    if (lastKnownPosition == null) {
        lastKnownPosition = 0l;
    }

    //Set maximum limit
    if ((appLogFile.length() - lastKnownPosition) > MAX_POSITION_LIMIT) {
        lastKnownPosition = appLogFile.length() - MAX_POSITION_LIMIT;
    }

    logBuilder.setLength(0);
    // Reading and writing file
    RandomAccessFile readFileAccess = null;
    try {
        readFileAccess = new RandomAccessFile(appLogFile, "r");
        readFileAccess.seek(lastKnownPosition);
        String log = null;
        while ((log = readFileAccess.readLine()) != null) {
            logBuilder.append(log).append("\n");
        }
        lastKnownPosition = readFileAccess.getFilePointer();
    } catch (FileNotFoundException ex) {
        _logger.error("Error,", ex);
    } catch (IOException ex) {
        _logger.error("Error,", ex);
    } finally {
        if (readFileAccess != null) {
            try {
                readFileAccess.close();
            } catch (IOException ex) {
                _logger.error("Error,", ex);
            }
        }
    }
    return LogFileJson.builder().lastKnownPosition(lastKnownPosition).data(logBuilder.toString()).build();
}

From source file:org.stem.db.FatFileAllocator.java

public static void allocateFile(String filePath, long sizeInMB, boolean mark) throws IOException {
    long started = System.currentTimeMillis();

    Closer closer = Closer.create();//  w  w  w .ja  v a2  s. c o m
    try {
        File file = new File(filePath);
        if (file.exists())
            throw new IOException(String.format("File already exists: %s", filePath));

        RandomAccessFile rw = closer.register(new RandomAccessFile(file, "rw"));
        rw.setLength(sizeInMB * 1024 * 1024);
        if (mark) {
            rw.seek(0);
            rw.writeByte(FatFile.MARKER_BLANK);
            rw.seek(rw.length() - 1);
            rw.writeByte(FatFile.MARKER_BLANK);
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
        logger.debug("{} was allocated in {} ms", filePath, System.currentTimeMillis() - started);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.BlockMetadataHeader.java

/**
 * Read the header at the beginning of the given block meta file.
 * The current file position will be altered by this method.
 * If an error occurs, the file is <em>not</em> closed.
 *//*from ww w. j av a2  s.co  m*/
public static BlockMetadataHeader readHeader(RandomAccessFile raf) throws IOException {
    byte[] buf = new byte[getHeaderSize()];
    raf.seek(0);
    raf.readFully(buf, 0, buf.length);
    return readHeader(new DataInputStream(new ByteArrayInputStream(buf)));
}