Java Tutorial/File/MappedByteBuffer

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

Create MappedByteBuffer from FileInputStream

   <source lang="java">

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++));
 }

}</source>





Create Read only buffer

   <source lang="java">

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++));
 }

}</source>





Creating a very large file using mapping

   <source lang="java">

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));
 }

}</source>





Get MappedByteBuffer from FileChannel

   <source lang="java">

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();
 }

}</source>





Locking portions of a mapped file

   <source lang="java">

// 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);
     }
   }
 }

}</source>





Mapping an entire file into memory for reading

   <source lang="java">

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++));
 }

}</source>





Read file upside/down with RandomAccessFile

   <source lang="java">

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();
 }

}</source>





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

   <source lang="java">

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++));
 }

}</source>