All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.shanqiang.offheap.buffer.ByteBufferOffheap Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
package io.github.shanqiang.offheap.buffer;

import io.github.shanqiang.offheap.BufferOffheap;
import io.github.shanqiang.offheap.InternalUnsafe;
import org.openjdk.jol.info.ClassLayout;

import static io.github.shanqiang.offheap.InternalUnsafe.copyMemory;
import static io.github.shanqiang.offheap.InternalUnsafe.putByte;

public class ByteBufferOffheap extends BufferOffheap
{
    private static final int INSTANCE_SIZE = ClassLayout.parseClass(ByteBufferOffheap.class).instanceSize();

    public ByteBufferOffheap(long size)
    {
        super(size);
    }

    private ByteBufferOffheap copyFrom(ByteBufferOffheap from, long size) {
        copyMemory(from.addr, addr, size);
        return this;
    }

    public ByteBufferOffheap copy(long newSize) {
        return new ByteBufferOffheap(newSize).copyFrom(this, this.size);
    }

    public void set(long index, byte value) {
        if (index < 0) {
            throw new IllegalArgumentException();
        }
        if ((index + 1) * Byte.BYTES > size) {
            throw new IndexOutOfBoundsException();
        }
        putByte(addr + index * Byte.BYTES, value);
    }

    public byte get(long index) {
        if (index < 0) {
            throw new IllegalArgumentException();
        }
        if ((index + 1) * Byte.BYTES > size) {
            throw new IndexOutOfBoundsException();
        }

        return InternalUnsafe.getByte(addr + index * Byte.BYTES);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy