Java Tutorial/File/MappedByteBuffer — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:19, 1 июня 2010

Create MappedByteBuffer from FileInputStream

import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  private static final int LENGTH = 100; // Small
  public static void main(String[] args) throws Exception {
    MappedByteBuffer in = new FileInputStream("test.txt").getChannel().map(
        FileChannel.MapMode.READ_ONLY, 0, LENGTH);
    int i = 0;
    while (i < LENGTH)
      System.out.print((char) in.get(i++));
    System.out.println((char) in.get(i++));
  }
}





Create Read only buffer

import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  private static final int LENGTH = 100; // Small
  public static void main(String[] args) throws Exception {
    MappedByteBuffer in = new FileInputStream("test.txt").getChannel().map(
        FileChannel.MapMode.READ_ONLY, 0, LENGTH);
    int i = 0;
    while (i < LENGTH)
      System.out.print((char) in.get(i++));
    System.out.println((char) in.get(i++));
  }
}





Creating a very large file using mapping

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  static int length = 0x8FFFFFF; // 128 Mb
  public static void main(String[] args) throws Exception {
    MappedByteBuffer out = new RandomAccessFile("test.dat", "rw").getChannel().map(
        FileChannel.MapMode.READ_WRITE, 0, length);
    for (int i = 0; i < length; i++)
      out.put((byte) "x");
    System.out.println("Finished writing");
    for (int i = length / 2; i < length / 2 + 6; i++)
      System.out.print((char) out.get(i));
  }
}





Get MappedByteBuffer from FileChannel

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String[] argv) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "r");
    FileChannel fc = raf.getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    buffer.clear();
    buffer.flip();
    System.out.println("hasArray=" + buffer.hasArray());
    System.out.println(buffer.toString());
    System.out.flush();
  }
}





Locking portions of a mapped file

// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
// Locking portions of a mapped file.
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class MainClass {
  static final int LENGTH = 0x8FFFFFF; // 128 Mb
  static FileChannel fc;
  public static void main(String[] args) throws Exception {
    fc = new RandomAccessFile("test.dat", "rw").getChannel();
    MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH);
    for (int i = 0; i < LENGTH; i++)
      out.put((byte) "x");
    new LockAndModify(out, 0, 0 + LENGTH / 3);
    new LockAndModify(out, LENGTH / 2, LENGTH / 2 + LENGTH / 4);
  }
  private static class LockAndModify extends Thread {
    private ByteBuffer buff;
    private int start, end;
    LockAndModify(ByteBuffer mbb, int start, int end) {
      this.start = start;
      this.end = end;
      mbb.limit(end);
      mbb.position(start);
      buff = mbb.slice();
      start();
    }
    public void run() {
      try {
        FileLock fl = fc.lock(start, end, false);
        System.out.println("Locked: " + start + " to " + end);
        while (buff.position() < buff.limit() - 1){
          buff.put((byte) (buff.get() + 1));
        }
        fl.release();
        System.out.println("Released: " + start + " to " + end);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }
}





Mapping an entire file into memory for reading

import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String[] args) throws Exception {
    long length = new File("test.txt").length();
    MappedByteBuffer in = new FileInputStream("test.txt").getChannel().map(
        FileChannel.MapMode.READ_ONLY, 0, length);
    int i = 0;
    while (i < length)
      System.out.print((char) in.get(i++));
  }
}





Read file upside/down with RandomAccessFile

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
  public static void main(String[] args) throws Exception {
    RandomAccessFile f = new RandomAccessFile("hello.txt", "rw");
    FileChannel fc = f.getChannel();
    MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, f.length());
    int len = (int) f.length();
    for (int i = 0, j = len - 1; i < j; i++, j--) {
      byte b = mbb.get(i);
      mbb.put(i, mbb.get(j));
      mbb.put(j, b);
    }
    fc.close();
  }
}





What happens when the entire file isn"t in your mapping region?

import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  private static final int LENGTH = 100; // Small
  public static void main(String[] args) throws Exception {
    MappedByteBuffer in = new FileInputStream("test.txt").getChannel().map(
        FileChannel.MapMode.READ_ONLY, 0, LENGTH);
    int i = 0;
    while (i < LENGTH)
      System.out.print((char) in.get(i++));
    System.out.println((char) in.get(i++));
  }
}