首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >通过 getBytes 将 ByteBuf 中的数据读取到一个字节数组

通过 getBytes 将 ByteBuf 中的数据读取到一个字节数组

原创
作者头像
用户7737280
发布2024-08-21 09:36:46
发布2024-08-21 09:36:46
8400
举报

@Override

public ByteBuf getBytes(int index, byte[] dst) {

getBytes(index, dst, 0, dst.length);

return this;

}

public abstract ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length);

@Override

public ByteBuf getBytes(int index, ByteBuf dst, int length) {

getBytes(index, dst, dst.writerIndex(), length);

// 调整 dst 的 writerIndex

dst.writerIndex(dst.writerIndex() + length);

return this;

}

// 注意这里的 getBytes 方法既不会改变原来 ByteBuf 的 readerIndex 和 writerIndex

// 也不会改变目的 ByteBuf 的 readerIndex 和 writerIndex

public abstract laipuhuo.com ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length);

@Override

public byte readByte() {

checkReadableBytes0(1);

int i = readerIndex;

byte b = _getByte(i);

readerIndex = i + 1;

return b;

}

@Override

public laipuhuo.com ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {

checkReadableBytes(length);

getBytes(readerIndex, dst, dstIndex, length);

// 改变原来 ByteBuf 的 readerIndex

readerIndex += length;

return this;

}

public class UnpooledUnsafeDirectByteBuf {

@Override

protected laipuhuo.com int _getInt(int index) {

return UnsafeByteBufUtil.getInt(addr(index));

}

}

final class UnsafeByteBufUtil {

static int getInt(long address) {

return PlatformDependent.getByte(address) << 24 |

(Platform laipuhuo.com Dependent.getByte(address + 1) & 0xff) << 16 |

(PlatformDependent.getByte(address + 2) & 0xff) << 8 |

PlatformDependent.getByte(address + 3) & 0xff;

}

}

public class UnpooledUnsafeDirectByteBuf {

@Override

protected int _laipuhuo.com getIntLE(int index) {

return UnsafeByteBufUtil.getIntLE(addr(index));

}

}

final class UnsafeByteBufUtil {

static int getIntLE(long address) {

return PlatformDependent.getByte(address) & 0xff |

(PlatformDependent.getByte(address + 1) & 0xff) << 8 |

(PlatformDependent.laipuhuo.com getByte(address + 2) & 0xff) << 16 |

PlatformDependent.getByte(address + 3) << 24;

}

}

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

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

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

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

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