Example usage for java.nio MappedByteBuffer load

List of usage examples for java.nio MappedByteBuffer load

Introduction

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

Prototype

public final MappedByteBuffer load() 

Source Link

Document

Loads this buffer's content into 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();/* w  ww.j a v  a2 s  .  co m*/

    MappedByteBuffer mbb = buf.load();

    channel.close();
}

From source file:io.ecarf.core.utils.Utils.java

/**
 * Get the likely uncompressed size of a Gziped file
 * @param filename/* www .j  ava2 s  .c om*/
 * @return
 * @throws IOException 
 * @see http://stackoverflow.com/questions/27825927/get-gzipped-file-attributes-like-gzip-l-basically-compression-ratio
 * @throws FileNotFoundException 
 */
public static long getUncompressedFileSize(String filename) throws FileNotFoundException, IOException {

    File f = new File(filename);
    try (RandomAccessFile ra = new RandomAccessFile(f, "r"); FileChannel channel = ra.getChannel()) {

        MappedByteBuffer fileBuffer = channel.map(MapMode.READ_ONLY, f.length() - 4, 4);
        fileBuffer.load();

        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.order(ByteOrder.LITTLE_ENDIAN);

        buf.put(fileBuffer);
        buf.flip();
        //will print the uncompressed size
        //getInt() reads the 4 bytes as a int
        // if the file is between 2GB and 4GB
        // then this will return a negative value
        //and you'll have to do your own converting to an unsigned int
        int size = buf.getInt();

        if (size < 0) {
            return FileUtils.ONE_GB + size;
        } else {
            return size;
        }
    }
}

From source file:nl.opengeogroep.filesetsync.FileRecord.java

public static String calculateHashMappedIO(File f) throws FileNotFoundException, IOException {
    try (RandomAccessFile raf = new RandomAccessFile(f, "r"); FileChannel channel = raf.getChannel();) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        buffer.load();

        MessageDigest md = DigestUtils.getMd5Digest();
        md.update(buffer);/*from ww  w.  j  a  va 2s  .  com*/
        byte[] digest = md.digest();

        return Hex.encodeHexString(digest);
    }
}

From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java

private boolean checkVersion(FileChannel channel) throws IOException {
    if (channel.size() > 0) {
        channel.position(0);//  w w w  . ja  v a2s  .co  m
        ByteBuffer buffer;

        if (useNIOMemoryMapping) {
            MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, 8);
            mbb.load();
            buffer = mbb;
        } else {
            buffer = ByteBuffer.wrap(new byte[8]);
            channel.read(buffer);
            buffer.position(0);
        }

        buffer.position(0);
        long onDiskVersion = buffer.getLong();
        return (version == onDiskVersion);
    }
    return (version == 0);
}

From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java

private void setStatusFromFile(FileChannel channel) throws IOException {
    if (channel.size() > 0) {
        channel.position(0);/*  w  ww .  j  ava  2 s . c  om*/
        ByteBuffer buffer;

        if (useNIOMemoryMapping) {
            MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, channel.size());
            mbb.load();
            buffer = mbb;
        } else {
            buffer = ByteBuffer.wrap(new byte[(int) channel.size()]);
            channel.read(buffer);
            buffer.position(0);
        }

        buffer.position(0);
        long onDiskVersion = buffer.getLong();
        if (version != onDiskVersion) {
            CRC32 crc32 = new CRC32();
            crc32.update((int) (onDiskVersion >>> 32) & 0xFFFFFFFF);
            crc32.update((int) (onDiskVersion >>> 0) & 0xFFFFFFFF);
            int size = buffer.getInt();
            crc32.update(size);
            LinkedHashMap<String, IndexEntry> newIndexEntries = new LinkedHashMap<String, IndexEntry>();
            // Not all state is saved some is specific to this index so we
            // need to add the transient stuff.
            // Until things are committed they are not shared unless it is
            // prepared
            for (int i = 0; i < size; i++) {
                String indexTypeString = readString(buffer, crc32);
                IndexType indexType;
                try {
                    indexType = IndexType.valueOf(indexTypeString);
                } catch (IllegalArgumentException e) {
                    throw new IOException("Invalid type " + indexTypeString);
                }

                String name = readString(buffer, crc32);

                String parentName = readString(buffer, crc32);

                String txStatus = readString(buffer, crc32);
                TransactionStatus status;
                try {
                    status = TransactionStatus.valueOf(txStatus);
                } catch (IllegalArgumentException e) {
                    throw new IOException("Invalid status " + txStatus);
                }

                String mergeId = readString(buffer, crc32);

                long documentCount = buffer.getLong();
                crc32.update((int) (documentCount >>> 32) & 0xFFFFFFFF);
                crc32.update((int) (documentCount >>> 0) & 0xFFFFFFFF);

                long deletions = buffer.getLong();
                crc32.update((int) (deletions >>> 32) & 0xFFFFFFFF);
                crc32.update((int) (deletions >>> 0) & 0xFFFFFFFF);

                byte deleteOnlyNodesFlag = buffer.get();
                crc32.update(deleteOnlyNodesFlag);
                boolean isDeletOnlyNodes = deleteOnlyNodesFlag == 1;

                if (!status.isTransient()) {
                    newIndexEntries.put(name, new IndexEntry(indexType, name, parentName, status, mergeId,
                            documentCount, deletions, isDeletOnlyNodes));
                }
            }
            long onDiskCRC32 = buffer.getLong();
            if (crc32.getValue() == onDiskCRC32) {
                for (IndexEntry entry : indexEntries.values()) {
                    if (entry.getStatus().isTransient()) {
                        newIndexEntries.put(entry.getName(), entry);
                    }
                }
                version = onDiskVersion;
                indexEntries = newIndexEntries;
            } else {
                throw new IOException("Invalid file check sum");
            }
        }
    }

}

From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java

private void writeStatusToFile(FileChannel channel) throws IOException {
    long size = getBufferSize();

    ByteBuffer buffer;/*from  w  w  w.j a  v  a2 s  .  com*/
    if (useNIOMemoryMapping) {
        MappedByteBuffer mbb = channel.map(MapMode.READ_WRITE, 0, size);
        mbb.load();
        buffer = mbb;
    } else {
        channel.truncate(size);
        buffer = ByteBuffer.wrap(new byte[(int) size]);
    }

    buffer.position(0);

    buffer.putLong(version);
    CRC32 crc32 = new CRC32();
    crc32.update((int) (version >>> 32) & 0xFFFFFFFF);
    crc32.update((int) (version >>> 0) & 0xFFFFFFFF);

    buffer.putInt(indexEntries.size());
    crc32.update(indexEntries.size());

    for (IndexEntry entry : indexEntries.values()) {
        String entryType = entry.getType().toString();
        writeString(buffer, crc32, entryType);

        writeString(buffer, crc32, entry.getName());

        writeString(buffer, crc32, entry.getParentName());

        String entryStatus = entry.getStatus().toString();
        writeString(buffer, crc32, entryStatus);

        writeString(buffer, crc32, entry.getMergeId());

        buffer.putLong(entry.getDocumentCount());
        crc32.update((int) (entry.getDocumentCount() >>> 32) & 0xFFFFFFFF);
        crc32.update((int) (entry.getDocumentCount() >>> 0) & 0xFFFFFFFF);

        buffer.putLong(entry.getDeletions());
        crc32.update((int) (entry.getDeletions() >>> 32) & 0xFFFFFFFF);
        crc32.update((int) (entry.getDeletions() >>> 0) & 0xFFFFFFFF);

        buffer.put(entry.isDeletOnlyNodes() ? (byte) 1 : (byte) 0);
        crc32.update(entry.isDeletOnlyNodes() ? new byte[] { (byte) 1 } : new byte[] { (byte) 0 });
    }
    buffer.putLong(crc32.getValue());

    if (useNIOMemoryMapping) {
        ((MappedByteBuffer) buffer).force();
    } else {
        buffer.rewind();
        channel.position(0);
        channel.write(buffer);
    }
}

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   ww w . j a  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);
    }
}