MCPcopy Create free account
hub / github.com/e2wugui/zeze / testMmap

Method testMmap

ZezeJava/ZezeJavaTest/src/TestLog4jQuery/TestMmap.java:23–100  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21 public final static int ePageMask = ePageSize - 1;
22
23 @Test
24 public void testMmap() throws Exception {
25 var file = new File("testMmapFile");
26 new FileOutputStream(file).close(); // create or truncate 0
27 var pExists = file.toPath();
28 var pMmap = Path.of(file.getName() + ".mmap");
29 if (!pMmap.toFile().exists())
30 Files.createLink(pMmap, pExists);
31 var fileMmap = pMmap.toFile();
32 try (var channel = new RandomAccessFile(fileMmap, "rw").getChannel()) {
33 final var fileSize = channel.size();
34 if (fileSize > 0) {
35 var dst = ByteBuffer.allocate((int)fileSize);
36 channel.read(dst);
37 dst.flip();
38 while (dst.remaining() > 0) {
39 System.out.print(dst.get());
40 System.out.print(",");
41 }
42 System.out.println("------");
43 }
44 final var lastPageSize = (int)(fileSize & ePageMask);
45 final var mapSize = fileSize + ePageSize - lastPageSize;
46 final var mmap = channel.map(FileChannel.MapMode.READ_WRITE, 0, mapSize);
47 mmap.position((int)fileSize);
48 mmap.put((byte)1);
49 mmap.put((byte)3);
50 mmap.put((byte)5);
51 }
52
53 if (fileMmap.delete())
54 System.out.println("delete link ok.");
55
56 MappedByteBuffer last;
57 try (var channel = new RandomAccessFile(fileMmap, "rw").getChannel()) {
58 final var fileSize = channel.size();
59 if (fileSize > 0) {
60 var dst = ByteBuffer.allocate((int)fileSize);
61 channel.read(dst);
62 dst.flip();
63 while (dst.remaining() > 0) {
64 System.out.print(dst.get());
65 System.out.print(",");
66 }
67 System.out.println("------");
68 }
69 final var lastPageSize = (int)(fileSize & ePageMask);
70 final var mapSize = fileSize + ePageSize - lastPageSize;
71 final var mmap = channel.map(FileChannel.MapMode.READ_WRITE, 0, mapSize);
72 mmap.position((int)fileSize);
73 mmap.put((byte)2);
74 mmap.put((byte)4);
75 mmap.put((byte)6);
76 last = mmap;
77 }
78
79 try (var channel = new RandomAccessFile(fileMmap, "rw").getChannel()) {
80 final var fileSize = channel.size();

Callers

nothing calls this directly

Calls 12

getChannelMethod · 0.80
closeMethod · 0.65
getNameMethod · 0.65
sizeMethod · 0.65
getMethod · 0.65
putMethod · 0.65
ofMethod · 0.45
allocateMethod · 0.45
readMethod · 0.45
printMethod · 0.45
mapMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected