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:cn.iie.haiep.hbase.value.Bytes.java

License:Apache License

/**
 * @param buffer buffer to convert//  w w  w.  ja  v a2s  .  c o  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 : i);
}

From source file:cn.iie.haiep.hbase.value.Bytes.java

License:Apache License

/**
 * Reads a zero-compressed encoded long from input stream and returns it.
 * @param buffer Binary array/*www.j  a  v  a 2 s.  co  m*/
 * @param offset Offset into array at which vint begins.
 * @throws java.io.IOException e
 * @return deserialized long from stream.
 */
public static long readVLong(final byte[] buffer, final int offset) throws IOException {
    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 + 1 + idx];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
}

From source file:com.asakusafw.runtime.io.util.WritableRawComparableUnion.java

License:Apache License

@Override
public int getSizeInBytes(byte[] buf, int offset) throws IOException {
    int pos = WritableComparator.readVInt(buf, offset);
    WritableRawComparable object = objects[pos];
    int meta = WritableUtils.decodeVIntSize(buf[offset]);
    return meta + object.getSizeInBytes(buf, offset + meta);
}

From source file:com.asakusafw.runtime.io.util.WritableRawComparableUnion.java

License:Apache License

@Override
public int compareInBytes(byte[] b1, int o1, byte[] b2, int o2) throws IOException {
    int p1 = WritableComparator.readVInt(b1, o1);
    int p2 = WritableComparator.readVInt(b2, o2);
    if (p1 < p2) {
        return -1;
    } else if (p1 > p2) {
        return +1;
    }// w ww  . java  2s . c  o  m
    WritableRawComparable object = objects[p1];
    int meta = WritableUtils.decodeVIntSize(b1[o1]);
    return object.compareInBytes(b1, o1 + meta, b2, o2 + meta);
}

From source file:com.asakusafw.runtime.stage.directio.StringTemplate.java

License:Apache License

/**
 * Computes and returns size in bytes.//from   w  w  w  .j ava2s . co m
 * @param buf bytes array
 * @param offset bytes offset
 * @return size in bytes
 * @throws IOException if failed to compute size
 */
@Override
public final int getSizeInBytes(byte[] buf, int offset) throws IOException {
    int cursor = 0;
    for (int i = 0; i < formatters.length; i++) {
        int metaSize = WritableUtils.decodeVIntSize(buf[offset + cursor]);
        int bodySize = WritableComparator.readVInt(buf, offset + cursor);
        cursor += metaSize + bodySize;
    }
    return cursor;
}

From source file:com.asakusafw.runtime.value.DecimalOption.java

License:Apache License

@SuppressWarnings("deprecation")
@Override//from   ww  w .  jav a 2  s  .  c o  m
public int restore(byte[] bytes, int offset, int limit) throws IOException {
    int cursor = offset;
    int head = bytes[cursor++] & 0xff;
    if ((head & MASK_PRESENT) == 0) {
        setNull();
    } else {
        boolean plus = (head & MASK_PLUS) != 0;
        int scale = WritableComparator.readVInt(bytes, cursor);
        cursor += WritableUtils.decodeVIntSize(bytes[cursor]);
        int length = WritableComparator.readVInt(bytes, cursor);
        cursor += WritableUtils.decodeVIntSize(bytes[cursor]);
        DecimalBuffer buffer = BUFFER_MAIN.get();
        buffer.set(plus, scale, bytes, cursor, length);
        cursor += length;
        modify(buffer.toBigDecimal());
    }
    return cursor - offset;
}

From source file:com.asakusafw.runtime.value.DecimalOption.java

License:Apache License

/**
 * Returns the actual number of bytes from the serialized byte array.
 * @param bytes the target byte array//from   ww  w. jav a  2s .c om
 * @param offset the beginning index in the byte array (inclusive)
 * @param length the limit length of the byte array
 * @return the comparison result
 */
public static int getBytesLength(byte[] bytes, int offset, int length) {
    try {
        int cursor = offset;
        int head = bytes[cursor++] & 0xff;
        if ((head & MASK_PRESENT) != 0) {
            cursor += WritableUtils.decodeVIntSize(bytes[cursor]);
            int bytesLength = WritableComparator.readVInt(bytes, cursor);
            cursor += WritableUtils.decodeVIntSize(bytes[cursor]);
            cursor += bytesLength;
        }
        return cursor - offset;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.asakusafw.runtime.value.DecimalOption.java

License:Apache License

/**
 * Compares between the two objects in serialized form.
 * @param b1 the first byte array to be compared
 * @param s1 the beginning index in {@code b1}
 * @param l1 the limit byte size in {@code b1}
 * @param b2 the second byte array to be compared
 * @param s2 the beginning index in {@code b2}
 * @param l2 the limit byte size in {@code b2}
 * @return the comparison result/*from   w  ww  .  j  a v  a2s  .  co  m*/
 */
public static int compareBytes(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
    try {
        // head
        int cursor1 = s1;
        int cursor2 = s2;
        int h1 = b1[cursor1++] & 0xff;
        int h2 = b2[cursor2++] & 0xff;

        // nullity
        boolean null1 = (h1 & MASK_PRESENT) == 0;
        boolean null2 = (h2 & MASK_PRESENT) == 0;
        if (null1) {
            if (null2) {
                return 0;
            } else {
                return -1;
            }
        } else if (null2) {
            return +1;
        }

        // sign
        boolean plus1 = (h1 & MASK_PLUS) != 0;
        boolean plus2 = (h2 & MASK_PLUS) != 0;
        if (plus1 && plus2 == false) {
            return +1;
        } else if (plus1 == false && plus2) {
            return -1;
        }

        // scale
        int scale1 = WritableComparator.readVInt(b1, cursor1);
        int scale2 = WritableComparator.readVInt(b2, cursor2);
        cursor1 += WritableUtils.decodeVIntSize(b1[cursor1]);
        cursor2 += WritableUtils.decodeVIntSize(b2[cursor2]);

        // bytesCount
        int bytesCount1 = WritableComparator.readVInt(b1, cursor1);
        int bytesCount2 = WritableComparator.readVInt(b2, cursor2);
        cursor1 += WritableUtils.decodeVIntSize(b1[cursor1]);
        cursor2 += WritableUtils.decodeVIntSize(b2[cursor2]);

        DecimalBuffer d1 = BUFFER_MAIN.get();
        d1.set(plus1, scale1, b1, cursor1, bytesCount1);

        DecimalBuffer d2 = BUFFER_SUB.get();
        d2.set(plus2, scale2, b2, cursor2, bytesCount2);

        return DecimalBuffer.compare(d1, d2);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.asakusafw.runtime.value.StringOption.java

License:Apache License

@SuppressWarnings("deprecation")
@Override/*from   w ww.  j a  v  a 2  s .  co  m*/
public int restore(byte[] bytes, int offset, int limit) throws IOException {
    if (limit - offset == 0) {
        throw new IOException(MessageFormat.format("Cannot restore a String field ({0})", "invalid length"));
    }
    if (bytes[offset] == 0) {
        setNull();
        return 1;
    }
    int size = WritableUtils.decodeVIntSize(bytes[offset + 1]);
    if (limit - offset < size + 1) {
        throw new IOException(MessageFormat.format("Cannot restore a String field ({0})", "invalid length"));
    }
    int length = (int) ByteArrayUtil.readVLong(bytes, offset + 1);
    if (limit - offset >= size + 1 + length) {
        nullValue = false;
        entity.set(bytes, offset + size + 1, length);
        return size + 1 + length;
    } else {
        throw new IOException(MessageFormat.format("Cannot restore a String field ({0})", "invalid length"));
    }
}

From source file:com.asakusafw.runtime.value.StringOption.java

License:Apache License

/**
 * Returns the actual number of bytes from the serialized byte array.
 * @param bytes the target byte array//from w ww  . j  ava 2 s  .co m
 * @param offset the beginning index in the byte array (inclusive)
 * @param length the limit length of the byte array
 * @return the comparison result
 */
public static int getBytesLength(byte[] bytes, int offset, int length) {
    if (bytes[offset] == 0) {
        return 1;
    }
    int size = WritableUtils.decodeVIntSize(bytes[offset + 1]);
    int textLength = (int) ByteArrayUtil.readVLong(bytes, offset + 1);
    return 1 + size + textLength;
}