List of usage examples for org.apache.hadoop.io WritableUtils isNegativeVInt
public static boolean isNegativeVInt(byte value)
From source file:org.apache.gora.util.ByteUtils.java
License:Apache License
/** * Reads a zero-compressed encoded long from input stream and returns it. * @param buffer Binary array/*from www. j ava 2s . com*/ * @param offset Offset into array at which vint begins. * @throws java.io.IOException * @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 ^ -1L) : i); }
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 w w w .j a v 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.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++;/*from w ww . jav a 2s. com*/ // 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++;//from ww w. j a v a 2 s.co 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; }//w ww. j ava 2 s . c o 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: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; }/*from www . j av a 2 s.com*/ 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 ava 2 s . c om * @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); }