Java Convert via ByteBuffer toMappedBuffer(File file)

Here you can find the source of toMappedBuffer(File file)

Description

to Mapped Buffer

License

Open Source License

Declaration

public static ByteBuffer toMappedBuffer(File file) throws IOException 

Method Source Code


//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.io.File;
import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel.MapMode;

public class Main {
    public static ByteBuffer toMappedBuffer(File file) throws IOException {
        RandomAccessFile raf = null;
        try {//  ww  w  .  jav  a  2s  .co m
            raf = new RandomAccessFile(file, "r");
            return raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
        } finally {
            if (raf != null) {
                raf.close();
            }

        }
    }

    /** Get remaining from null checked buffer
     * @param buffer The buffer to get the remaining from, in flush mode.
     * @return 0 if the buffer is null, else the bytes remaining in the buffer.
     */
    public static int length(ByteBuffer buffer) {
        return buffer == null ? 0 : buffer.remaining();
    }
}

Related

  1. toLong(byte[] bytes)
  2. toLongArray(byte[] b)
  3. ToLongArray(byte[] data)
  4. toLongArray(byte[] ids)
  5. toMappedBuffer(File file)
  6. toMappedBuffer(File file, long start, long end)
  7. toMsftBinary(UUID uuid)
  8. toShort(byte firstByte, byte secondByte)
  9. toShort(byte[] bytes, int index)