Example usage for io.netty.buffer ByteBuf memoryAddress

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

Introduction

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

Prototype

public abstract long memoryAddress();

Source Link

Document

Returns the low-level memory address that point to the first byte of ths backing data.

Usage

From source file:com.antsdb.saltedfish.server.mysql.util.BindValueUtil.java

License:Open Source License

public static long read(Heap heap, ByteBuf buf, int type) {
    long pValue = 0;
    switch (type & 0xff) {
    case Fields.FIELD_TYPE_TINY:
        pValue = Int4.allocSet(heap, buf.readByte());
        break;/* w  w  w .  j a  v a2s . c o m*/
    case Fields.FIELD_TYPE_SHORT:
        pValue = Int4.allocSet(heap, (short) BufferUtils.readInt(buf));
        break;
    case Fields.FIELD_TYPE_LONG:
        pValue = Int4.allocSet(heap, BufferUtils.readLong(buf));
        break;
    case Fields.FIELD_TYPE_LONGLONG:
        pValue = Int8.allocSet(heap, BufferUtils.readLongLong(buf));
        break;
    case Fields.FIELD_TYPE_FLOAT:
        pValue = Float4.allocSet(heap, BufferUtils.readFloat(buf));
        break;
    case Fields.FIELD_TYPE_DOUBLE:
        pValue = Float8.allocSet(heap, BufferUtils.readDouble(buf));
        break;
    case Fields.FIELD_TYPE_TIME:
    case Fields.FIELD_TYPE_TIME2:
        pValue = FishTime.allocSet(heap, BufferUtils.readTime(buf));
        break;
    case Fields.FIELD_TYPE_DATE:
        pValue = FishDate.allocSet(heap, BufferUtils.readDate(buf));
        break;
    case Fields.FIELD_TYPE_DATETIME:
    case Fields.FIELD_TYPE_TIMESTAMP:
    case Fields.FIELD_TYPE_DATETIME2:
    case Fields.FIELD_TYPE_TIMESTAMP2:
        pValue = FishTimestamp.allocSet(heap, BufferUtils.readTimestamp(buf));
        break;
    case Fields.FIELD_TYPE_VAR_STRING:
    case Fields.FIELD_TYPE_STRING:
    case Fields.FIELD_TYPE_VARCHAR:
        int len = (int) BufferUtils.readLength(buf);
        long pData = buf.memoryAddress() + buf.readerIndex();
        pValue = FishUtf8.allocSet(heap, pData, len);
        buf.readerIndex(buf.readerIndex() + len);
        break;
    case Fields.FIELD_TYPE_DECIMAL:
    case Fields.FIELD_TYPE_NEW_DECIMAL:
        pValue = FishNumber.allocSet(heap, BufferUtils.readBigDecimal(buf));
        break;
    default:
        throw new IllegalArgumentException("bindValue error,unsupported type:" + type);
    }
    return pValue;
}

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.j a va 2  s .  c  o  m
 * @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//from w w  w.jav  a 2s  .  co m
 * @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  ww  w  . j  ava2 s  .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//from  w ww .j  a va2  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:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cDirectMemoryHardware() {
    if (HARDWARE_CRC32C_HASH == null) {
        return;//w ww .  j a v a2  s.co  m
    }

    ByteBuf payload = ByteBufAllocator.DEFAULT.directBuffer(inputBytes.length);
    payload.writeBytes(inputBytes);

    // read directly from memory address
    int checksum = HARDWARE_CRC32C_HASH.calculate(payload.memoryAddress(), payload.readableBytes());

    payload.release();
    assertEquals(checksum, expectedChecksum);
}

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>//from   ww w  . j a  v  a2 s. co m
 * <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  a  v a  2  s. c o  m*/
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());
    }//w  w w  .j  av  a  2 s  .  c om

    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.activemq.artemis.utils.UTF8Util.java

License:Apache License

public static String readUTF(final ActiveMQBuffer input) {
    StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer();

    final int size = input.readUnsignedShort();

    if (UTF8Util.isTrace) {
        // This message is too verbose for debug, that's why we are using trace here
        ActiveMQUtilLogger.LOGGER.trace("Reading string with utfSize=" + size);
    }/*from  w w w .  ja v a2s  . co m*/
    if (PlatformDependent.hasUnsafe() && input.byteBuf() != null && input.byteBuf().hasMemoryAddress()) {
        final ByteBuf byteBuf = input.byteBuf();
        final long addressBytes = byteBuf.memoryAddress();
        final int index = byteBuf.readerIndex();
        byteBuf.skipBytes(size);
        final char[] chars = buffer.borrowCharBuffer(size);
        return unsafeOffHeapReadUTF(addressBytes, index, chars, size);
    }
    final byte[] bytes;
    final int index;
    if (input.byteBuf() != null && input.byteBuf().hasArray()) {
        final ByteBuf byteBuf = input.byteBuf();
        bytes = byteBuf.array();
        index = byteBuf.arrayOffset() + byteBuf.readerIndex();
        byteBuf.skipBytes(size);
    } else {
        bytes = buffer.borrowByteBuffer(size);
        index = 0;
        input.readBytes(bytes, 0, size);
    }
    final char[] chars = buffer.borrowCharBuffer(size);
    if (PlatformDependent.hasUnsafe()) {
        return unsafeOnHeapReadUTF(bytes, index, chars, size);
    } else {
        return readUTF(bytes, index, chars, size);
    }
}