Example usage for org.apache.hadoop.io WritableUtils decodeVIntSize

List of usage examples for org.apache.hadoop.io WritableUtils decodeVIntSize

Introduction

In this page you can find the example usage for org.apache.hadoop.io WritableUtils decodeVIntSize.

Prototype

public static int decodeVIntSize(byte value) 

Source Link

Document

Parse the first byte of a vint/vlong to determine the number of bytes

Usage

From source file:org.apache.phoenix.util.ByteUtil.java

License:Apache License

/**
 * Decode a vint from the buffer pointed at to by ptr and
 * increment the offset of the ptr by the length of the
 * vint./*from   ww w.  j  av  a2s  .  c  o m*/
 * @param ptr a pointer to a byte array buffer
 * @return the decoded vint value as a long
 */
public static long vlongFromBytes(ImmutableBytesWritable ptr) {
    final byte[] buffer = ptr.get();
    final int offset = ptr.getOffset();
    byte firstByte = buffer[offset];
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        ptr.set(buffer, offset + 1, ptr.getLength());
        return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = buffer[offset + 1 + idx];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    ptr.set(buffer, offset + len, ptr.getLength());
    return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
}

From source file:org.apache.tajo.storage.sequencefile.SequenceFileScanner.java

License:Apache License

/**
 * Check a particular field and set its size and offset in bytes based on the
 * field type and the bytes arrays./*from   w  ww.  ja  v a  2 s.  c om*/
 *
 * For void, boolean, byte, short, int, long, float and double, there is no
 * offset and the size is fixed. For string, the first four bytes are used to store the size.
 * So the offset is 4 and the size is computed by concating the first four bytes together.
 * The first four bytes are defined with respect to the offset in the bytes arrays.
 *
 * @param col
 *          catalog column information
 * @param bytes
 *          bytes arrays store the table row
 * @param offset
 *          offset of this field
 */
private void parse(Column col, byte[] bytes, int offset) throws IOException {
    switch (col.getDataType().getType()) {
    case BOOLEAN:
    case BIT:
        elementOffset = 0;
        elementSize = 1;
        break;
    case INT2:
        elementOffset = 0;
        elementSize = 2;
        break;
    case INT4:
    case INT8:
        elementOffset = 0;
        elementSize = WritableUtils.decodeVIntSize(bytes[offset]);
        break;
    case FLOAT4:
        elementOffset = 0;
        elementSize = 4;
        break;
    case FLOAT8:
        elementOffset = 0;
        elementSize = 8;
        break;
    case BLOB:
    case PROTOBUF:
    case INET4:
    case CHAR:
    case TEXT:
        elementOffset = 1;
        elementSize = bytes[offset];
        break;
    default:
        elementOffset = 0;
        elementSize = 0;
    }
}

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

License:Apache License

/**
 * @param n Long to make a VLong of.//from  www . ja va 2 s .  c  o m
 * @return VLong as bytes array.
 */
public static byte[] vlongToBytes(long n) {
    byte[] result;
    int offset = 0;
    if (n >= -112 && n <= 127) {
        result = new byte[1];
        result[offset] = (byte) n;
        return result;
    }

    int len = -112;
    if (n < 0) {
        n ^= -1L; // take one's complement'
        len = -120;
    }

    long tmp = n;
    while (tmp != 0) {
        tmp = tmp >> 8;
        len--;
    }

    int size = WritableUtils.decodeVIntSize((byte) len);

    result = new byte[size];
    result[offset++] = (byte) len;
    len = (len < -120) ? -(len + 120) : -(len + 112);

    for (int idx = len; idx != 0; idx--) {
        int shiftbits = (idx - 1) * 8;
        long mask = 0xFFL << shiftbits;
        result[offset++] = (byte) ((n & mask) >> shiftbits);
    }
    return result;
}

From source file:org.commoncrawl.rpc.base.shared.BinaryProtocol.java

License:Open Source License

public int readFieldId(DataInput in) throws IOException {

    int fieldIdOut = -1;

    // read first byte no matter what
    byte firstByte = in.readByte();
    // if mode is not VINT MODE
    if (_currentMode != FIELD_ID_ENCODING_MODE_VINT) {
        // ok if first byte is zero, then this is the old short encoding style ..
        if (_currentMode == FIELD_ID_ENCODING_MODE_SHORT || (firstByte == 0 || firstByte == -1)) {
            if (_currentMode == FIELD_ID_ENCODING_MODE_UNKNOWN) {
                // System.out.println("Protocol: Shifted to SHORT MODE");
                // increment nesting level ...
                _nestingLevel++;// w  ww  . ja va2s  . c  o  m
                // set mode
                pushFieldIdEncodingMode(FIELD_ID_ENCODING_MODE_SHORT);
            }
            // return second byte
            fieldIdOut = (((firstByte << 8)) | (in.readByte() & 0xff));
        } else if (_currentMode == FIELD_ID_ENCODING_MODE_UNKNOWN) {

            if (_nestingLevel++ == 0) {
                // System.out.println("Protocol: Skipping Version Byte");
                // skip version byte
                firstByte = in.readByte();
            }
            // System.out.println("Protocol: Shifted to VINT MODE");
            // shift to vint encoding mode ...
            pushFieldIdEncodingMode(FIELD_ID_ENCODING_MODE_VINT);
        }
    }

    if (_currentMode == FIELD_ID_ENCODING_MODE_VINT) {
        // ok a little messier ...
        int len = WritableUtils.decodeVIntSize(firstByte);
        if (len == 1) {
            fieldIdOut = firstByte;
        } else {
            long i = 0;
            for (int idx = 0; idx < len - 1; idx++) {
                byte b = in.readByte();
                i = i << 8;
                i = i | (b & 0xFF);
            }
            fieldIdOut = (int) (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
        }
    }

    if (fieldIdOut == -1 && !_skipping) {
        // ok pop encoding mode off stack
        popFieldIdEncodingMode();
        // reduce nesting level ...
        _nestingLevel--;

        // System.out.println("Protocol: POPED EncodingMode. NestLevel:" +
        // _nestingLevel);
    }
    return fieldIdOut;
}

From source file:org.commoncrawl.rpc.BinaryProtocol.java

License:Apache License

public int readFieldId(DataInput in) throws IOException {

    int fieldIdOut = -1;

    // read first byte no matter what
    byte firstByte = in.readByte();
    // if mode is not VINT MODE
    if (_currentMode != FIELD_ID_ENCODING_MODE_VINT) {
        // ok if first byte is zero, then this is the old short encoding style ..
        if (_currentMode == FIELD_ID_ENCODING_MODE_SHORT || (firstByte == 0 || firstByte == -1)) {
            if (_currentMode == FIELD_ID_ENCODING_MODE_UNKNOWN) {
                // increment nesting level ...
                _nestingLevel++;/*w  ww . j a va 2  s.c  o m*/
                // set mode
                pushFieldIdEncodingMode(FIELD_ID_ENCODING_MODE_SHORT);
            }
            // return second byte
            fieldIdOut = (int) ((((int) firstByte << 8)) | (in.readByte() & 0xff));
        } else if (_currentMode == FIELD_ID_ENCODING_MODE_UNKNOWN) {

            if (_nestingLevel++ == 0) {
                // skip version byte
                firstByte = in.readByte();
            }
            // shift to vint encoding mode ...
            pushFieldIdEncodingMode(FIELD_ID_ENCODING_MODE_VINT);
        }
    }

    if (_currentMode == FIELD_ID_ENCODING_MODE_VINT) {
        // ok a little messier ...
        int len = WritableUtils.decodeVIntSize(firstByte);
        if (len == 1) {
            fieldIdOut = firstByte;
        } else {
            long i = 0;
            for (int idx = 0; idx < len - 1; idx++) {
                byte b = in.readByte();
                i = i << 8;
                i = i | (b & 0xFF);
            }
            fieldIdOut = (int) (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
        }
    }

    if (fieldIdOut == -1) {
        // ok pop encoding mode off stack
        popFieldIdEncodingMode();
        // reduce nesting level ...
        _nestingLevel--;
    }
    return fieldIdOut;
}

From source file:org.commoncrawl.service.listcrawler.CacheManager.java

License:Open Source License

public static long readVLongFromByteBuffer(ByteBuffer source) {
    byte firstByte = source.get();
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        return firstByte;
    }/*from w w w  .  j  av a 2s  . c om*/
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = source.get();
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}

From source file:org.commoncrawl.service.pagerank.slave.PageRankUtils.java

License:Open Source License

private static long readVLongFromByteBuffer(ByteBuffer source) {
    byte firstByte = source.get();
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        return firstByte;
    }/* w  w  w  .ja v  a  2 s .co  m*/
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = source.get();
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}

From source file:uk.ac.cam.eng.rulebuilding.hadoop.Bytes.java

License:Apache License

/**
 * @param buffer/* ww w.  j av  a  2 s .co  m*/
 * @return vint bytes as an integer.
 */
public static long bytesToVint(final byte[] buffer) {
    int offset = 0;
    byte firstByte = buffer[offset++];
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = buffer[offset++];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}