setBytes 方法用于向 ByteBuf 的指定位置 index

44 阅读1分钟
@Override
public ByteBuf setBytes(int index, ByteBuf src, int length) {
    setBytes(index, src, src.readerIndex(), length);
    // 调整 src 的  readerIndex
    src.readerIndex(src.readerIndex() + length);
    return this;
}

// 注意这里的 setBytes 方法既不会改变原来 ByteBuf 的 readerIndex 和 writerIndex
// 也不会改变目的 ByteBuf 的 readerIndex 和 writerIndex
public abstract ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length);

public class UnpooledUnsafeDirectByteBuf { @Override protected void _setInt(int index, int value) { // 以大端字节序写入 ByteBuf UnsafeByteBufUtil.setInt(addr(index), value); } www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail…

final class UnsafeByteBufUtil { static void setInt(long address, int value) { PlatformDependent.putByte(address, (byte) (value >>> 24)); PlatformDependent.putByte(address + 1, (byte) (value >>> 16)); PlatformDependent.putByte(address + 2, (byte) (value >>> 8)); PlatformDependent.putByte(address + 3, (byte) value);
} } www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail… www.laipuhuo.com/goodsDetail…

public class UnpooledUnsafeDirectByteBuf { @Override protected void _setIntLE(int index, int value) { // // 以小端字节序写入 ByteBuf UnsafeByteBufUtil.setIntLE(addr(index), value); } }

final class UnsafeByteBufUtil { static void setIntLE(long address, int value) { PlatformDependent.putByte(address, (byte) value); PlatformDependent.putByte(address + 1, (byte) (value >>> 8)); PlatformDependent.putByte(address + 2, (byte) (value >>> 16)); PlatformDependent.putByte(address + 3, (byte) (value >>> 24)); } }

final void ensureWritable0(int minWritableBytes) {
    final int writerIndex = writerIndex();
    // 为满足本次的写入操作,预期的 ByteBuf 容量大小
    final int targetCapacity = writerIndex + minWritableBytes;
    // 剩余容量可以满足本次写入要求,直接返回,不需要扩容
    if (targetCapacity >= 0 & targetCapacity <= capacity()) {
        return;
    }
    // 扩容后的容量不能超过 maxCapacity
    if (checkBounds && (targetCapacity < 0 || targetCapacity > maxCapacity)) {
        ensureAccessible();
        throw new IndexOutOfBoundsException(String.format(
                "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
                writerIndex, minWritableBytes, maxCapacity, this));
    }

    // 如果 targetCapacity 在(capacity , maxCapacity] 之间,则进行扩容
    // fastWritable 表示在不涉及到 memory reallocation or data-copy 的情况下,当前 ByteBuf 可以直接写入的容量
    // 对于 UnpooledDirectBuffer 这里的 fastWritable = capacity - writerIndex
    // PooledDirectBuffer 有另外的实现,这里先暂时不需要关注
    final int fastWritable = maxFastWritableBytes();
    // 计算扩容后的容量 newCapacity
    // 对于 UnpooledDirectBuffer 来说这里直接通过 calculateNewCapacity 计算扩容后的容量。
    int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
            : alloc().calculateNewCapacity(targetCapacity, maxCapacity);

    // 根据 new capacity 对 ByteBuf 进行扩容
    capacity(newCapacity);
}