Java File read to ByteBuffer

Description

Java File read to ByteBuffer


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

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static void main(String[] argv) throws Exception {
        File file = new File("Main.java");
        System.out.println(fromFile(file));
    }// w ww.  j  a v  a  2 s  . co m

    public static ByteBuffer fromFile(File file) throws IOException {
        RandomAccessFile raf = null;
        FileChannel channel = null;
        try {
            raf = new RandomAccessFile(file, "r");
            channel = raf.getChannel();
            return channel.map(FileChannel.MapMode.READ_ONLY, 0,
                    file.length()).load();
        } finally {
            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    // Ignored.
                }
            }
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    // Ignored.
                }
            }
        }
    }
}

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("Main.java");
    System.out.println(readFile(file));
  }/* w ww  .ja v  a2s.  com*/

  /**
   * Reads the contents of the given File into a ByteBuffer. This ByteBuffer is
   * always backed by a byte[] of exactly the file's length (at the time we
   * started to read).
   */
  public static ByteBuffer readFile(File file) throws IOException {
    DataInputStream dataInputStream = null;
    try {
      // FIXME: this is broken for files larger than 4GiB.
      int byteCount = (int) file.length();

      // Always read the whole file in rather than using memory mapping.
      // Windows' file system semantics also mean that there's a period after a search
      // finishes but before the buffer is actually unmapped where you can't write to
      // the file (see Sun bug 6359560).
      // Being unable to manually unmap causes no functional problems but hurts
      // performance on Unix (see Sun bug 4724038).
      // Testing in C (working on Ctags) shows that for typical source files (Linux
      // 2.6.17 and JDK6), the performance benefit of mmap(2) is small anyway.
      // Evergreen actually searches both of those source trees faster with readFully
      // than with map.
      // At the moment, then, there's no obvious situation where we should map the
      // file.
      FileInputStream fileInputStream = new FileInputStream(file);
      dataInputStream = new DataInputStream(fileInputStream);
      final byte[] bytes = new byte[byteCount];
      dataInputStream.readFully(bytes);

      return ByteBuffer.wrap(bytes);
    } finally {
      dataInputStream.close();
    }
  }
}   



PreviousNext

Related