Example usage for java.nio MappedByteBuffer force

List of usage examples for java.nio MappedByteBuffer force

Introduction

In this page you can find the example usage for java.nio MappedByteBuffer force.

Prototype

public final MappedByteBuffer force() 

Source Link

Document

Forces any changes made to this buffer's content to be written to the storage device containing the mapped file.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    buf.force();

    channel.close();/*from w  w w . ja v a 2s . c o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();

    channel.close();//from   w  ww . jav  a2 s.  c  om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();

    MappedByteBuffer mbb = buf.load();

    channel.close();/*from ww w.java2 s  .  c  o  m*/
}

From source file:gedi.util.FileUtils.java

/**
*Make sure that there is no strong reference left to the buffer!
* @param ref/*from w  w  w  .  j av  a 2  s . c  om*/
* @return
*/
public static boolean unmapSynchronous(WeakReference<MappedByteBuffer> ref) {
    MappedByteBuffer buffer = ref.get();
    if (buffer == null)
        return true;
    // first write all data
    buffer.force();

    // need to dispose old direct buffer, see bug
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038

    boolean useSystemGc = true;
    try {
        ((DirectBuffer) buffer).cleaner().clean();
        useSystemGc = false;
    } catch (Throwable e) {
        // useSystemGc is already true
    } finally {
    }
    if (useSystemGc) {
        buffer = null;
        long start = System.currentTimeMillis();
        while (ref.get() != null) {
            if (System.currentTimeMillis() - start > GC_TIMEOUT_MS) {
                return false;
            }
            System.gc();
            Thread.yield();
        }
    }
    return true;
}

From source file:org.apache.hadoop.hdfs.hoss.db.FileBlockStore.java

@SuppressWarnings("unchecked")
private void syncAllMmaps() {
    final BufferReference<MappedByteBuffer>[] maps = mmaps.getValues();
    Arrays.sort(maps, comparatorByIdx);
    for (final Reference<MappedByteBuffer> ref : maps) {
        if (ref == null)
            break;
        final MappedByteBuffer mbb = ref.get();
        if (mbb != null) {
            try {
                mbb.force();
            } catch (Exception ign) {
            }/* w  w w  .  j  av  a  2  s  .  c om*/
        }
    }
}

From source file:org.cytobank.fcs_files.events.MemoryEvents.java

protected synchronized boolean writeOut() {
    if (memoryEventsArray == null)
        return false;

    File memoryEventsArrayOnDisk = null;

    synchronized (memoryEventsArray) {
        memoryEventsArrayOnDisk = memoryEventsArray.getOnDisk();

        if (memoryEventsArrayOnDisk != null || events == null)
            return false;

        RandomAccessFile randomAccessFile = null;
        FileChannel fileChannel = null;

        try {/*  w  ww .j  a va 2s. c o m*/
            memoryEventsArrayOnDisk = File.createTempFile("memoryEventsArrayOnDisk", "evt");
            memoryEventsArrayOnDisk.deleteOnExit();
            randomAccessFile = new RandomAccessFile(memoryEventsArrayOnDisk, "rw");
            fileChannel = randomAccessFile.getChannel();

            MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0,
                    numberOfEvents * BYTES_PER_DOUBLE);
            DoubleBuffer doubleBuffer = mappedByteBuffer.asDoubleBuffer();
            doubleBuffer.put(events, 0, numberOfEvents);
            mappedByteBuffer.force();
            fileChannel.close();
            randomAccessFile.close();

            memoryEventsArray.setOnDisk(memoryEventsArrayOnDisk);

        } catch (IOException ioe) {
            logger.log(Level.INFO, "::::: Failed to write out MemoryEventsArray :::::", ioe);
            if (memoryEventsArrayOnDisk != null) {
                memoryEventsArrayOnDisk.delete();
                memoryEventsArrayOnDisk = null;
            }
        } finally {
            memoryEventsArrayOnDisk.delete();
        }
    }

    return true;

}