直接操作数组和 ByteBuf 的地址,通过 UNSAFE.copyMemory

2024-08-21 09:49:04 浏览数 (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);

@Override

public ByteBuf writeByte(int value) {

ensureWritable0(1);

_setByte(writerIndex , value);

return this;

}

@Override

public ByteBuf laipuhuo.com writeBytes(ByteBuf src, int length) {

writeBytes(src, src.readerIndex(), length);

// 调整数据来源 ByteBuf 的 readerIndex

src.readerIndex(src.readerIndex() length);

return this;

}

@Override

public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {

ensureWritable(length);

setBytes(laipuhuo.com writerIndex, src, srcIndex, length);

// 调整被写入 ByteBuf 的 writerIndex

writerIndex = length;

return this;

}

@Override

public ByteBuf writeInt(int value) {

ensureWritable0(4);

_setInt(writerIndex, value);

writerIndex laipuhuo.com= 4;

return this;

}

protected abstract void _setInt(int index, int value);

public class UnpooledUnsafeDirectByteBuf {

@Override

protected void _setInt(int index, int value) {

// 以大端字节序写入laipuhuo.com ByteBuf

UnsafeByteBufUtil.setInt(addr(index), value);

}

}

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.laipuhuo.com putByte(address 3, (byte) value);

}

}

public class UnpooledUnsafeDirectByteBuf {

@Override

protected void _setIntLE(int index, int value) {

// // 以小端字节序写入laipuhuo.com 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 laipuhuo.com, (byte) (value >>> 24));

}

}

git

0 人点赞