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

net.quasardb.qdb.Buffer Maven / Gradle / Ivy

Go to download

API for the JNI components of the QuasarDB API for Java. Should not be included directly.

There is a newer version: 3.14.1
Show newest version
package net.quasardb.qdb;

import java.nio.ByteBuffer;
import java.lang.AutoCloseable;

import net.quasardb.qdb.exception.BufferClosedException;
import net.quasardb.qdb.jni.*;

public final class Buffer implements AutoCloseable {
    final Session session;
    ByteBuffer buffer;

    protected Buffer(Session session, ByteBuffer buffer) {
        this.session = session;
        this.buffer = buffer;
    }

    static public Buffer wrap(Session session, ByteBuffer buffer) {
        return buffer != null ? new Buffer(session, buffer) : null;
    }

    static public Buffer wrap(Session session, Reference ref) {
        return wrap(session, ref.value);
    }

    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }

    @Override
    public void close() {
        if (buffer != null) {
            qdb.release(session.handle(), buffer);
            buffer = null;
        }
    }

    public ByteBuffer toByteBuffer() {
        this.throwIfClosed();
        session.throwIfClosed();
        return buffer != null ? buffer.duplicate() : null;
    }

    @Override
    public String toString() {
        if (buffer == null || session.isClosed())
            return this.getClass().getName() + "[closed]";
        else
            return this.getClass().getName() + "[size=" + buffer.limit() + "]";
    }

    private void throwIfClosed() {
        if (buffer == null) {
            throw new BufferClosedException("The buffer is closed");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy