@Override public SingleByteBuff put(byte[] src, int offset, int length) { ByteBufferUtils.copyFromArrayToBuffer(this.buf, src, offset, length); return this; }
@Override public void visit(ByteBuffer bb, int pos, byte[] array, int arrayIdx, int len) { ByteBufferUtils.copyFromArrayToBuffer(bb, pos, array, arrayIdx, len); } };
@Override public void write(ByteBuffer buf, int offset) { ByteBufferUtils.copyFromArrayToBuffer(buf, offset, this.bytes, this.offset, this.length); }
@Override public void setTimestamp(byte[] ts) throws IOException { ByteBufferUtils.copyFromArrayToBuffer(this.buf, this.getTimestampOffset(), ts, 0, Bytes.SIZEOF_LONG); }
@Override public void write(byte[] b, int off, int len) throws IOException { checkSizeAndGrow(len); ByteBufferUtils.copyFromArrayToBuffer(curBuf, b, off, len); }
@Override public void setTimestamp(long ts) throws IOException { ByteBufferUtils.copyFromArrayToBuffer(this.buf, this.getTimestampOffset(), Bytes.toBytes(ts), 0, Bytes.SIZEOF_LONG); }
@Override public void write(byte[] b, int off, int len) throws IOException { int toWrite = 0; while (len > 0) { toWrite = Math.min(len, this.curBuf.remaining()); ByteBufferUtils.copyFromArrayToBuffer(this.curBuf, b, off, toWrite); off += toWrite; len -= toWrite; if (len > 0) { allocateNewBuffer();// The curBuf is over. Let us move to the next one } } }
/** * Copies from the given byte[] to this MBB * @param src * @param offset the position in the byte array from which the copy should be done * @param length the length upto which the copy should happen * @return this MBB */ @Override public MultiByteBuff put(byte[] src, int offset, int length) { if (this.curItem.remaining() >= length) { ByteBufferUtils.copyFromArrayToBuffer(this.curItem, src, offset, length); return this; } int end = offset + length; for (int i = offset; i < end; i++) { this.put(src[i]); } return this; }
@Override public void write(ByteBuffer buf, int offset) { offset = KeyValueUtil.appendTo(this.cell, buf, offset, false); int tagsLen = this.tags == null ? 0 : this.tags.length; if (tagsLen > 0) { offset = ByteBufferUtils.putAsShort(buf, offset, tagsLen); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, this.tags, 0, tagsLen); } }
@Override public void write(ByteBuffer buf, int offset) { offset = KeyValueUtil.appendTo(this.cell, buf, offset, false); int tagsLen = this.tags == null ? 0 : this.tags.length; if (tagsLen > 0) { offset = ByteBufferUtils.putAsShort(buf, offset, tagsLen); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, this.tags, 0, tagsLen); } }
/** * Writes a cell to the buffer at the given offset * @param cell the cell to be written * @param buf the buffer to which the cell has to be wrriten * @param offset the offset at which the cell should be written */ public static void writeCellToBuffer(Cell cell, ByteBuffer buf, int offset) { if (cell instanceof ExtendedCell) { ((ExtendedCell) cell).write(buf, offset); } else { // Using the KVUtil byte[] bytes = KeyValueUtil.copyToNewByteArray(cell); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, bytes, 0, bytes.length); } }
/** * Made into a static method so as to reuse the logic * within ValueAndTagRewriteByteBufferExtendedCell */ static void write(ByteBuffer buf, int offset, Cell cell, byte[] value, byte[] tags) { offset = ByteBufferUtils.putInt(buf, offset, KeyValueUtil.keyLength(cell));// Key length offset = ByteBufferUtils.putInt(buf, offset, value.length);// Value length offset = KeyValueUtil.appendKeyTo(cell, buf, offset); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, value, 0, value.length); offset += value.length; int tagsLen = tags == null ? 0 : tags.length; if (tagsLen > 0) { offset = ByteBufferUtils.putAsShort(buf, offset, tagsLen); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, tags, 0, tagsLen); } }
private static Cell getOffheapCell(byte [] row, byte [] family, byte [] qualifier) { KeyValue kvCell = new KeyValue(row, family, qualifier, 0L, Type.Put, row); ByteBuffer buf = ByteBuffer.allocateDirect(kvCell.getBuffer().length); ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), 0, kvCell.getBuffer().length); return new ByteBufferKeyValue(buf, 0, buf.capacity(), 0L); }
/** * Copies the tags info into the tag portion of the cell * @param cell * @param destination * @param destinationOffset * @return the position after tags */ public static int copyTagsTo(Cell cell, ByteBuffer destination, int destinationOffset) { int tlen = cell.getTagsLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToBuffer(((ByteBufferExtendedCell) cell).getTagsByteBuffer(), destination, ((ByteBufferExtendedCell) cell).getTagsPosition(), destinationOffset, tlen); } else { ByteBufferUtils.copyFromArrayToBuffer(destination, destinationOffset, cell.getTagsArray(), cell.getTagsOffset(), tlen); } return destinationOffset + tlen; }
/** * Copies the qualifier to the given bytebuffer * @param cell the cell whose qualifier has to be copied * @param destination the destination bytebuffer to which the qualifier has to be copied * @param destinationOffset the offset in the destination bytebuffer * @return the offset of the bytebuffer after the copy has happened */ public static int copyQualifierTo(Cell cell, ByteBuffer destination, int destinationOffset) { int qlen = cell.getQualifierLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToBuffer( ((ByteBufferExtendedCell) cell).getQualifierByteBuffer(), destination, ((ByteBufferExtendedCell) cell).getQualifierPosition(), destinationOffset, qlen); } else { ByteBufferUtils.copyFromArrayToBuffer(destination, destinationOffset, cell.getQualifierArray(), cell.getQualifierOffset(), qlen); } return destinationOffset + qlen; }
/** * Copies the family to the given bytebuffer * @param cell the cell whose family has to be copied * @param destination the destination bytebuffer to which the family has to be copied * @param destinationOffset the offset in the destination bytebuffer * @return the offset of the bytebuffer after the copy has happened */ public static int copyFamilyTo(Cell cell, ByteBuffer destination, int destinationOffset) { byte fLen = cell.getFamilyLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToBuffer(((ByteBufferExtendedCell) cell).getFamilyByteBuffer(), destination, ((ByteBufferExtendedCell) cell).getFamilyPosition(), destinationOffset, fLen); } else { ByteBufferUtils.copyFromArrayToBuffer(destination, destinationOffset, cell.getFamilyArray(), cell.getFamilyOffset(), fLen); } return destinationOffset + fLen; }
/** * Copies the row to the given bytebuffer * @param cell cell the cell whose row has to be copied * @param destination the destination bytebuffer to which the row has to be copied * @param destinationOffset the offset in the destination byte[] * @return the offset of the bytebuffer after the copy has happened */ public static int copyRowTo(Cell cell, ByteBuffer destination, int destinationOffset) { short rowLen = cell.getRowLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToBuffer(((ByteBufferExtendedCell) cell).getRowByteBuffer(), destination, ((ByteBufferExtendedCell) cell).getRowPosition(), destinationOffset, rowLen); } else { ByteBufferUtils.copyFromArrayToBuffer(destination, destinationOffset, cell.getRowArray(), cell.getRowOffset(), rowLen); } return destinationOffset + rowLen; }
/** * Copies the value to the given bytebuffer * @param cell the cell whose value has to be copied * @param destination the destination bytebuffer to which the value has to be copied * @param destinationOffset the offset in the destination bytebuffer * @return the offset of the bytebuffer after the copy has happened */ public static int copyValueTo(Cell cell, ByteBuffer destination, int destinationOffset) { int vlen = cell.getValueLength(); if (cell instanceof ByteBufferExtendedCell) { ByteBufferUtils.copyFromBufferToBuffer(((ByteBufferExtendedCell) cell).getValueByteBuffer(), destination, ((ByteBufferExtendedCell) cell).getValuePosition(), destinationOffset, vlen); } else { ByteBufferUtils.copyFromArrayToBuffer(destination, destinationOffset, cell.getValueArray(), cell.getValueOffset(), vlen); } return destinationOffset + vlen; }
@Test public void testCopyFromArrayToBuffer() { byte[] b = new byte[15]; b[0] = -1; long l = 988L; int i = 135; short s = 7; Bytes.putLong(b, 1, l); Bytes.putShort(b, 9, s); Bytes.putInt(b, 11, i); ByteBuffer buffer = ByteBuffer.allocate(14); ByteBufferUtils.copyFromArrayToBuffer(buffer, b, 1, 14); buffer.rewind(); assertEquals(l, buffer.getLong()); assertEquals(s, buffer.getShort()); assertEquals(i, buffer.getInt()); }
private Cell createOffheapKVWithTags(int noOfTags) { List<Tag> tags = new ArrayList<>(); for (int i = 0; i < noOfTags; i++) { tags.add(new ArrayBackedTag((byte) i, "tagValue" + i)); } KeyValue kv = new KeyValue(ROW, CF, Q, 1234L, V, tags); ByteBuffer dbb = ByteBuffer.allocateDirect(kv.getBuffer().length); ByteBufferUtils.copyFromArrayToBuffer(dbb, kv.getBuffer(), 0, kv.getBuffer().length); ByteBufferKeyValue offheapKV = new ByteBufferKeyValue(dbb, 0, kv.getBuffer().length, 0); return offheapKV; } }