@Override public void get(ByteBuffer out, int sourceOffset, int length) { ByteBufferUtils.copyFromBufferToBuffer(buf, out, sourceOffset, length); }
@Override public void write(ByteBuffer buf, int offset) { ByteBufferUtils.copyFromBufferToBuffer(this.buf, buf, this.offset, offset, this.length); }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { checkSizeAndGrow(len); ByteBufferUtils.copyFromBufferToBuffer(b, curBuf, off, len); }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { int toWrite = 0; while (len > 0) { toWrite = Math.min(len, this.curBuf.remaining()); ByteBufferUtils.copyFromBufferToBuffer(b, this.curBuf, off, toWrite); off += toWrite; len -= toWrite; if (len > 0) { allocateNewBuffer();// The curBuf is over. Let us move to the next one } } } }
protected void checkSizeAndGrow(int extra) { long capacityNeeded = curBuf.position() + (long) extra; if (capacityNeeded > curBuf.limit()) { // guarantee it's possible to fit if (capacityNeeded > MAX_ARRAY_SIZE) { throw new BufferOverflowException(); } // double until hit the cap long nextCapacity = Math.min(curBuf.capacity() * 2L, MAX_ARRAY_SIZE); // but make sure there is enough if twice the existing capacity is still too small nextCapacity = Math.max(nextCapacity, capacityNeeded); ByteBuffer newBuf = allocate((int) nextCapacity, curBuf.isDirect()); curBuf.flip(); ByteBufferUtils.copyFromBufferToBuffer(curBuf, newBuf); curBuf = newBuf; } }
@Override public SingleByteBuff put(int offset, ByteBuff src, int srcOffset, int length) { if (src instanceof SingleByteBuff) { ByteBufferUtils.copyFromBufferToBuffer(((SingleByteBuff) src).buf, this.buf, srcOffset, offset, length); } else { // TODO we can do some optimization here? Call to asSubByteBuffer might // create a copy. ObjectIntPair<ByteBuffer> pair = new ObjectIntPair<>(); src.asSubByteBuffer(srcOffset, length, pair); if (pair.getFirst() != null) { ByteBufferUtils.copyFromBufferToBuffer(pair.getFirst(), this.buf, pair.getSecond(), offset, length); } } return this; }
private int decodeKeyValue(DataInputStream source, ByteBuffer buffer, int prevKeyOffset) throws IOException, EncoderBufferTooSmallException { int keyLength = ByteBufferUtils.readCompressedInt(source); int valueLength = ByteBufferUtils.readCompressedInt(source); int commonLength = ByteBufferUtils.readCompressedInt(source); int keyOffset; keyLength += commonLength; ensureSpace(buffer, keyLength + valueLength + KeyValue.ROW_OFFSET); buffer.putInt(keyLength); buffer.putInt(valueLength); // copy the prefix if (commonLength > 0) { keyOffset = buffer.position(); ByteBufferUtils.copyFromBufferToBuffer(buffer, buffer, prevKeyOffset, commonLength); } else { keyOffset = buffer.position(); } // copy rest of the key and value int len = keyLength - commonLength + valueLength; ByteBufferUtils.copyFromStreamToBuffer(buffer, source, len); return keyOffset; }
/** * Test copying from buffer. */ @Test public void testCopyFromBuffer() { ByteBuffer srcBuffer = ByteBuffer.allocate(array.length); ByteBuffer dstBuffer = ByteBuffer.allocate(array.length); srcBuffer.put(array); ByteBufferUtils.copyFromBufferToBuffer(srcBuffer, dstBuffer, array.length / 2, array.length / 4); for (int i = 0; i < array.length / 4; ++i) { assertEquals(srcBuffer.get(i + array.length / 2), dstBuffer.get(i)); } }
toRead = srcItem.limit() - srcOffset; toMove = Math.min(length, Math.min(toRead, toWrite)); ByteBufferUtils.copyFromBufferToBuffer(srcItem, destItem, srcOffset, offset, toMove); length -= toMove; if (length == 0) break;
ByteBufferUtils.copyFromBufferToBuffer(out, out, prevOffset, common); ByteBufferUtils.copyFromBufferToBuffer(out, out, state.prevOffset + KeyValue.ROW_OFFSET + KeyValue.ROW_LENGTH_SIZE + state.rowLength, state.familyLength ByteBufferUtils.copyFromBufferToBuffer(out, out, state.prevTimestampOffset, prefixTimestamp); state.prevTimestampOffset = out.position() - prefixTimestamp; out.put(state.type); if ((flag & FLAG_SAME_VALUE) != 0) { ByteBufferUtils.copyFromBufferToBuffer(out, out, state.prevOffset + KeyValue.ROW_OFFSET + prevKeyLength, state.valueLength); } else { ByteBufferUtils.copyFromStreamToBuffer(out, source, KeyValue.TYPE_SIZE); ByteBufferUtils.copyFromBufferToBuffer(out, out, state.prevOffset + KeyValue.ROW_OFFSET + prevKeyLength, state.valueLength); } else {
@Test public void testRelativeCopyFromBuffertoBuffer() { ByteBuffer bb1 = ByteBuffer.allocate(135); ByteBuffer bb2 = ByteBuffer.allocate(135); fillBB(bb1, (byte) 5); ByteBufferUtils.copyFromBufferToBuffer(bb1, bb2); assertTrue(bb1.position() == bb2.position()); assertTrue(bb1.limit() == bb2.limit()); bb1 = ByteBuffer.allocateDirect(135); bb2 = ByteBuffer.allocateDirect(135); fillBB(bb1, (byte) 5); ByteBufferUtils.copyFromBufferToBuffer(bb1, bb2); assertTrue(bb1.position() == bb2.position()); assertTrue(bb1.limit() == bb2.limit()); } @Test
ByteBufferUtils.copyFromBufferToBuffer(buffer, buffer, state.prevOffset + KeyValue.ROW_OFFSET, commonPrefix);
/** * 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; }
/** * 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 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; }
ByteBufferUtils.copyFromBufferToBuffer((ByteBuffer) input, (ByteBuffer) output, offset, offset, length);
/** * 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; }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { checkSizeAndGrow(len); ByteBufferUtils.copyFromBufferToBuffer(b, curBuf, off, len); }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { checkSizeAndGrow(len); ByteBufferUtils.copyFromBufferToBuffer(b, curBuf, off, len); }