Example usage for java.nio MappedByteBuffer isLoaded

List of usage examples for java.nio MappedByteBuffer isLoaded

Introduction

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

Prototype

public final boolean isLoaded() 

Source Link

Document

Tells whether or not this buffer's content is resident in physical memory.

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);

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

    buf.force();/* ww w .  j av  a 2  s . c  o  m*/

    channel.close();
}

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();//w  ww  .j  a  va 2 s  .co m

    MappedByteBuffer mbb = buf.load();

    channel.close();
}

From source file:org.roda.core.storage.fs.FSUtils.java

public static String computeContentDigest(Path path, String algorithm) throws GenericException {
    try (FileChannel fc = FileChannel.open(path)) {
        final int bufferSize = 1073741824;
        final long size = fc.size();
        final MessageDigest hash = MessageDigest.getInstance(algorithm);
        long position = 0;
        while (position < size) {
            final MappedByteBuffer data = fc.map(FileChannel.MapMode.READ_ONLY, 0, Math.min(size, bufferSize));
            if (!data.isLoaded()) {
                data.load();/*from   www .  ja  v  a 2s  . c  o m*/
            }
            hash.update(data);
            position += data.limit();
            if (position >= size) {
                break;
            }
        }

        byte[] mdbytes = hash.digest();
        StringBuilder hexString = new StringBuilder();

        for (int i = 0; i < mdbytes.length; i++) {
            String hexInt = Integer.toHexString((0xFF & mdbytes[i]));
            if (hexInt.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hexInt);
        }

        return hexString.toString();

    } catch (NoSuchAlgorithmException | IOException e) {
        throw new GenericException(
                "Cannot compute content digest for " + path + " using algorithm " + algorithm);
    }
}