public ByteBufferTag(ByteBuffer buffer, int offset, int length) { this.buffer = buffer; this.offset = offset; this.length = length; this.type = ByteBufferUtils.toByte(buffer, offset + TAG_LENGTH_SIZE); }
@Override public byte getTypeByte() { return ByteBufferUtils.toByte(this.buf, this.offset + this.length - 1); }
private byte getFamilyLength(int famLenPos) { return ByteBufferUtils.toByte(this.buf, famLenPos); }
byte getTypeByte(int keyLen) { return ByteBufferUtils.toByte(this.buf, this.offset + keyLen - 1 + KeyValue.ROW_OFFSET); }
byte getFamilyLength(int famLenPos) { return ByteBufferUtils.toByte(this.buf, famLenPos); }
/** * @param buf * ByteBuffer to hash * @param offset * offset to start from * @param length * length to hash */ public static int hashCode(ByteBuffer buf, int offset, int length) { int hash = 1; for (int i = offset; i < offset + length; i++) { hash = (31 * hash) + (int) toByte(buf, i); } return hash; }
public static boolean equals(ByteBuffer buf1, int o1, int l1, ByteBuffer buf2, int o2, int l2) { if ((l1 == 0) || (l2 == 0)) { // both 0 length, return true, or else false return l1 == l2; } // Since we're often comparing adjacent sorted data, // it's usual to have equal arrays except for the very last byte // so check that first if (toByte(buf1, o1 + l1 - 1) != toByte(buf2, o2 + l2 - 1)) return false; return compareTo(buf1, o1, l1, buf2, o2, l2) == 0; }
/** * Find length of common prefix in two arrays. * @param left ByteBuffer to be compared. * @param leftOffset Offset in left ByteBuffer. * @param leftLength Length of left ByteBuffer. * @param right ByteBuffer to be compared. * @param rightOffset Offset in right ByteBuffer. * @param rightLength Length of right ByteBuffer. */ public static int findCommonPrefix(ByteBuffer left, int leftOffset, int leftLength, ByteBuffer right, int rightOffset, int rightLength) { int length = Math.min(leftLength, rightLength); int result = 0; while (result < length && ByteBufferUtils.toByte(left, leftOffset + result) == ByteBufferUtils .toByte(right, rightOffset + result)) { result++; } return result; }
public static boolean equals(ByteBuffer buf1, int o1, int l1, byte[] buf2, int o2, int l2) { if ((l1 == 0) || (l2 == 0)) { // both 0 length, return true, or else false return l1 == l2; } // Since we're often comparing adjacent sorted data, // it's usual to have equal arrays except for the very last byte // so check that first if (toByte(buf1, o1 + l1 - 1) != buf2[o2 + l2 - 1]) return false; return compareTo(buf1, o1, l1, buf2, o2, l2) == 0; }
/** * Fetches the byte at the given index. Does not change position of the underlying ByteBuffers * @param index * @return the byte at the given index */ @Override public byte get(int index) { int itemIndex = getItemIndex(index); return ByteBufferUtils.toByte(this.items[itemIndex], index - this.itemBeginPos[itemIndex]); }
/** * Converts a ByteBuffer to an int value * * @param buf The ByteBuffer * @param offset Offset to int value * @param length Number of bytes used to store the int value. * @return the int value * @throws IllegalArgumentException * if there's not enough bytes left in the buffer after the given offset */ public static int readAsInt(ByteBuffer buf, int offset, final int length) { if (offset + length > buf.limit()) { throw new IllegalArgumentException("offset (" + offset + ") + length (" + length + ") exceed the" + " limit of the buffer: " + buf.limit()); } int n = 0; for(int i = offset; i < (offset + length); i++) { n <<= 8; n ^= toByte(buf, i) & 0xFF; } return n; }
private static byte[] getMinimumMidpointArray(ByteBuffer left, int leftOffset, int leftLength, ByteBuffer right, int rightOffset, int rightLength) { // rows are different int minLength = leftLength < rightLength ? leftLength : rightLength; int diffIdx = 0; while (diffIdx < minLength && ByteBufferUtils.toByte(left, leftOffset + diffIdx) == ByteBufferUtils.toByte(right, rightOffset + diffIdx)) { diffIdx++; } byte[] minMidpoint = null; if (diffIdx >= minLength) { // leftKey's row is prefix of rightKey's. minMidpoint = new byte[diffIdx + 1]; ByteBufferUtils.copyFromBufferToArray(minMidpoint, right, rightOffset, 0, diffIdx + 1); } else { int diffByte = ByteBufferUtils.toByte(left, leftOffset + diffIdx); if ((0xff & diffByte) < 0xff && (diffByte + 1) < (ByteBufferUtils.toByte(right, rightOffset + diffIdx) & 0xff)) { minMidpoint = new byte[diffIdx + 1]; ByteBufferUtils.copyFromBufferToArray(minMidpoint, left, leftOffset, 0, diffIdx); minMidpoint[diffIdx] = (byte) (diffByte + 1); } else { minMidpoint = new byte[diffIdx + 1]; ByteBufferUtils.copyFromBufferToArray(minMidpoint, right, rightOffset, 0, diffIdx + 1); } } return minMidpoint; }
/** * Copy data from a buffer to an output stream. Does not update the position * in the buffer. * @param out the stream to write bytes to * @param in the buffer to read bytes from * @param offset the offset in the buffer (from the buffer's array offset) * to start copying bytes from * @param length the number of bytes to copy */ public static void copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length) throws IOException { if (out instanceof ByteBufferWriter) { ((ByteBufferWriter) out).write(in, offset, length); } else if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + offset, length); } else { for (int i = 0; i < length; ++i) { out.write(toByte(in, offset + i)); } } }
/** * Copy data from a buffer to an output stream. Does not update the position * in the buffer. * @param out the output stream to write bytes to * @param in the buffer to read bytes from * @param offset the offset in the buffer (from the buffer's array offset) * to start copying bytes from * @param length the number of bytes to copy */ public static void copyBufferToStream(DataOutput out, ByteBuffer in, int offset, int length) throws IOException { if (out instanceof ByteBufferWriter) { ((ByteBufferWriter) out).write(in, offset, length); } else if (in.hasArray()) { out.write(in.array(), in.arrayOffset() + offset, length); } else { for (int i = 0; i < length; ++i) { out.write(toByte(in, offset + i)); } } }
private short getShort(int index, int itemIndex) { ByteBuffer item = items[itemIndex]; int offsetInItem = index - this.itemBeginPos[itemIndex]; int remainingLen = item.limit() - offsetInItem; if (remainingLen >= Bytes.SIZEOF_SHORT) { return ByteBufferUtils.toShort(item, offsetInItem); } if (items.length - 1 == itemIndex) { // means cur item is the last one and we wont be able to read a short. Throw exception throw new BufferUnderflowException(); } ByteBuffer nextItem = items[itemIndex + 1]; // Get available bytes from this item and remaining from next short l = 0; for (int i = offsetInItem; i < item.capacity(); i++) { l = (short) (l << 8); l = (short) (l ^ (ByteBufferUtils.toByte(item, i) & 0xFF)); } for (int i = 0; i < Bytes.SIZEOF_SHORT - remainingLen; i++) { l = (short) (l << 8); l = (short) (l ^ (ByteBufferUtils.toByte(nextItem, i) & 0xFF)); } return l; }
@Override public byte getByteAfterPosition(int offset) { // Mostly the index specified will land within this current item. Short circuit for that int index = offset + this.position(); int itemIndex = getItemIndexFromCurItemIndex(index); return ByteBufferUtils.toByte(this.items[itemIndex], index - this.itemBeginPos[itemIndex]); }
/** * Converts the value bytes of the given tag into a byte value * @param tag The Tag * @return value as byte */ public static byte getValueAsByte(Tag tag) { if (tag.hasArray()) { return tag.getValueArray()[tag.getValueOffset()]; } return ByteBufferUtils.toByte(tag.getValueByteBuffer(), tag.getValueOffset()); } }
ByteBuffer tagsBuffer = ((ByteBufferExtendedCell) cell).getTagsByteBuffer(); tagLen = ByteBufferUtils.readAsInt(tagsBuffer, pos, TAG_LENGTH_SIZE); if (ByteBufferUtils.toByte(tagsBuffer, pos + TAG_LENGTH_SIZE) == type) { return Optional.of(new ByteBufferTag(tagsBuffer, pos, tagLen + TAG_LENGTH_SIZE));
/** * Fetches the byte at the given index. Does not change position of the underlying ByteBuffers * @param index * @return the byte at the given index */ @Override public byte get(int index) { int itemIndex = getItemIndex(index); return ByteBufferUtils.toByte(this.items[itemIndex], index - this.itemBeginPos[itemIndex]); }