Example usage for java.io RandomAccessFile RandomAccessFile

List of usage examples for java.io RandomAccessFile RandomAccessFile

Introduction

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

Prototype

public RandomAccessFile(File file, String mode) throws FileNotFoundException 

Source Link

Document

Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

Usage

From source file:count.ly.messaging.CrashDetails.java

private static long getTotalRAM() {
    if (totalMemory == 0) {
        RandomAccessFile reader = null;
        String load = null;//  w w w .j  a v a  2  s .  com
        try {
            reader = new RandomAccessFile("/proc/meminfo", "r");
            load = reader.readLine();

            // Get the Number value from the string
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(load);
            String value = "";
            while (m.find()) {
                value = m.group(1);
            }
            reader.close();

            totalMemory = Long.parseLong(value) / 1024;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return totalMemory;
}

From source file:eu.stratosphere.nephele.io.channels.LocalChannelWithAccessInfo.java

LocalChannelWithAccessInfo(final File file, final boolean deleteOnClose) throws IOException {

    this.file = file;
    this.channel = new RandomAccessFile(file, "rw").getChannel();
    this.reservedWritePosition = new AtomicLong(0L);
    this.referenceCounter = new AtomicInteger(0);
    this.deleteOnClose = new AtomicBoolean(deleteOnClose);
}

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

public void saveToDisk(MemorySegment segment) throws IOException {
    File theFile = createPagingFile(segment);
    RandomAccessFile raFile = new RandomAccessFile(theFile, "rw");
    try {/*w w  w. j  a  v a 2  s  . c om*/
        segment.writeToDisk(raFile);
    } finally {
        raFile.close();
    }
}

From source file:ly.count.android.sdk.CrashDetails.java

private static long getTotalRAM() {
    if (totalMemory == 0) {
        RandomAccessFile reader = null;
        String load = null;//  www .j a va2s  .  co  m
        try {
            reader = new RandomAccessFile("/proc/meminfo", "r");
            load = reader.readLine();

            // Get the Number value from the string
            Pattern p = Pattern.compile("(\\d+)");
            Matcher m = p.matcher(load);
            String value = "";
            while (m.find()) {
                value = m.group(1);
            }
            try {
                totalMemory = Long.parseLong(value) / 1024;
            } catch (NumberFormatException ex) {
                totalMemory = 0;
            }
        } catch (IOException ex) {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
            ex.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        }
    }
    return totalMemory;
}

From source file:com.linkedin.pinot.core.io.writer.impl.FixedBitSingleColumnMultiValueWriter.java

public FixedBitSingleColumnMultiValueWriter(File file, int numDocs, int totalNumValues, int columnSizeInBits)
        throws Exception {
    // there will be two sections header and data
    // header will contain N lines, each line corresponding to the
    int headerSize = numDocs * SIZE_OF_INT * NUM_COLS_IN_HEADER;
    int dataSize = (totalNumValues * columnSizeInBits + 7) / 8;
    int totalSize = headerSize + dataSize;
    raf = new RandomAccessFile(file, "rw");
    headerBuffer = MmapUtils.mmapFile(raf, FileChannel.MapMode.READ_WRITE, 0, headerSize, file,
            this.getClass().getSimpleName() + " headerBuffer");
    dataBuffer = MmapUtils.mmapFile(raf, FileChannel.MapMode.READ_WRITE, headerSize, dataSize, file,
            this.getClass().getSimpleName() + " dataBuffer");
    headerWriter = new FixedByteSingleValueMultiColWriter(headerBuffer, numDocs, 2,
            new int[] { SIZE_OF_INT, SIZE_OF_INT });
    headerReader = new FixedByteSingleValueMultiColReader(headerBuffer, numDocs, 2,
            new int[] { SIZE_OF_INT, SIZE_OF_INT });

    dataWriter = new FixedBitSingleValueMultiColWriter(dataBuffer, totalNumValues, 1,
            new int[] { columnSizeInBits });

}

From source file:at.tuwien.minimee.util.PslistWinParser.java

public void parse() {

    try {/*from ww  w  .j  ava  2 s .  c o  m*/
        input = new RandomAccessFile(file, "r");
        list = new WinExecutionFootprintList();

        try {
            String line = null;

            while ((line = input.readLine()) != null && line != "") {
                parseLine(line);
            }

        } finally {
            input.close();
        }

        // list.debugToConsole();

    } catch (Exception e) {
        log.error(e);
    }
}

From source file:com.netease.flume.taildirSource.TailFile.java

public TailFile(File file, Map<String, String> headers, long inode, long pos) throws IOException {
    this.raf = new RandomAccessFile(file, "r");
    if (pos > 0)
        raf.seek(pos);//w  w w .j  ava 2s .  co  m
    this.path = file.getAbsolutePath();
    this.inode = inode;
    this.pos = pos;
    this.lastUpdated = 0L;
    this.needTail = true;
    this.headers = headers;
}

From source file:eu.delving.metadata.Hasher.java

public static String quickHash(File file) throws IOException {
    Hasher hasher = new Hasher();
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    byte[] chunk = new byte[QUICK_SAMPLE_SIZE];
    long length = raf.length() - chunk.length;
    long step = length / QUICK_SAMPLES;
    for (int walk = 0; walk < QUICK_SAMPLES; walk++) {
        raf.seek(step * walk);//from w w w  .j  a v  a 2  s .  c o  m
        raf.readFully(chunk);
        hasher.update(chunk, chunk.length);
    }
    raf.close();
    return hasher.getHashString().substring(4, 14);
}

From source file:at.tuwien.ifs.somtoolbox.apps.helper.VectorFileToRandomAccessFileConverter.java

public VectorFileToRandomAccessFileConverter(String inputVectorFile) throws IOException {
    String outputFile = StringUtils.chomp(inputVectorFile, ".gz") + ".bin";
    if (new File(outputFile).exists()) {
        new File(outputFile).delete();
    }//from w w w.  jav  a 2s . co  m
    file = new RandomAccessFile(outputFile, "rw");
    // write all the vectors
    readVectorFile(inputVectorFile, false);
    // write the vector labels in the end of the file
    RandomAccessFileSOMLibInputData.writeVectorLabels(file, dataNames);
    file.close();
}

From source file:com.linkedin.pinot.core.index.writer.impl.FixedBitWidthSingleColumnMultiValueWriter.java

public FixedBitWidthSingleColumnMultiValueWriter(File file, int numDocs, int totalNumValues,
        int columnSizeInBits) throws Exception {
    // there will be two sections header and data
    // header will contain N lines, each line corresponding to the
    int headerSize = numDocs * SIZE_OF_INT * NUM_COLS_IN_HEADER;
    int dataSize = (totalNumValues * columnSizeInBits + 7) / 8;
    int totalSize = headerSize + dataSize;
    raf = new RandomAccessFile(file, "rw");
    headerBuffer = MmapUtils.mmapFile(raf, FileChannel.MapMode.READ_WRITE, 0, headerSize, file,
            this.getClass().getSimpleName() + " headerBuffer");
    dataBuffer = MmapUtils.mmapFile(raf, FileChannel.MapMode.READ_WRITE, headerSize, dataSize, file,
            this.getClass().getSimpleName() + " dataBuffer");
    headerWriter = new FixedByteWidthRowColDataFileWriter(headerBuffer, numDocs, 2,
            new int[] { SIZE_OF_INT, SIZE_OF_INT });
    headerReader = new FixedByteWidthRowColDataFileReader(headerBuffer, numDocs, 2,
            new int[] { SIZE_OF_INT, SIZE_OF_INT });

    dataWriter = new FixedBitWidthRowColDataFileWriter(dataBuffer, totalNumValues, 1,
            new int[] { columnSizeInBits });

}