-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMappedByteBufferDemo.java
More file actions
40 lines (36 loc) · 1.02 KB
/
Copy pathMappedByteBufferDemo.java
File metadata and controls
40 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;
public class MappedByteBufferDemo{
public static void main(String[] args) throws IOException{
if(args.length != 1){
System.err.println("usage: java MappedByteBufferDemo file");
return;
}
RandomAccessFile raf = new RandomAccessFile(args[0], "rw");
FileChannel fc = raf.getChannel();
long size = fc.size();
System.out.println("Size: " + size);
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.PRIVATE, 0, size);
while(mbb.hasRemaining()){
System.out.print((char)mbb.get());
}
System.out.println();
System.out.println();
// reverse the file content.
for(int i=0; i<mbb.limit()/2; i++){
int mirrorIndex = mbb.limit()-1-i;
byte b1 = mbb.get(i);
byte b2 = mbb.get(mirrorIndex);
mbb.put(i, b2);
mbb.put(mirrorIndex, b1);
}
// read the buffer content again.
mbb.flip();
while(mbb.hasRemaining()){
System.out.print((char)mbb.get());
}
fc.close();
}
}