Java : MappedByteBuffer 示例

Java 中的 MappedByteBuffer (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 MappedByteBuffer 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

直接字节缓冲区,其内容是文件的内存映射区域。 (机器翻译)

Class diagram

public MappedByteBuffer createBuffer(Path file) throws IOException {
    try (final var channel = FileChannel.open(file,
            StandardOpenOption.READ, StandardOpenOption.WRITE)) {

        return channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());
    }
}
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

final var buffer = createBuffer(file);

{
    final var dst = new byte[buffer.capacity()];
    buffer.get(0, dst);
    System.out.println(Arrays.toString(dst)); // [10, 20, 30, 40, 50]

    final var ret = Files.readAllBytes(file);
    System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
}

buffer.put(0, (byte) -10);
buffer.put(1, (byte) -20);
buffer.put(2, (byte) -30);

{
    final var dst = new byte[buffer.capacity()];
    buffer.get(0, dst);
    System.out.println(Arrays.toString(dst)); // [-10, -20, -30, 40, 50]

    final var ret = Files.readAllBytes(file);
    System.out.println(Arrays.toString(ret)); // [-10, -20, -30, 40, 50]
}
final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file,
        StandardOpenOption.READ, StandardOpenOption.WRITE)) {

    final var buffer = channel.map(
            FileChannel.MapMode.READ_ONLY, 0, channel.size());

    {
        final var dst = new byte[buffer.capacity()];
        buffer.get(0, dst);
        System.out.println(Arrays.toString(dst)); // [10, 20, 30, 40, 50]

        final var ret = Files.readAllBytes(file);
        System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]
    }

    final var src = ByteBuffer.allocate(3)
            .put((byte) -10)
            .put((byte) -20)
            .put((byte) -30)
            .clear();

    System.out.println(channel.write(src)); // 3

    {
        final var dst = new byte[buffer.capacity()];
        buffer.get(0, dst);
        System.out.println(Arrays.toString(dst)); // [-10, -20, -30, 40, 50]

        final var ret = Files.readAllBytes(file);
        System.out.println(Arrays.toString(ret)); // [-10, -20, -30, 40, 50]
    }
}

Methods

final MappedByteBuffer clear ()

清除此缓冲区。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    buffer.position(2);
    buffer.limit(4);

    System.out.println(buffer); // java.nio.DirectByteBufferR[pos=2 lim=4 cap=5]

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.hasRemaining()); // false

    System.out.println(buffer.clear()); // java.nio.DirectByteBufferR[pos=0 lim=5 cap=5]

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
    System.out.println(buffer.hasRemaining()); // false
}

abstract MappedByteBuffer compact ()

压缩此缓冲区(可选操作)。 (机器翻译)

final var file = Path.of("R:", "java-work", "test1.data");
System.out.println(file); // R:\java-work\test1.data

final byte[] bytes = {10, 20, 30, 40, 50, 60};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file,
        StandardOpenOption.READ, StandardOpenOption.WRITE)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());
    buffer.position(3);
    System.out.println(buffer.compact()); // java.nio.DirectByteBuffer[pos=3 lim=6 cap=6]
}

final var ret = Files.readAllBytes(file);
System.out.println(Arrays.toString(ret)); // [40, 50, 60, 40, 50, 60]
final var file = Path.of("R:", "java-work", "test2.data");
System.out.println(file); // R:\java-work\test2.data

final byte[] bytes = {10, 20, 30, 40, 50, 60};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file,
        StandardOpenOption.READ, StandardOpenOption.WRITE)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());

    buffer.position(2);
    System.out.println(buffer.compact()); // java.nio.DirectByteBuffer[pos=4 lim=6 cap=6]
}

final var ret = Files.readAllBytes(file);
System.out.println(Arrays.toString(ret)); // [30, 40, 50, 60, 50, 60]
final var file = Path.of("R:", "java-work", "test3.data");
System.out.println(file); // R:\java-work\test3.data

final byte[] bytes = {10, 20, 30, 40, 50, 60};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file,
        StandardOpenOption.READ, StandardOpenOption.WRITE)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());

    buffer.position(4);
    System.out.println(buffer.compact()); // java.nio.DirectByteBuffer[pos=2 lim=6 cap=6]
}

final var ret = Files.readAllBytes(file);
System.out.println(Arrays.toString(ret)); // [50, 60, 30, 40, 50, 60]

abstract MappedByteBuffer duplicate ()

创建一个共享此缓冲区内容的新字节缓冲区。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    buffer.position(1);
    buffer.limit(4);

    final var duplicated = buffer.duplicate();

    System.out.println(buffer); // java.nio.DirectByteBufferR[pos=1 lim=4 cap=5]
    System.out.println(duplicated); // java.nio.DirectByteBufferR[pos=1 lim=4 cap=5]

    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40

    System.out.println(duplicated.get()); // 20
    System.out.println(duplicated.get()); // 30
    System.out.println(duplicated.get()); // 40
}

final MappedByteBuffer flip ()

翻转此缓冲区。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30

    System.out.println(buffer); // java.nio.DirectByteBufferR[pos=3 lim=5 cap=5]
    System.out.println(buffer.flip()); // java.nio.DirectByteBufferR[pos=0 lim=3 cap=5]

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
}

final MappedByteBuffer force ()

强制将对此缓冲区内容所做的任何更改写入包含映射文件的存储设备。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final var channel = FileChannel.open(file, StandardOpenOption.READ,
        StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 3);

    {
        final var bytes = Files.readAllBytes(file);
        System.out.println(Arrays.toString(bytes)); // [0, 0, 0]
    }

    buffer.put((byte) 10);
    buffer.put((byte) 20);
    buffer.put((byte) 30);

    {
        // NOTE : In my environment, writing to the file is completed
        // before the force method is called.
        final var bytes = Files.readAllBytes(file);
        System.out.println(Arrays.toString(bytes)); // [10, 20, 30]
    }

    final var ret = buffer.force();
    System.out.println(buffer.equals(ret)); // true

    {
        final var bytes = Files.readAllBytes(file);
        System.out.println(Arrays.toString(bytes)); // [10, 20, 30]
    }
}

final MappedByteBuffer force (int index, int length)

强制将对此缓冲区内容区域所做的任何更改写入包含映射文件的存储设备。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

try (final var channel = FileChannel.open(file, StandardOpenOption.READ,
        StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);

    {
        final var bytes = Files.readAllBytes(file);
        System.out.println(Arrays.toString(bytes)); // [0, 0, 0, 0, 0]
    }

    buffer.put((byte) 10);
    buffer.put((byte) 20);
    buffer.put((byte) 30);

    {
        // NOTE : In my environment, writing to the file is completed
        // before the force method is called.
        final var bytes = Files.readAllBytes(file);
        System.out.println(Arrays.toString(bytes)); // [10, 20, 30, 0, 0]
    }

    final var pos = buffer.position();
    System.out.println(pos); // 3

    final var ret = buffer.force(0, pos);
    System.out.println(buffer.equals(ret)); // true

    {
        final var bytes = Files.readAllBytes(file);
        System.out.println(Arrays.toString(bytes)); // [10, 20, 30, 0, 0]
    }
}

final boolean isLoaded ()

告知此缓冲区的内容是否驻留在物理内存中。 (机器翻译)

final var os = System.getProperty("os.name");
System.out.println(os); // Windows 10

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer.isLoaded()); // false

    final var ret = buffer.load();
    System.out.println(buffer.equals(ret)); // true

    // The isLoaded method always returns false on Windows ...?
    System.out.println(buffer.isLoaded()); // false
}

final MappedByteBuffer limit (int newLimit)

设置此缓冲区的限制。 (机器翻译)

final var file = Path.of("R:", "java-work", "test1.data");
System.out.println(file); // R:\java-work\test1.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer); // java.nio.DirectByteBufferR[pos=0 lim=5 cap=5]
    System.out.println(buffer.limit()); // 5

    System.out.println(buffer.limit(3)); // java.nio.DirectByteBufferR[pos=0 lim=3 cap=5]
    System.out.println(buffer.limit()); // 3

    System.out.println(buffer.clear()); // java.nio.DirectByteBufferR[pos=0 lim=5 cap=5]
    System.out.println(buffer.limit()); // 5
}
final var file = Path.of("R:", "java-work", "test2.data");
System.out.println(file); // R:\java-work\test2.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer.limit(3)); // java.nio.DirectByteBufferR[pos=0 lim=3 cap=5]

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30

    try {
        var _ = buffer.get();
    } catch (BufferUnderflowException e) {
        System.out.println("BufferUnderflowException!");
    }

    // Result
    // ↓
    //BufferUnderflowException!
}

final MappedByteBuffer load ()

将此缓冲区的内容加载到物理内存中。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    {
        // NOTE : In my environment, reading from the file is completed
        // before the load method is called.
        final var dst = new byte[buffer.capacity()];
        buffer.get(0, dst);
        System.out.println(Arrays.toString(dst)); // [10, 20, 30, 40, 50]
    }

    final var ret = buffer.load();
    System.out.println(buffer.equals(ret)); // true

    {
        final var dst = new byte[buffer.capacity()];
        buffer.get(0, dst);
        System.out.println(Arrays.toString(dst)); // [10, 20, 30, 40, 50]
    }
}

final MappedByteBuffer mark ()

在此缓冲区的位置设置其标记。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20

    System.out.println(buffer.position()); // 2
    System.out.println(buffer.mark()); // java.nio.DirectByteBufferR[pos=2 lim=5 cap=5]

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50

    System.out.println(buffer.position()); // 5
    System.out.println(buffer.reset()); // java.nio.DirectByteBufferR[pos=2 lim=5 cap=5]

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
}

final MappedByteBuffer position (int newPosition)

设置此缓冲区的位置。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer.position()); // 0

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.position()); // 1

    System.out.println(buffer.get()); // 20
    System.out.println(buffer.position()); // 2

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.position()); // 3

    System.out.println(buffer.get()); // 40
    System.out.println(buffer.position()); // 4

    System.out.println(buffer.get()); // 50
    System.out.println(buffer.position()); // 5

    // java.nio.DirectByteBufferR[pos=3 lim=5 cap=5]
    System.out.println(buffer.position(3));

    System.out.println(buffer.get()); // 40
    System.out.println(buffer.position()); // 4

    System.out.println(buffer.get()); // 50
    System.out.println(buffer.position()); // 5
}

final MappedByteBuffer reset ()

将此缓冲区的位置重置为先前标记的位置。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20

    System.out.println(buffer.position()); // 2
    System.out.println(buffer.mark()); // java.nio.DirectByteBufferR[pos=2 lim=5 cap=5]

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50

    System.out.println(buffer.position()); // 5
    System.out.println(buffer.reset()); // java.nio.DirectByteBufferR[pos=2 lim=5 cap=5]

    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
}

final MappedByteBuffer rewind ()

倒回此缓冲区。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    buffer.position(2);
    buffer.limit(4);

    System.out.println(buffer.position()); // 2
    System.out.println(buffer.limit()); // 4

    System.out.println(buffer.rewind()); // java.nio.DirectByteBufferR[pos=0 lim=4 cap=5]

    System.out.println(buffer.position()); // 0
    System.out.println(buffer.limit()); // 4
}

abstract MappedByteBuffer slice ()

创建一个新的字节缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    buffer.position(1);
    buffer.limit(4);

    final var sliced = buffer.slice();

    System.out.println(buffer); // java.nio.DirectByteBufferR[pos=1 lim=4 cap=5]
    System.out.println(sliced); // java.nio.DirectByteBufferR[pos=0 lim=3 cap=3]

    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.hasRemaining()); // false

    System.out.println(sliced.get()); // 20
    System.out.println(sliced.get()); // 30
    System.out.println(sliced.get()); // 40
    System.out.println(sliced.hasRemaining()); // false
}

abstract MappedByteBuffer slice (int index, int length)

创建一个新的字节缓冲区,其内容是此缓冲区内容的共享子序列。 (机器翻译)

final var file = Path.of("R:", "java-work", "test.data");
System.out.println(file); // R:\java-work\test.data

final byte[] bytes = {10, 20, 30, 40, 50};
Files.write(file, bytes);

try (final var channel = FileChannel.open(file, StandardOpenOption.READ)) {

    final var buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

    final var sliced = buffer.slice(2, 3);

    System.out.println(buffer); // java.nio.DirectByteBufferR[pos=0 lim=5 cap=5]
    System.out.println(sliced); // java.nio.DirectByteBufferR[pos=0 lim=3 cap=3]

    System.out.println(buffer.get()); // 10
    System.out.println(buffer.get()); // 20
    System.out.println(buffer.get()); // 30
    System.out.println(buffer.get()); // 40
    System.out.println(buffer.get()); // 50
    System.out.println(buffer.hasRemaining()); // false

    System.out.println(sliced.get()); // 30
    System.out.println(sliced.get()); // 40
    System.out.println(sliced.get()); // 50
    System.out.println(sliced.hasRemaining()); // false
}

Methods declared in ByteBuffer

alignedSlice, alignmentOffset, allocate, allocateDirect, array, arrayOffset, asCharBuffer, asDoubleBuffer, asFloatBuffer, asIntBuffer, asLongBuffer, asReadOnlyBuffer, asShortBuffer, compareTo, equals, get, get, get, get, get, get, getChar, getChar, getDouble, getDouble, getFloat, getFloat, getInt, getInt, getLong, getLong, getShort, getShort, hasArray, hashCode, isDirect, mismatch, order, order, put, put, put, put, put, put, put, put, putChar, putChar, putDouble, putDouble, putFloat, putFloat, putInt, putInt, putLong, putLong, putShort, putShort, toString, wrap, wrap

请参阅下面的链接。

Methods declared in Buffer

capacity, hasRemaining, isReadOnly, limit, position, remaining

请参阅下面的链接。


相关文章

To top of page