首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >直接操作数组和 ByteBuf 的地址,通过 UNSAFE.copyMemory

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

原创
作者头像
用户7737280
发布2024-08-21 09:49:04
发布2024-08-21 09:49:04
1440
举报

@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));

}

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档