Example usage for io.netty.buffer ByteBuf getByte

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

Introduction

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

Prototype

public abstract byte getByte(int index);

Source Link

Document

Gets a byte at the specified absolute index in this buffer.

Usage

From source file:org.apache.arrow.memory.TestEndianess.java

License:Apache License

@Test
public void testLittleEndian() {
    final BufferAllocator a = new RootAllocator(10000);
    final ByteBuf b = a.buffer(4);
    b.setInt(0, 35);// ww  w .j a  v  a 2  s.  c o  m
    assertEquals(b.getByte(0), 35);
    assertEquals(b.getByte(1), 0);
    assertEquals(b.getByte(2), 0);
    assertEquals(b.getByte(3), 0);
    b.release();
    a.close();
}

From source file:org.apache.bookkeeper.common.hash.Murmur3.java

License:Apache License

public static int hash32(ByteBuf data, int offset, int length, int seed) {
    int hash = seed;
    final int nblocks = length >> 2;

    // body//ww  w.  j  av a 2 s  .  c  o m
    for (int i = 0; i < nblocks; i++) {
        int i_4 = i << 2;
        int k = (data.getByte(offset + i_4) & 0xff) | ((data.getByte(offset + i_4 + 1) & 0xff) << 8)
                | ((data.getByte(offset + i_4 + 2) & 0xff) << 16)
                | ((data.getByte(offset + i_4 + 3) & 0xff) << 24);

        // mix functions
        k *= C1_32;
        k = Integer.rotateLeft(k, R1_32);
        k *= C2_32;
        hash ^= k;
        hash = Integer.rotateLeft(hash, R2_32) * M_32 + N_32;
    }

    // tail
    int idx = nblocks << 2;
    int k1 = 0;
    switch (length - idx) {
    case 3:
        k1 ^= data.getByte(offset + idx + 2) << 16;
    case 2:
        k1 ^= data.getByte(offset + idx + 1) << 8;
    case 1:
        k1 ^= data.getByte(offset + idx);

        // mix functions
        k1 *= C1_32;
        k1 = Integer.rotateLeft(k1, R1_32);
        k1 *= C2_32;
        hash ^= k1;
    }

    // finalization
    hash ^= length;
    hash ^= (hash >>> 16);
    hash *= 0x85ebca6b;
    hash ^= (hash >>> 13);
    hash *= 0xc2b2ae35;
    hash ^= (hash >>> 16);

    return hash;
}

From source file:org.apache.bookkeeper.common.hash.Murmur3.java

License:Apache License

/**
 * Murmur3 128-bit variant./*from w w w.j  a v  a2  s .c o  m*/
 *
 * @param data   - input byte array
 * @param offset - the first element of array
 * @param length - length of array
 * @param seed   - seed. (default is 0)
 * @return - hashcode (2 longs)
 */
public static long[] hash128(ByteBuf data, int offset, int length, long seed) {
    long h1 = seed;
    long h2 = seed;
    final int nblocks = length >> 4;

    // body
    for (int i = 0; i < nblocks; i++) {
        final int i16 = i << 4;
        long k1 = ((long) data.getByte(offset + i16) & 0xff)
                | (((long) data.getByte(offset + i16 + 1) & 0xff) << 8)
                | (((long) data.getByte(offset + i16 + 2) & 0xff) << 16)
                | (((long) data.getByte(offset + i16 + 3) & 0xff) << 24)
                | (((long) data.getByte(offset + i16 + 4) & 0xff) << 32)
                | (((long) data.getByte(offset + i16 + 5) & 0xff) << 40)
                | (((long) data.getByte(offset + i16 + 6) & 0xff) << 48)
                | (((long) data.getByte(offset + i16 + 7) & 0xff) << 56);

        long k2 = ((long) data.getByte(offset + i16 + 8) & 0xff)
                | (((long) data.getByte(offset + i16 + 9) & 0xff) << 8)
                | (((long) data.getByte(offset + i16 + 10) & 0xff) << 16)
                | (((long) data.getByte(offset + i16 + 11) & 0xff) << 24)
                | (((long) data.getByte(offset + i16 + 12) & 0xff) << 32)
                | (((long) data.getByte(offset + i16 + 13) & 0xff) << 40)
                | (((long) data.getByte(offset + i16 + 14) & 0xff) << 48)
                | (((long) data.getByte(offset + i16 + 15) & 0xff) << 56);

        // mix functions for k1
        k1 *= C1;
        k1 = Long.rotateLeft(k1, R1);
        k1 *= C2;
        h1 ^= k1;
        h1 = Long.rotateLeft(h1, R2);
        h1 += h2;
        h1 = h1 * M + N1;

        // mix functions for k2
        k2 *= C2;
        k2 = Long.rotateLeft(k2, R3);
        k2 *= C1;
        h2 ^= k2;
        h2 = Long.rotateLeft(h2, R1);
        h2 += h1;
        h2 = h2 * M + N2;
    }

    // tail
    long k1 = 0;
    long k2 = 0;
    int tailStart = nblocks << 4;
    switch (length - tailStart) {
    case 15:
        k2 ^= (long) (data.getByte(offset + tailStart + 14) & 0xff) << 48;
    case 14:
        k2 ^= (long) (data.getByte(offset + tailStart + 13) & 0xff) << 40;
    case 13:
        k2 ^= (long) (data.getByte(offset + tailStart + 12) & 0xff) << 32;
    case 12:
        k2 ^= (long) (data.getByte(offset + tailStart + 11) & 0xff) << 24;
    case 11:
        k2 ^= (long) (data.getByte(offset + tailStart + 10) & 0xff) << 16;
    case 10:
        k2 ^= (long) (data.getByte(offset + tailStart + 9) & 0xff) << 8;
    case 9:
        k2 ^= (long) (data.getByte(offset + tailStart + 8) & 0xff);
        k2 *= C2;
        k2 = Long.rotateLeft(k2, R3);
        k2 *= C1;
        h2 ^= k2;

    case 8:
        k1 ^= (long) (data.getByte(offset + tailStart + 7) & 0xff) << 56;
    case 7:
        k1 ^= (long) (data.getByte(offset + tailStart + 6) & 0xff) << 48;
    case 6:
        k1 ^= (long) (data.getByte(offset + tailStart + 5) & 0xff) << 40;
    case 5:
        k1 ^= (long) (data.getByte(offset + tailStart + 4) & 0xff) << 32;
    case 4:
        k1 ^= (long) (data.getByte(offset + tailStart + 3) & 0xff) << 24;
    case 3:
        k1 ^= (long) (data.getByte(offset + tailStart + 2) & 0xff) << 16;
    case 2:
        k1 ^= (long) (data.getByte(offset + tailStart + 1) & 0xff) << 8;
    case 1:
        k1 ^= (long) (data.getByte(offset + tailStart) & 0xff);
        k1 *= C1;
        k1 = Long.rotateLeft(k1, R1);
        k1 *= C2;
        h1 ^= k1;
    }

    // finalization
    h1 ^= length;
    h2 ^= length;

    h1 += h2;
    h2 += h1;

    h1 = fmix64(h1);
    h2 = fmix64(h2);

    h1 += h2;
    h2 += h1;

    return new long[] { h1, h2 };
}

From source file:org.apache.bookkeeper.common.hash.MurmurHash.java

License:Apache License

/**
 * Create a 32 bits murmur hash value for the provided {@code data}.
 *
 * @param data   the data point to data/*from  w ww .j a  v a  2s.com*/
 * @param offset offset
 * @param length length
 * @param seed   the seed
 * @return the 32 bits murmur hash value.
 */
public static int hash32(ByteBuf data, int offset, int length, int seed) {
    int m = 0x5bd1e995;
    int r = 24;

    int h = seed ^ length;

    int len4 = length >> 2;

    for (int i = 0; i < len4; i++) {
        int i4 = i << 2;
        int k = data.getByte(offset + i4 + 3);
        k = k << 8;
        k = k | (data.getByte(offset + i4 + 2) & 0xff);
        k = k << 8;
        k = k | (data.getByte(offset + i4 + 1) & 0xff);
        k = k << 8;
        k = k | (data.getByte(offset + i4 + 0) & 0xff);
        k *= m;
        k ^= k >>> r;
        k *= m;
        h *= m;
        h ^= k;
    }

    // avoid calculating modulo
    int lenM = len4 << 2;
    int left = length - lenM;

    if (left != 0) {
        if (left >= 3) {
            h ^= (int) data.getByte(offset + length - 3) << 16;
        }
        if (left >= 2) {
            h ^= (int) data.getByte(offset + length - 2) << 8;
        }
        if (left >= 1) {
            h ^= (int) data.getByte(offset + length - 1);
        }

        h *= m;
    }

    h ^= h >>> 13;
    h *= m;
    h ^= h >>> 15;

    return h;
}

From source file:org.apache.bookkeeper.common.hash.MurmurHash.java

License:Apache License

/**
 * Create a 64 bits murmur hash value for the provided {@code data}.
 *
 * @param key    the data point to data/*from   w ww  .  j  a  v  a2  s.  c  o  m*/
 * @param offset offset
 * @param length length
 * @param seed   the seed
 * @return the 32 bits murmur hash value.
 */
public static long hash64(ByteBuf key, int offset, int length, long seed) {
    long m64 = 0xc6a4a7935bd1e995L;
    int r64 = 47;

    long h64 = (seed & 0xffffffffL) ^ (m64 * length);

    int lenLongs = length >> 3;

    for (int i = 0; i < lenLongs; ++i) {
        int i8 = i << 3;

        long k64 = ((long) key.getByte(offset + i8 + 0) & 0xff)
                + (((long) key.getByte(offset + i8 + 1) & 0xff) << 8)
                + (((long) key.getByte(offset + i8 + 2) & 0xff) << 16)
                + (((long) key.getByte(offset + i8 + 3) & 0xff) << 24)
                + (((long) key.getByte(offset + i8 + 4) & 0xff) << 32)
                + (((long) key.getByte(offset + i8 + 5) & 0xff) << 40)
                + (((long) key.getByte(offset + i8 + 6) & 0xff) << 48)
                + (((long) key.getByte(offset + i8 + 7) & 0xff) << 56);

        k64 *= m64;
        k64 ^= k64 >>> r64;
        k64 *= m64;

        h64 ^= k64;
        h64 *= m64;
    }

    int rem = length & 0x7;

    // CHECKSTYLE.OFF: FallThrough
    switch (rem) {
    case 0:
        break;
    case 7:
        h64 ^= (long) key.getByte(offset + length - rem + 6) << 48;
    case 6:
        h64 ^= (long) key.getByte(offset + length - rem + 5) << 40;
    case 5:
        h64 ^= (long) key.getByte(offset + length - rem + 4) << 32;
    case 4:
        h64 ^= (long) key.getByte(offset + length - rem + 3) << 24;
    case 3:
        h64 ^= (long) key.getByte(offset + length - rem + 2) << 16;
    case 2:
        h64 ^= (long) key.getByte(offset + length - rem + 1) << 8;
    case 1:
        h64 ^= (long) key.getByte(offset + length - rem);
        h64 *= m64;
    }
    // CHECKSTYLE.ON: FallThrough

    h64 ^= h64 >>> r64;
    h64 *= m64;
    h64 ^= h64 >>> r64;

    return h64;
}

From source file:org.apache.drill.common.util.DecimalUtility.java

License:Apache License

public static BigDecimal getBigDecimalFromDense(ByteBuf data, int startIndex, int nDecimalDigits, int scale,
        int maxPrecision, int width) {

    /* This method converts the dense representation to
     * an intermediate representation. The intermediate
     * representation has one more integer than the dense
     * representation.//from  w ww .ja v a 2s  .c  om
     */
    byte[] intermediateBytes = new byte[((nDecimalDigits + 1) * integerSize)];

    // Start storing from the least significant byte of the first integer
    int intermediateIndex = 3;

    int[] mask = { 0x03, 0x0F, 0x3F, 0xFF };
    int[] reverseMask = { 0xFC, 0xF0, 0xC0, 0x00 };

    int maskIndex;
    int shiftOrder;
    byte shiftBits;

    // TODO: Some of the logic here is common with casting from Dense to Sparse types, factor out common code
    if (maxPrecision == 38) {
        maskIndex = 0;
        shiftOrder = 6;
        shiftBits = 0x00;
        intermediateBytes[intermediateIndex++] = (byte) (data.getByte(startIndex) & 0x7F);
    } else if (maxPrecision == 28) {
        maskIndex = 1;
        shiftOrder = 4;
        shiftBits = (byte) ((data.getByte(startIndex) & 0x03) << shiftOrder);
        intermediateBytes[intermediateIndex++] = (byte) (((data.getByte(startIndex) & 0x3C) & 0xFF) >>> 2);
    } else {
        throw new UnsupportedOperationException("Dense types with max precision 38 and 28 are only supported");
    }

    int inputIndex = 1;
    boolean sign = false;

    if ((data.getByte(startIndex) & 0x80) != 0) {
        sign = true;
    }

    while (inputIndex < width) {

        intermediateBytes[intermediateIndex] = (byte) ((shiftBits)
                | (((data.getByte(startIndex + inputIndex) & reverseMask[maskIndex]) & 0xFF) >>> (8
                        - shiftOrder)));

        shiftBits = (byte) ((data.getByte(startIndex + inputIndex) & mask[maskIndex]) << shiftOrder);

        inputIndex++;
        intermediateIndex++;

        if (((inputIndex - 1) % integerSize) == 0) {
            shiftBits = (byte) ((shiftBits & 0xFF) >>> 2);
            maskIndex++;
            shiftOrder -= 2;
        }

    }
    /* copy the last byte */
    intermediateBytes[intermediateIndex] = shiftBits;

    if (sign == true) {
        intermediateBytes[0] = (byte) (intermediateBytes[0] | 0x80);
    }

    ByteBuf intermediateData = Unpooled.wrappedBuffer(intermediateBytes);

    return getBigDecimalFromIntermediate(intermediateData, 0, nDecimalDigits + 1, scale);
}

From source file:org.apache.drill.common.util.DecimalUtility.java

License:Apache License

public static int compareDenseBytes(ByteBuf left, int leftStart, boolean leftSign, ByteBuf right,
        int rightStart, boolean rightSign, int width) {

    int invert = 1;

    /* If signs are different then simply look at the
     * sign of the two inputs and determine which is greater
     *//* ww w . ja v a2  s.  c o  m*/
    if (leftSign != rightSign) {

        return ((leftSign == true) ? -1 : 1);
    } else if (leftSign == true) {
        /* Both inputs are negative, at the end we will
         * have to invert the comparison
         */
        invert = -1;
    }

    int cmp = 0;

    for (int i = 0; i < width; i++) {
        byte leftByte = left.getByte(leftStart + i);
        byte rightByte = right.getByte(rightStart + i);
        // Unsigned byte comparison
        if ((leftByte & 0xFF) > (rightByte & 0xFF)) {
            cmp = 1;
            break;
        } else if ((leftByte & 0xFF) < (rightByte & 0xFF)) {
            cmp = -1;
            break;
        }
    }
    cmp *= invert; // invert the comparison if both were negative values

    return cmp;
}

From source file:org.apache.drill.common.util.DrillStringUtils.java

License:Apache License

/**
 * Return a printable representation of a byte buffer, escaping the non-printable
 * bytes as '\\xNN' where NN is the hexadecimal representation of such bytes.
 *
 * This function does not modify  the {@code readerIndex} and {@code writerIndex}
 * of the byte buffer.//from  w w w .ja va  2s. co m
 */
public static String toBinaryString(ByteBuf buf, int strStart, int strEnd) {
    StringBuilder result = new StringBuilder();
    for (int i = strStart; i < strEnd; ++i) {
        appendByte(result, buf.getByte(i));
    }
    return result.toString();
}

From source file:org.apache.drill.common.util.DrillStringUtils.java

License:Apache License

/**
 * In-place parsing of a hex encoded binary string.
 *
 * This function does not modify  the {@code readerIndex} and {@code writerIndex}
 * of the byte buffer.//from w  ww.  ja  va  2 s  . co m
 *
 * @return Index in the byte buffer just after the last written byte.
 */
public static int parseBinaryString(ByteBuf str, int strStart, int strEnd) {
    int length = (strEnd - strStart);
    int dstEnd = strStart;
    for (int i = strStart; i < strStart + length; i++) {
        byte b = str.getByte(i);
        if (b == '\\' && strEnd > i + 3 && (str.getByte(i + 1) == 'x' || str.getByte(i + 1) == 'X')) {
            // ok, take next 2 hex digits.
            byte hd1 = str.getByte(i + 2);
            byte hd2 = str.getByte(i + 3);
            if (isHexDigit(hd1) && isHexDigit(hd2)) { // [a-fA-F0-9]
                // turn hex ASCII digit -> number
                b = (byte) ((toBinaryFromHex(hd1) << 4) + toBinaryFromHex(hd2));
                i += 3; // skip 3
            }
        }
        str.setByte(dstEnd++, b);
    }
    return dstEnd;
}

From source file:org.apache.drill.exec.expr.fn.impl.StringFunctionUtil.java

License:Apache License

public static int stringLeftMatchUTF8(ByteBuf str, int strStart, int strEnd, ByteBuf substr, int subStart,
        int subEnd) {
    for (int i = strStart; i <= strEnd - (subEnd - subStart); i++) {
        int j = subStart;
        for (; j < subEnd; j++) {
            if (str.getByte(i + j - subStart) != substr.getByte(j)) {
                break;
            }//from  www  . ja  v  a2  s . c o  m
        }

        if (j == subEnd && j != subStart) { // found a matched substr (non-empty) in str.
            return i; // found a match.
        }
    }

    return -1;
}