Example usage for java.nio.channels FileChannel map

List of usage examples for java.nio.channels FileChannel map

Introduction

In this page you can find the example usage for java.nio.channels FileChannel map.

Prototype

public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;

Source Link

Document

Maps a region of this channel's file directly into memory.

Usage

From source file:Main.java

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));//from   w w  w. j a va  2 s  . c  o m
        mbb.put(j, b);
    }
    fc.close();
}

From source file:MainClass.java

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();/*from  ww  w. j  av a  2 s.c  o  m*/
    buffer.flip();

    System.out.println("hasArray=" + buffer.hasArray());
    System.out.println(buffer.toString());

    System.out.flush();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Pattern pattern = Pattern.compile("pattern");
    FileInputStream input = new FileInputStream("file.txt");
    FileChannel channel = input.getChannel();

    ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);

    Matcher matcher = pattern.matcher(cbuf);
    while (matcher.find()) {
        String match = matcher.group();
        System.out.println(match);
    }/*from w  w w.  j  av  a  2  s .c om*/
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    if (args.length < 2) {
        System.err.println("Usage: java MainClass inFile1 inFile2... outFile");
        return;//w  ww  .  ja  va2s  .  co  m
    }

    ByteBuffer[] buffers = new ByteBuffer[args.length - 1];
    for (int i = 0; i < args.length - 1; i++) {
        RandomAccessFile raf = new RandomAccessFile(args[i], "r");
        FileChannel channel = raf.getChannel();
        buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
    }

    FileOutputStream outFile = new FileOutputStream(args[args.length - 1]);
    FileChannel out = outFile.getChannel();
    out.write(buffers);
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile("temp.tmp", "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    for (int i = 0; i < 10; i++)
        ib.put(i);/* w ww . ja  v  a 2  s  . c o m*/
    fc.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");

    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
    ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
    while (ib.hasRemaining())
        ib.get();// w ww .  jav a  2s  .c  o m
    fc.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    ib.put(0);/*  www .  j a v  a  2s.  c o m*/
    for (int i = 1; i < 10; i++)
        ib.put(ib.get(i - 1));
    fc.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    buf.force();/*from   w ww  .j  a v  a 2 s .c  om*/

    channel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileChannel rwChannel = new RandomAccessFile(new File("test.txt"), "rw").getChannel();
    ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());
}