/** * Transfers the specified source buffer's data to this buffer starting at * the specified absolute {@code index}. * * @param srcIndex the first index of the source * @param length the number of bytes to transfer * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, * if the specified {@code srcIndex} is less than {@code 0}, * if {@code index + length} is greater than * {@code this.capacity}, or * if {@code srcIndex + length} is greater than * {@code src.capacity} */ public void setBytes(int index, Slice src, int srcIndex, int length) { setBytes(index, src.data, src.offset + srcIndex, length); }
public static Slice copiedBuffer(ByteBuffer source) { Preconditions.checkNotNull(source, "source is null"); Slice copy = allocate(source.limit() - source.position()); copy.setBytes(0, source.duplicate().order(ByteOrder.LITTLE_ENDIAN)); return copy; }
public static Slice ensureSize(Slice existingSlice, int minWritableBytes) { if (existingSlice == null) { existingSlice = EMPTY_SLICE; } if (minWritableBytes <= existingSlice.length()) { return existingSlice; } int newCapacity; if (existingSlice.length() == 0) { newCapacity = 1; } else { newCapacity = existingSlice.length(); } int minNewCapacity = existingSlice.length() + minWritableBytes; while (newCapacity < minNewCapacity) { newCapacity <<= 1; } Slice newSlice = Slices.allocate(newCapacity); newSlice.setBytes(0, existingSlice, 0, existingSlice.length()); return newSlice; }