@Override public byte[] toBytes(int offset, int length) { byte[] output = new byte[length]; ByteBufferUtils.copyFromBufferToArray(output, buf, offset, 0, length); return output; }
@Override byte[] getContents() { // This ideally should not be called byte[] copy = new byte[this.length]; ByteBufferUtils.copyFromBufferToArray(copy, (ByteBuffer) this.container, this.offset, 0, length); return copy; }
@Override public void visit(ByteBuffer bb, int pos, byte[] array, int arrayIdx, int len) { ByteBufferUtils.copyFromBufferToArray(array, bb, pos, arrayIdx, len); } };
@Override public void get(int sourceOffset, byte[] dst, int offset, int length) { ByteBufferUtils.copyFromBufferToArray(dst, buf, sourceOffset, offset, length); }
/** * Similar to {@link Arrays#copyOfRange(byte[], int, int)} * @param original the buffer from which the copy has to happen * @param from the starting index * @param to the ending index * @return a byte[] created out of the copy */ public static byte[] copyOfRange(ByteBuffer original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); byte[] copy = new byte[newLength]; ByteBufferUtils.copyFromBufferToArray(copy, original, from, 0, newLength); return copy; }
@Override public void get(byte[] dst, int offset, int length) { ByteBufferUtils.copyFromBufferToArray(dst, buf, buf.position(), offset, length); buf.position(buf.position() + length); }
@Override public void write(ByteBuffer b, int off, int len) { checkSizeAndGrow(len); ByteBufferUtils.copyFromBufferToArray(this.buf, b, off, this.pos, len); this.pos += len; }
@Override public short findEntry(ByteBuffer data, int offset, int length) { short ret = backingStore.findIdx(data, offset, length); if (ret == NOT_IN_DICTIONARY) { byte[] copy = new byte[length]; ByteBufferUtils.copyFromBufferToArray(copy, data, offset, 0, length); addEntryInternal(copy, 0, length, false); } return ret; } }
/** * Reads a BigDecimal value at the given buffer's offset. * @param buffer * @param offset * @return BigDecimal value at offset */ public static BigDecimal toBigDecimal(ByteBuffer buffer, int offset, int length) { if (buffer == null || length < Bytes.SIZEOF_INT + 1 || (offset + length > buffer.limit())) { return null; } int scale = toInt(buffer, offset); byte[] tcBytes = new byte[length - Bytes.SIZEOF_INT]; copyFromBufferToArray(tcBytes, buffer, offset + Bytes.SIZEOF_INT, 0, length - Bytes.SIZEOF_INT); return new BigDecimal(new BigInteger(tcBytes), scale); }
@Override public void get(int sourceOffset, byte[] dst, int offset, int length) { int itemIndex = getItemIndex(sourceOffset); ByteBuffer item = this.items[itemIndex]; sourceOffset = sourceOffset - this.itemBeginPos[itemIndex]; while (length > 0) { int toRead = Math.min((item.limit() - sourceOffset), length); ByteBufferUtils.copyFromBufferToArray(dst, item, sourceOffset, offset, toRead); length -= toRead; if (length == 0) break; itemIndex++; item = this.items[itemIndex]; offset += toRead; sourceOffset = 0; } }
@Override public Cell getKey() { if (current.keyBuffer.hasArray()) { return new KeyValue.KeyOnlyKeyValue(current.keyBuffer.array(), current.keyBuffer.arrayOffset() + current.keyBuffer.position(), current.keyLength); } else { byte[] key = new byte[current.keyLength]; ByteBufferUtils.copyFromBufferToArray(key, current.keyBuffer, current.keyBuffer.position(), 0, current.keyLength); return new KeyValue.KeyOnlyKeyValue(key, 0, current.keyLength); } }
@Override public ExtendedCell deepClone() { byte[] copy = new byte[this.length]; ByteBufferUtils.copyFromBufferToArray(copy, this.buf, this.offset, 0, this.length); KeyValue kv = new KeyValue(copy, 0, copy.length); kv.setSequenceId(this.getSequenceId()); return kv; }
@Override public ExtendedCell deepClone() { byte[] copy = new byte[this.length]; ByteBufferUtils.copyFromBufferToArray(copy, this.buf, this.offset, 0, this.length); KeyValue kv = new NoTagsKeyValue(copy, 0, copy.length); kv.setSequenceId(this.getSequenceId()); return kv; } }
/** * Copies the qualifier to the given byte[] * @param cell the cell whose qualifier has to be copied * @param destination the destination byte[] to which the qualifier has to be copied * @param destinationOffset the offset in the destination byte[] * @return the offset of the byte[] after the copy has happened */ public static int copyQualifierTo(Cell cell, byte[] destination, int destinationOffset) { int qlen = cell.getQualifierLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToArray(destination, ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), ((ByteBufferExtendedCell) cell).getQualifierPosition(), destinationOffset, qlen); } else { System.arraycopy(cell.getQualifierArray(), cell.getQualifierOffset(), destination, destinationOffset, qlen); } return destinationOffset + qlen; }
/** * Copies the value to the given byte[] * @param cell the cell whose value has to be copied * @param destination the destination byte[] to which the value has to be copied * @param destinationOffset the offset in the destination byte[] * @return the offset of the byte[] after the copy has happened */ public static int copyValueTo(Cell cell, byte[] destination, int destinationOffset) { int vlen = cell.getValueLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToArray(destination, ((ByteBufferExtendedCell) cell).getValueByteBuffer(), ((ByteBufferExtendedCell) cell).getValuePosition(), destinationOffset, vlen); } else { System.arraycopy(cell.getValueArray(), cell.getValueOffset(), destination, destinationOffset, vlen); } return destinationOffset + vlen; }
private static String getTagValuePartAsString(Tag t, int offset, int length) { if (t.hasArray()) { return Bytes.toString(t.getValueArray(), offset, length); } byte[] b = new byte[length]; ByteBufferUtils.copyFromBufferToArray(b, t.getValueByteBuffer(), offset, 0, length); return Bytes.toString(b); } }
@Test public void testCopyFromBufferToArray() { ByteBuffer buffer = ByteBuffer.allocate(15); buffer.put((byte) -1); long l = 988L; int i = 135; short s = 7; buffer.putShort(s); buffer.putInt(i); buffer.putLong(l); byte[] b = new byte[15]; ByteBufferUtils.copyFromBufferToArray(b, buffer, 1, 1, 14); assertEquals(s, Bytes.toShort(b, 1)); assertEquals(i, Bytes.toInt(b, 3)); assertEquals(l, Bytes.toLong(b, 7)); }
public static byte[] concatTags(byte[] tags, Cell cell) { int cellTagsLen = cell.getTagsLength(); if (cellTagsLen == 0) { // If no Tags, return early. return tags; } byte[] b = new byte[tags.length + cellTagsLen]; int pos = Bytes.putBytes(b, 0, tags, 0, tags.length); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToArray(b, ((ByteBufferExtendedCell) cell).getTagsByteBuffer(), ((ByteBufferExtendedCell) cell).getTagsPosition(), pos, cellTagsLen); } else { Bytes.putBytes(b, pos, cell.getTagsArray(), cell.getTagsOffset(), cellTagsLen); } return b; }
/** * Returns tag value in a new byte array. Primarily for use client-side. If server-side, use * {@link Tag#getValueArray()} with appropriate {@link Tag#getValueOffset()} and * {@link Tag#getValueLength()} instead to save on allocations. * @param tag The Tag whose value to be returned * @return tag value in a new byte array. */ public static byte[] cloneValue(Tag tag) { int tagLength = tag.getValueLength(); byte[] tagArr = new byte[tagLength]; if (tag.hasArray()) { Bytes.putBytes(tagArr, 0, tag.getValueArray(), tag.getValueOffset(), tagLength); } else { ByteBufferUtils.copyFromBufferToArray(tagArr, tag.getValueByteBuffer(), tag.getValueOffset(), 0, tagLength); } return tagArr; }
/** * Copies the tag's value bytes to the given byte array * @param tag The Tag * @param out The byte array where to copy the Tag value. * @param offset The offset within 'out' array where to copy the Tag value. */ public static void copyValueTo(Tag tag, byte[] out, int offset) { if (tag.hasArray()) { Bytes.putBytes(out, offset, tag.getValueArray(), tag.getValueOffset(), tag.getValueLength()); } else { ByteBufferUtils.copyFromBufferToArray(out, tag.getValueByteBuffer(), tag.getValueOffset(), offset, tag.getValueLength()); } }