本文整理了Java中io.netty.buffer.ByteBuf.setIndex()
方法的一些代码示例,展示了ByteBuf.setIndex()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.setIndex()
方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:setIndex
[英]Sets the readerIndex and writerIndex of this buffer in one shot. This method is useful when you have to worry about the invocation order of #readerIndex(int) and #writerIndex(int)methods. For example, the following code will fail:
// Create a buffer whose readerIndex, writerIndex and capacity are
// 0, 0 and 8 respectively.
ByteBuf buf =
Unpooled.buffer(8);
// IndexOutOfBoundsException is thrown because the specified
// readerIndex (2) cannot be greater than the current writerIndex (0).
buf.readerIndex(2);
buf.writerIndex(4);
The following code will also fail:
// Create a buffer whose readerIndex, writerIndex and capacity are
// 0, 8 and 8 respectively.
ByteBuf buf =
Unpooled.wrappedBuffer(new byte[8]);
// readerIndex becomes 8.
buf.readLong();
// IndexOutOfBoundsException is thrown because the specified
// writerIndex (4) cannot be less than the current readerIndex (8).
buf.writerIndex(4);
buf.readerIndex(2);
By contrast, this method guarantees that it never throws an IndexOutOfBoundsException as long as the specified indexes meet basic constraints, regardless what the current index values of the buffer are:
// No matter what the current state of the buffer is, the following
// call always succeeds as long as the capacity of the buffer is not
// less than 4.
buf.setIndex(2, 4);
[中]一次性设置此缓冲区的readerIndex和writerIndex。当您需要考虑#readerIndex(int)和#writeridex(int)方法的调用顺序时,此方法非常有用。例如,以下代码将失败:
// Create a buffer whose readerIndex, writerIndex and capacity are
// 0, 0 and 8 respectively.
ByteBuf buf =
Unpooled.buffer(8);
// IndexOutOfBoundsException is thrown because the specified
// readerIndex (2) cannot be greater than the current writerIndex (0).
buf.readerIndex(2);
buf.writerIndex(4);
以下代码也将失败:
// Create a buffer whose readerIndex, writerIndex and capacity are
// 0, 8 and 8 respectively.
ByteBuf buf =
Unpooled.wrappedBuffer(new byte[8]);
// readerIndex becomes 8.
buf.readLong();
// IndexOutOfBoundsException is thrown because the specified
// writerIndex (4) cannot be less than the current readerIndex (8).
buf.writerIndex(4);
buf.readerIndex(2);
相比之下,此方法保证只要指定的索引满足基本约束,就不会引发IndexOutOfBoundsException,而不管缓冲区的当前索引值是什么:
// No matter what the current state of the buffer is, the following
// call always succeeds as long as the capacity of the buffer is not
// less than 4.
buf.setIndex(2, 4);
代码示例来源:origin: apache/incubator-dubbo
@Override
public void setIndex(int readerIndex, int writerIndex) {
buffer.setIndex(readerIndex, writerIndex);
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf setIndex(int readerIndex, int writerIndex) {
buf.setIndex(readerIndex, writerIndex);
return this;
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf setIndex(int readerIndex, int writerIndex) {
buf.setIndex(readerIndex, writerIndex);
return this;
}
代码示例来源:origin: redisson/redisson
@Override
public ByteBuf setIndex(int readerIndex, int writerIndex) {
buf.setIndex(readerIndex, writerIndex);
return this;
}
代码示例来源:origin: redisson/redisson
@Override
public ByteBuf setIndex(int readerIndex, int writerIndex) {
buf.setIndex(readerIndex, writerIndex);
return this;
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf duplicate() {
return duplicate0().setIndex(readerIndex(), writerIndex());
}
代码示例来源:origin: netty/netty
ByteBuf duplicate() {
return buf.duplicate().setIndex(idx(offset), idx(endOffset));
}
代码示例来源:origin: redisson/redisson
@Override
public ByteBuf duplicate() {
return duplicate0().setIndex(readerIndex(), writerIndex());
}
代码示例来源:origin: redisson/redisson
ByteBuf duplicate() {
return buf.duplicate().setIndex(idx(offset), idx(endOffset));
}
代码示例来源:origin: netty/netty
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) {
buf.checkIndex(index, length);
ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity());
if (length != 0) {
if (copy.hasMemoryAddress()) {
PlatformDependent.copyMemory(addr, copy.memoryAddress(), length);
copy.setIndex(0, length);
} else {
copy.writeBytes(buf, index, length);
}
}
return copy;
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf duplicate() {
return duplicate0().setIndex(idx(readerIndex()), idx(writerIndex()));
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf copy(int index, int length) {
checkIndex(index, length);
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
if (length != 0) {
if (copy.hasMemoryAddress()) {
PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
copy.setIndex(0, length);
} else {
copy.writeBytes(this, index, length);
}
}
return copy;
}
代码示例来源:origin: redisson/redisson
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) {
buf.checkIndex(index, length);
ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity());
if (length != 0) {
if (copy.hasMemoryAddress()) {
PlatformDependent.copyMemory(addr, copy.memoryAddress(), length);
copy.setIndex(0, length);
} else {
copy.writeBytes(buf, index, length);
}
}
return copy;
}
代码示例来源:origin: netty/netty
@Override
public ByteBuf duplicate() {
return unwrap().duplicate().setIndex(idx(readerIndex()), idx(writerIndex()));
}
代码示例来源:origin: redisson/redisson
@Override
public ByteBuf duplicate() {
return duplicate0().setIndex(idx(readerIndex()), idx(writerIndex()));
}
代码示例来源:origin: redisson/redisson
@Override
public ByteBuf copy(int index, int length) {
checkIndex(index, length);
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
if (length != 0) {
if (copy.hasMemoryAddress()) {
PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
copy.setIndex(0, length);
} else {
copy.writeBytes(this, index, length);
}
}
return copy;
}
代码示例来源:origin: redisson/redisson
@Override
public ByteBuf duplicate() {
return unwrap().duplicate().setIndex(idx(readerIndex()), idx(writerIndex()));
}
代码示例来源:origin: eclipse-vertx/vert.x
@Benchmark
public void netty() {
GET.setIndex(readerIndex, writeIndex);
nettyChannel.writeInbound(GET);
ByteBuf result = (ByteBuf) nettyChannel.outboundMessages().poll();
consume(result);
}
}
代码示例来源:origin: eclipse-vertx/vert.x
@Benchmark
public void vertx() {
GET.setIndex(readerIndex, writeIndex);
vertxChannel.writeInbound(GET);
ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll();
consume(result);
}
代码示例来源:origin: eclipse-vertx/vert.x
@Fork(value = 1, jvmArgsAppend = {
"-Dvertx.threadChecks=false",
"-Dvertx.disableContextTimings=true",
"-Dvertx.disableTCCL=true",
"-Dvertx.disableHttpHeadersValidation=true",
})
@Benchmark
public void vertxOpt() {
GET.setIndex(readerIndex, writeIndex);
vertxChannel.writeInbound(GET);
ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll();
consume(result);
}
内容来源于网络,如有侵权,请联系作者删除!