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

2024-08-21 09:36:46 浏览数 (1)

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

}

}

0 人点赞