Java Code Examples for org.apache.hadoop.io.WritableUtils#isNegativeVInt()

The following examples show how to use org.apache.hadoop.io.WritableUtils#isNegativeVInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Decode a vint from the buffer pointed at to by ptr and
 * increment the offset of the ptr by the length of the
 * vint.
 * @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);
}
 
Example 2
Source File: Bytes.java    From tajo with Apache License 2.0 6 votes vote down vote up
/**
 * @param buffer buffer to convert
 * @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);
}
 
Example 3
Source File: Bytes.java    From tajo with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a zero-compressed encoded long from input stream and returns it.
 * @param buffer Binary array
 * @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);
}
 
Example 4
Source File: Bytes.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param buffer buffer to convert
 * @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);
}
 
Example 5
Source File: Bytes.java    From incubator-tajo with Apache License 2.0 6 votes vote down vote up
/**
 * @param buffer buffer to convert
 * @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);
}
 
Example 6
Source File: Bytes.java    From incubator-tajo with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a zero-compressed encoded long from input stream and returns it.
 * @param buffer Binary array
 * @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 length = (byte) WritableUtils.decodeVIntSize(firstByte);
  if (length == 1) {
    return firstByte;
  }
  long i = 0;
  for (int idx = 0; idx < length - 1; idx++) {
    byte b = buffer[offset + 1 + idx];
    i = i << 8;
    i = i | (b & 0xFF);
  }
  return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}
 
Example 7
Source File: ByteUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Decode a vint from the buffer pointed at to by ptr and
 * increment the offset of the ptr by the length of the
 * vint.
 * @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);
}
 
Example 8
Source File: ByteUtil.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Decode a vint from the buffer pointed at to by ptr and
 * increment the offset of the ptr by the length of the
 * vint.
 * @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);
}
 
Example 9
Source File: BulkIngestKey.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a Variable Long from a byte[]. Also returns the variable int size in the second position (index 1) of the startAndLen array. This allows the
 * caller to have access to the VInt size without having to call decode again.
 * 
 * @param bytes
 *            payload containing variable long
 * @param startAndLen
 *            index 0 holds the offset into the byte array and position 1 is populated with the length of the bytes
 * @return the value
 */
public static long readVLong(byte[] bytes, int[] startAndLen) {
    byte firstByte = bytes[startAndLen[0]];
    startAndLen[1] = WritableUtils.decodeVIntSize(firstByte);
    if (startAndLen[1] == 1) {
        return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < startAndLen[1] - 1; idx++) {
        byte b = bytes[startAndLen[0] + 1 + idx];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}
 
Example 10
Source File: HFileReaderImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Actually do the mvcc read. Does no checks.
 */
private void _readMvccVersion(int offsetFromPos) {
  // This is Bytes#bytesToVint inlined so can save a few instructions in this hot method; i.e.
  // previous if one-byte vint, we'd redo the vint call to find int size.
  // Also the method is kept small so can be inlined.
  byte firstByte = blockBuffer.getByteAfterPosition(offsetFromPos);
  int len = WritableUtils.decodeVIntSize(firstByte);
  if (len == 1) {
    this.currMemstoreTS = firstByte;
  } else {
    int remaining = len -1;
    long i = 0;
    offsetFromPos++;
    if (remaining >= Bytes.SIZEOF_INT) {
      // The int read has to be converted to unsigned long so the & op
      i = (blockBuffer.getIntAfterPosition(offsetFromPos) & 0x00000000ffffffffL);
      remaining -= Bytes.SIZEOF_INT;
      offsetFromPos += Bytes.SIZEOF_INT;
    }
    if (remaining >= Bytes.SIZEOF_SHORT) {
      short s = blockBuffer.getShortAfterPosition(offsetFromPos);
      i = i << 16;
      i = i | (s & 0xFFFF);
      remaining -= Bytes.SIZEOF_SHORT;
      offsetFromPos += Bytes.SIZEOF_SHORT;
    }
    for (int idx = 0; idx < remaining; idx++) {
      byte b = blockBuffer.getByteAfterPosition(offsetFromPos + idx);
      i = i << 8;
      i = i | (b & 0xFF);
    }
    currMemstoreTS = (WritableUtils.isNegativeVInt(firstByte) ? ~i : i);
  }
  this.currMemstoreTSLen = len;
}
 
Example 11
Source File: ByteBufferUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
private static long readVLong(ByteVisitor visitor) {
  byte firstByte = visitor.get();
  int len = WritableUtils.decodeVIntSize(firstByte);
  if (len == 1) {
    return firstByte;
  }
  long i = 0;
  for (int idx = 0; idx < len - 1; idx++) {
    byte b = visitor.get();
    i = i << 8;
    i = i | (b & 0xFF);
  }
  return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}
 
Example 12
Source File: Bytes.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a zero-compressed encoded long from input buffer and returns it.
 * @param buffer Binary array
 * @param offset Offset into array at which vint begins.
 * @return deserialized long from buffer.
 */
public static long readAsVLong(final byte [] buffer, final int offset) {
  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);
}