ikorennoy / jasyncfio

Java asynchronous file I/O based on io_uring Linux interface
Apache License 2.0
71 stars 10 forks source link

You can use ".alignedSlice()" to allocate aligned ByteBuffer instead of manual alignment #61

Closed GavinRay97 closed 1 year ago

GavinRay97 commented 1 year ago

Just a tip:

    private static void testWrite(Path p) throws Exception {
        try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE,
             ExtendedOpenOption.DIRECT)) {
            FileStore fs = Files.getFileStore(p);
            int alignment = (int)fs.getBlockSize();
            ByteBuffer src = ByteBuffer.allocateDirect(PAGE_SIZE + alignment - 1)
                                       .alignedSlice(alignment);
            for (int j = 0; j < SIZE; j++) {
                src.put((byte)0);
            }
            src.flip();
            fc.write(src);
        }
    }

    private static void testRead(Path p) throws Exception {
        try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) {
            FileStore fs = Files.getFileStore(p);
            int alignment = (int)fs.getBlockSize();
            ByteBuffer dest = ByteBuffer.allocateDirect(PAGE_SIZE + alignment - 1)
                                        .alignedSlice(alignment);
            fc.read(dest);
        }
    }
ikorennoy commented 1 year ago

@GavinRay97, I want to support Java 8, but this method was added in Java 9. It might not make much sense to support Java 8 and I will switch to Java 11 and use this method.

Anyway thanks:)