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

net.openhft.chronicle.algo.bytes.ByteBufferAccessor Maven / Gradle / Ivy

There is a newer version: 2.27ea0
Show newest version
/*
 *     Copyright (C) 2015  higherfrequencytrading.com
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this program.  If not, see .
 */

package net.openhft.chronicle.algo.bytes;

import sun.nio.ch.DirectBuffer;

import java.nio.ByteBuffer;

interface ByteBufferAccessor extends Accessor.Full {

    static ByteBufferAccessor unchecked(ByteBuffer buffer) {
        return buffer.isDirect() ? Direct.INSTANCE : Heap.INSTANCE;
    }

    static ByteBufferAccessor checked() {
        return Generic.INSTANCE;
    }

    enum Direct implements ByteBufferAccessor {
        INSTANCE;

        @Override
        public Access access() {
            return NativeAccess.instance();
        }

        @Override
        public Void handle(ByteBuffer buffer) {
            return null;
        }

        @Override
        public long offset(ByteBuffer buffer, long bufferIndex) {
            return ((DirectBuffer) buffer).address() + bufferIndex;
        }
    }

    enum Heap implements ByteBufferAccessor {
        INSTANCE;

        @Override
        public Access access() {
            return NativeAccess.instance();
        }

        @Override
        public byte[] handle(ByteBuffer buffer) {
            return buffer.array();
        }

        @Override
        public long offset(ByteBuffer buffer, long bufferIndex) {
            return ArrayAccessors.BYTE_BASE + buffer.arrayOffset() + bufferIndex;
        }
    }

    enum Generic implements ByteBufferAccessor {
        INSTANCE;

        @Override
        public Access access() {
            return ByteBufferAccess.INSTANCE;
        }

        @Override
        public ByteBuffer handle(ByteBuffer buffer) {
            return buffer;
        }

        @Override
        public long offset(ByteBuffer buffer, long bufferIndex) {
            return bufferIndex;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy