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