Locking portions of a mapped file : Input Output Stream « File Input Output « Java






Locking portions of a mapped file

Locking portions of a mapped file
// : c12:LockingMappedFiles.java
// Locking portions of a mapped file.
// {RunByHand}
// {Clean: test.dat}
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

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 LockingMappedFiles {
  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 {
        // Exclusive lock with no overlap:
        FileLock fl = fc.lock(start, end, false);
        System.out.println("Locked: " + start + " to " + end);
        // Perform modification:
        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);
      }
    }
  }
} ///:~



           
       








Related examples in the same category

1.Buffer EqualityBuffer Equality
2.Input and output with human-readable text filesInput and output with human-readable text files
3.Input and output of primitive values with binary filesInput and output of primitive values with binary files
4.Input and output of arrays and objects with binary filesInput and output of arrays and objects with binary files
5.Input and output of primitive values with random access binary filesInput and output of primitive values with random access binary files
6.Input and output using strings and string buffersInput and output using strings and string buffers
7.Timing Unbuffered Reads
8.Reading Bytes from a DataInputStreamReading Bytes from a DataInputStream
9.Comparing Buffered and Unbuffered Writing Performance
10.Read a file and print, using LineReader and System.out
11.Demonstrate ProgressMeterInputStream
12.Converting text to and from ByteBuffersConverting text to and from ByteBuffers
13.Demonstrates standard I/O redirection
14.Creating a very large file using mappingCreating a very large file using mapping
15.Demonstrates the use of the File class to create directories and manipulate files
16.Mapping an entire file into memory for reading
17.Mapped IOMapped IO
18.What happens when the entire file isn't in your mapping region
19.Object serializationObject serialization
20.File read and writeFile read and write