Java File Read via ByteBuffer mapReadWrite(File file)

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

Description

This is a convenience method for mapping an entire File into a ByteBuffer .

License

Open Source License

Parameter

Parameter Description
file The File to map.

Return

Returns a new .

Declaration

public static ByteBuffer mapReadWrite(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class Main {
    /**// w ww  .  j  a v  a  2  s  .  c  o m
     * This is a convenience method for mapping an entire {@link File} into a
     * {@link ByteBuffer}.
     *
     * @param file The {@link File} to map.
     * @return Returns a new {@link ByteBuffer}.
     * @see #map(File,FileChannel.MapMode)
     * @see #map(File,long,long,FileChannel.MapMode)
     * @see #mapReadOnly(File)
     * @see #mapReadOnly(File,long,long)
     * @see #mapReadWrite(File,long,long)
     */
    public static ByteBuffer mapReadWrite(File file) throws IOException {
        return mapReadWrite(file, 0, file.length());
    }

    /**
     * This is a convenience method for mapping a {@link File} into a
     * {@link ByteBuffer}.
     *
     * @param file The {@link File} to map.
     * @param position The position within the file at which the mapped region
     * is to start.
     * @param size The size of the region to be mapped.
     * @return Returns a new {@link ByteBuffer}.
     * @see #map(File,FileChannel.MapMode)
     * @see #map(File,long,long,FileChannel.MapMode)
     * @see #mapReadOnly(File)
     * @see #mapReadOnly(File,long,long)
     * @see #mapReadWrite(File)
     */
    public static ByteBuffer mapReadWrite(File file, long position, long size) throws IOException {
        return map(file, position, size, FileChannel.MapMode.READ_WRITE);
    }

    /**
     * This is a convenience method for mapping an entire {@link File} into a
     * {@link ByteBuffer}.
     *
     * @param file The {@link File} to map.
     * @return Returns a new {@link ByteBuffer}.
     * @see #map(File,long,long,FileChannel.MapMode)
     * @see #mapReadOnly(File)
     * @see #mapReadOnly(File,long,long)
     * @see #mapReadWrite(File)
     * @see #mapReadWrite(File,long,long)
     */
    public static ByteBuffer map(File file, FileChannel.MapMode mapMode) throws IOException {
        return map(file, 0, file.length(), mapMode);
    }

    /**
     * This is a convenience method for mapping a {@link File} into a
     * {@link ByteBuffer}.
     *
     * @param file The {@link File} to map.
     * @param position The position within the file at which the mapped region
     * is to start.
     * @param size The size of the region to be mapped.
     * @param mapMode The {@link FileChannel.MapMode} to use.
     * @return Returns a new {@link ByteBuffer}.
     * @see #map(File,FileChannel.MapMode)
     * @see #mapReadOnly(File)
     * @see #mapReadOnly(File,long,long)
     * @see #mapReadWrite(File)
     * @see #mapReadWrite(File,long,long)
     */
    public static ByteBuffer map(File file, long position, long size, FileChannel.MapMode mapMode)
            throws IOException {
        final String rafMode;
        if (mapMode == FileChannel.MapMode.READ_ONLY)
            rafMode = "r";
        else if (mapMode == FileChannel.MapMode.READ_WRITE)
            rafMode = "rw";
        else
            throw new IllegalArgumentException("unsupported MapMode");

        final RandomAccessFile raf = new RandomAccessFile(file, rafMode);
        try {
            return raf.getChannel().map(mapMode, position, size);
        } finally {
            raf.close();
        }
    }
}

Related

  1. copyStream(BufferedReader reader, BufferedWriter writer)
  2. count(final ReadableByteChannel src)
  3. getReadBuffer(File file)
  4. getStringContents(ReadableByteChannel channel)
  5. largeFileReader(String filename)
  6. nioCopy(ReadableByteChannel input, WritableByteChannel output)
  7. openReadOnly(Path path, int offset, int length)
  8. read(DataInput in, int length)
  9. read(File file)