Example usage for io.netty.buffer ByteBuf hasMemoryAddress

List of usage examples for io.netty.buffer ByteBuf hasMemoryAddress

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf hasMemoryAddress.

Prototype

public abstract boolean hasMemoryAddress();

Source Link

Document

Returns true if and only if this buffer has a reference to the low-level memory address that points to the backing data.

Usage

From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java

License:Apache License

/**
 * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library
 * which is faster as it computes using hardware machine instruction else it computes using crc32c algo.
 *
 * @param payload/*from w w  w. java2 s . c  om*/
 * @return
 */
public static int computeChecksum(ByteBuf payload) {
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        return CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        return CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        return CRC32C_HASH.calculate(payload.nioBuffer());
    }
}

From source file:com.scurrilous.circe.checksum.Crc32cIntChecksum.java

License:Apache License

/**
 * Computes incremental checksum with input previousChecksum and input payload
 *
 * @param previousChecksum : previously computed checksum
 * @param payload/* www .j  a v  a 2s . c  om*/
 * @return
 */
public static int resumeChecksum(int previousChecksum, ByteBuf payload) {
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        return CRC32C_HASH.resume(previousChecksum, payload.memoryAddress() + payload.readerIndex(),
                payload.readableBytes());
    } else if (payload.hasArray()) {
        return CRC32C_HASH.resume(previousChecksum, payload.array(),
                payload.arrayOffset() + payload.readerIndex(), payload.readableBytes());
    } else {
        return CRC32C_HASH.resume(previousChecksum, payload.nioBuffer());
    }
}

From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java

License:Apache License

/**
 * Computes crc32c checksum: if it is able to load crc32c native library then it computes using that native library
 * which is faster as it computes using hardware machine instruction else it computes using crc32c algo.
 *
 * @param payload/*from  w  w  w  . j a v a  2s. c o  m*/
 * @return
 */
public static long computeChecksum(ByteBuf payload) {
    int crc;
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        crc = CRC32C_HASH.calculate(payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        crc = CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        crc = CRC32C_HASH.calculate(payload.nioBuffer());
    }
    return crc & 0xffffffffL;
}

From source file:com.scurrilous.circe.checksum.Crc32cLongChecksum.java

License:Apache License

/**
 * Computes incremental checksum with input previousChecksum and input payload
 *
 * @param previousChecksum : previously computed checksum
 * @param payload/*www. j ava  2  s  .  c  o  m*/
 * @return
 */
public static long resumeChecksum(long previousChecksum, ByteBuf payload) {
    int crc = (int) previousChecksum;
    if (payload.hasMemoryAddress() && (CRC32C_HASH instanceof Sse42Crc32C)) {
        crc = CRC32C_HASH.resume(crc, payload.memoryAddress() + payload.readerIndex(), payload.readableBytes());
    } else if (payload.hasArray()) {
        crc = CRC32C_HASH.resume(crc, payload.array(), payload.arrayOffset() + payload.readerIndex(),
                payload.readableBytes());
    } else {
        crc = CRC32C_HASH.resume(crc, payload.nioBuffer());
    }
    return crc & 0xffffffffL;
}

From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java

License:Apache License

/**
 * Writes a sequence of bytes to this file from the given buffer.
 * <p>// w ww .j  a  v  a 2 s . c  om
 * <p> Bytes are written starting at this file's current position,
 */
public void write(ByteBuf src, int srcStart, int srcLength) throws IOException {
    final int nextPosition = this.position + srcLength;
    checkCapacity(nextPosition);
    final long destAddress = this.address + this.position;
    if (src.hasMemoryAddress()) {
        final long srcAddress = src.memoryAddress() + srcStart;
        PlatformDependent.copyMemory(srcAddress, destAddress, srcLength);
    } else if (src.hasArray()) {
        final byte[] srcArray = src.array();
        PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength);
    } else {
        throw new IllegalArgumentException("unsupported byte buffer");
    }
    rawMovePositionAndLength(nextPosition);
}

From source file:org.apache.activemq.artemis.utils.AbstractByteBufPool.java

License:Apache License

/**
 * Batch hash code implementation that works at its best if {@code bytes}
 * contains a {@link org.apache.activemq.artemis.api.core.SimpleString} encoded.
 *//*  w w  w  .  j  ava2s.c  om*/
private static int hashCode(final ByteBuf bytes, final int offset, final int length) {
    if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) {
        //if the platform allows it, the hash code could be computed without bounds checking
        if (bytes.hasArray()) {
            return onHeapHashCode(bytes.array(), bytes.arrayOffset() + offset, length);
        } else if (bytes.hasMemoryAddress()) {
            return offHeapHashCode(bytes.memoryAddress(), offset, length);
        }
    }
    return byteBufHashCode(bytes, offset, length);
}

From source file:org.apache.activemq.artemis.utils.UTF8Util.java

License:Apache License

public static void saveUTF(final ByteBuf out, final String str) {

    if (str.length() > 0xffff) {
        throw ActiveMQUtilBundle.BUNDLE.stringTooLong(str.length());
    }/*ww w. j  a  v a2  s  .c o  m*/

    final int len = UTF8Util.calculateUTFSize(str);

    if (len > 0xffff) {
        throw ActiveMQUtilBundle.BUNDLE.stringTooLong(len);
    }

    out.writeShort((short) len);

    final int stringLength = str.length();

    if (UTF8Util.isTrace) {
        // This message is too verbose for debug, that's why we are using trace here
        ActiveMQUtilLogger.LOGGER.trace("Saving string with utfSize=" + len + " stringSize=" + stringLength);
    }

    if (out.hasArray()) {
        out.ensureWritable(len);
        final byte[] bytes = out.array();
        final int writerIndex = out.writerIndex();
        final int index = out.arrayOffset() + writerIndex;
        if (PlatformDependent.hasUnsafe()) {
            unsafeOnHeapWriteUTF(str, bytes, index, stringLength);
        } else {
            writeUTF(str, bytes, index, stringLength);
        }
        out.writerIndex(writerIndex + len);
    } else {
        if (PlatformDependent.hasUnsafe() && out.hasMemoryAddress()) {
            out.ensureWritable(len);
            final long addressBytes = out.memoryAddress();
            final int writerIndex = out.writerIndex();
            unsafeOffHeapWriteUTF(str, addressBytes, writerIndex, stringLength);
            out.writerIndex(writerIndex + len);
        } else {
            final StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer();
            final byte[] bytes = buffer.borrowByteBuffer(len);
            writeUTF(str, bytes, 0, stringLength);
            out.writeBytes(bytes, 0, len);
        }
    }
}

From source file:org.apache.bookkeeper.proto.checksum.DirectMemoryCRC32Digest.java

License:Apache License

@Override
public void update(ByteBuf buf) {
    int index = buf.readerIndex();
    int length = buf.readableBytes();

    try {/*w w  w  . j a  v a 2s  .  c  o m*/
        if (buf.hasMemoryAddress()) {
            // Calculate CRC directly from the direct memory pointer
            crcValue = (int) updateByteBuffer.invoke(null, crcValue, buf.memoryAddress(), index, length);
        } else if (buf.hasArray()) {
            // Use the internal method to update from array based
            crcValue = (int) updateBytes.invoke(null, crcValue, buf.array(), buf.arrayOffset() + index, length);
        } else {
            // Fallback to data copy if buffer is not contiguous
            byte[] b = new byte[length];
            buf.getBytes(index, b, 0, length);
            crcValue = (int) updateBytes.invoke(null, crcValue, b, 0, b.length);
        }
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.pulsar.common.compression.CompressionCodecZstd.java

License:Apache License

@Override
public ByteBuf encode(ByteBuf source) {
    int uncompressedLength = source.readableBytes();
    int maxLength = (int) Zstd.compressBound(uncompressedLength);

    ByteBuf target = PooledByteBufAllocator.DEFAULT.directBuffer(maxLength, maxLength);
    int compressedLength;

    if (source.hasMemoryAddress()) {
        compressedLength = (int) Zstd.compressUnsafe(target.memoryAddress(), maxLength,
                source.memoryAddress() + source.readerIndex(), uncompressedLength, ZSTD_COMPRESSION_LEVEL);
    } else {//ww w. j  a  va 2  s . c o  m
        ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes());
        ByteBuffer targetNio = target.nioBuffer(0, maxLength);

        compressedLength = Zstd.compress(targetNio, sourceNio, ZSTD_COMPRESSION_LEVEL);
    }

    target.writerIndex(compressedLength);
    return target;
}

From source file:org.apache.pulsar.common.compression.CompressionCodecZstd.java

License:Apache License

@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.directBuffer(uncompressedLength, uncompressedLength);

    if (encoded.hasMemoryAddress()) {
        Zstd.decompressUnsafe(uncompressed.memoryAddress(), uncompressedLength,
                encoded.memoryAddress() + encoded.readerIndex(), encoded.readableBytes());
    } else {/*from   w  ww.j  av a 2s .  com*/
        ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);
        ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());

        Zstd.decompress(uncompressedNio, encodedNio);
    }

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}