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: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 {//from   w w  w . ja va2s.  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 {/*  w w w  . j  av  a  2  s.  c om*/
        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 a  v a 2  s  .co  m
        ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength);
        ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes());

        Zstd.decompress(uncompressedNio, encodedNio);
    }

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}

From source file:org.apache.tajo.util.NumberUtil.java

License:Apache License

/**
 * Parses the byte array argument as if it was a double value and returns the
 * result. Throws NumberFormatException if the byte buffer does not represent a
 * double value.//from w w w. j  a v a 2 s .  c  o m
 *
 * @return double, the value represented by the argument
 * @throws NumberFormatException if the argument could not be parsed as a double
 */
public static double parseDouble(ByteBuf bytes, int start, int length) {
    if (!PlatformDependent.hasUnsafe()) {
        return parseDouble(bytes.array(), start, length);
    }

    if (bytes == null) {
        throw new NumberFormatException("String is null");
    }

    if (length == 0 || bytes.writerIndex() < start + length) {
        throw new NumberFormatException("Empty string or Invalid buffer!");
    }

    long memoryAddress = bytes.memoryAddress();
    /*
     * Strip off leading blanks
     */
    int offset = start;
    int end = start + length;

    while (offset < end && PlatformDependent.getByte(memoryAddress + offset) == ' ') {
        offset++;
    }
    if (offset == end) {
        throw new NumberFormatException("blank byte array!");
    }

    /*
     * check for a sign.
     */
    boolean sign = false;
    if (PlatformDependent.getByte(memoryAddress + offset) == '-') {
        sign = true;
        offset++;
    } else if (PlatformDependent.getByte(memoryAddress + offset) == '+') {
        offset++;
    }
    if (offset == end) {
        throw new NumberFormatException("the byte array only has a sign!");
    }

    /*
     * Count the number of digits in the mantissa (including the decimal
     * point), and also locate the decimal point.
     */
    int mantSize = 0; /* Number of digits in mantissa. */
    int decicalOffset = -1; /* Number of mantissa digits BEFORE decimal point. */
    for (; offset < end; offset++) {
        if (!isDigit(PlatformDependent.getByte(memoryAddress + offset))) {
            if ((PlatformDependent.getByte(memoryAddress + offset) != '.') || (decicalOffset >= 0)) {
                break;
            }
            decicalOffset = mantSize;
        }
        mantSize++;
    }

    int exponentOffset = offset; /* Temporarily holds location of exponent in bytes. */

    /*
     * Now suck up the digits in the mantissa.  Use two integers to
     * collect 9 digits each (this is faster than using floating-point).
     * If the mantissa has more than 18 digits, ignore the extras, since
     * they can't affect the value anyway.
     */
    offset -= mantSize;
    if (decicalOffset < 0) {
        decicalOffset = mantSize;
    } else {
        mantSize -= 1; /* One of the digits was the decimal point. */
    }
    int fracExponent; /* Exponent that derives from the fractional
                       * part.  Under normal circumstatnces, it is
                           * the negative of the number of digits in F.
                           * However, if I is very long, the last digits
                           * of I get dropped (otherwise a long I with a
                           * large negative exponent could cause an
                           * unnecessary overflow on I alone).  In this
                           * case, fracExp is incremented one for each
                           * dropped digit. */
    if (mantSize > 18) {
        fracExponent = decicalOffset - 18;
        mantSize = 18;
    } else {
        fracExponent = decicalOffset - mantSize;
    }

    if (mantSize == 0) {
        return 0.0;
    }

    int frac1 = 0;
    for (; mantSize > 9; mantSize--) {
        int b = PlatformDependent.getByte(memoryAddress + offset);
        offset++;
        if (b == '.') {
            b = PlatformDependent.getByte(memoryAddress + offset);
            offset++;
        }
        frac1 = 10 * frac1 + (b - '0');
    }
    int frac2 = 0;
    for (; mantSize > 0; mantSize--) {
        int b = PlatformDependent.getByte(memoryAddress + offset);
        offset++;
        if (b == '.') {
            b = PlatformDependent.getByte(memoryAddress + offset);
            offset++;
        }
        frac2 = 10 * frac2 + (b - '0');
    }
    double fraction = (1.0e9 * frac1) + frac2;

    /*
     * Skim off the exponent.
     */
    int exponent = 0; /* Exponent read from "EX" field. */
    offset = exponentOffset;
    boolean expSign = false;

    if (offset < end) {
        if ((PlatformDependent.getByte(memoryAddress + offset) != 'E')
                && (PlatformDependent.getByte(memoryAddress + offset) != 'e')) {
            throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset()));
        }

        // (bytes[offset] == 'E') || (bytes[offset] == 'e')
        offset++;

        if (PlatformDependent.getByte(memoryAddress + offset) == '-') {
            expSign = true;
            offset++;
        } else if (PlatformDependent.getByte(memoryAddress + offset) == '+') {
            offset++;
        }

        for (; offset < end; offset++) {
            if (isDigit(PlatformDependent.getByte(memoryAddress + offset))) {
                exponent = exponent * 10 + (PlatformDependent.getByte(memoryAddress + offset) - '0');
            } else {
                throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset()));
            }
        }
    }

    exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent);

    /*
     * Generate a floating-point number that represents the exponent.
     * Do this by processing the exponent one bit at a time to combine
     * many powers of 2 of 10. Then combine the exponent with the
     * fraction.
     */
    if (exponent < 0) {
        expSign = true;
        exponent = -exponent;
    } else {
        expSign = false;
    }
    if (exponent > maxExponent) {
        throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset()));
    }

    double dblExp = 1.0;
    for (int i = 0; exponent != 0; exponent >>= 1, i++) {
        if ((exponent & 01) == 01) {
            dblExp *= powersOf10[i];
        }
    }

    fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp);

    return sign ? (-fraction) : fraction;
}

From source file:org.apache.tajo.util.NumberUtil.java

License:Apache License

/**
 * Parses the byte buffer argument as if it was an int value and returns the
 * result. Throws NumberFormatException if the byte array does not represent an
 * int quantity. The second argument specifies the radix to use when parsing
 * the value.//from   w  ww  . j a  v a  2s .c om
 *
 * @param radix the base to use for conversion.
 * @return the value represented by the argument
 * @throws NumberFormatException if the argument could not be parsed as an int quantity.
 */
public static int parseInt(ByteBuf bytes, int start, int length, int radix) {
    if (!PlatformDependent.hasUnsafe()) {
        return parseInt(bytes.array(), start, length);
    }

    if (bytes == null) {
        throw new NumberFormatException("String is null");
    }
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
        throw new NumberFormatException("Invalid radix: " + radix);
    }
    if (length == 0 || bytes.writerIndex() < start + length) {
        throw new NumberFormatException("Empty string or Invalid buffer!");
    }

    long memoryAddress = bytes.memoryAddress();

    int offset = start;
    boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-';
    if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') {
        offset++;
        if (length == 1) {
            throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset()));
        }
    }

    return parseIntInternal(bytes, memoryAddress, start, length, offset, radix, negative);
}

From source file:org.apache.tajo.util.NumberUtil.java

License:Apache License

/**
 * Parses the byte buffer argument as if it was an long value and returns the
 * result. Throws NumberFormatException if the string does not represent an
 * long quantity. The second argument specifies the radix to use when parsing
 * the value.//ww w . ja  v a 2  s  .co  m
 *
 * @param bytes  the string byte buffer
 * @param start
 * @param length a UTF-8 encoded string representation of a long quantity.
 * @param radix  the base to use for conversion.
 * @return the value represented by the argument
 * @throws NumberFormatException if the argument could not be parsed as an long quantity.
 */
public static long parseLong(ByteBuf bytes, int start, int length, int radix) {
    if (!PlatformDependent.hasUnsafe()) {
        return parseInt(bytes.array(), start, length);
    }

    if (bytes == null) {
        throw new NumberFormatException("String is null");
    }
    if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
        throw new NumberFormatException("Invalid radix: " + radix);
    }
    if (length == 0 || bytes.writerIndex() < start + length) {
        throw new NumberFormatException("Empty string or Invalid buffer!");
    }

    long memoryAddress = bytes.memoryAddress();

    int offset = start;
    boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-';
    if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') {
        offset++;
        if (length == 1) {
            throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset()));
        }
    }

    return parseLongInternal(bytes, memoryAddress, start, length, offset, radix, negative);
}

From source file:org.lmdbjava.ByteBufProxy.java

License:Apache License

@Override
protected void in(final ByteBuf buffer, final Pointer ptr, final long ptrAddr) {
    UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, buffer.writerIndex() - buffer.readerIndex());
    UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, buffer.memoryAddress() + buffer.readerIndex());
}

From source file:org.lmdbjava.ByteBufProxy.java

License:Apache License

@Override
protected void in(final ByteBuf buffer, final int size, final Pointer ptr, final long ptrAddr) {
    UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, size);
    UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, buffer.memoryAddress() + buffer.readerIndex());
}