本文整理匯總了Java中io.netty.buffer.CompositeByteBuf.writerIndex方法的典型用法代碼示例。如果您正苦於以下問題:Java CompositeByteBuf.writerIndex方法的具體用法?Java CompositeByteBuf.writerIndex怎麽用?Java CompositeByteBuf.writerIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.buffer.CompositeByteBuf
的用法示例。
在下文中一共展示了CompositeByteBuf.writerIndex方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encode
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Override
public List<Object> encode(ByteBufAllocator alloc) {
ByteBuf meta = metadata(alloc);
ByteBuf head = alloc.buffer(FastdfsConstants.FDFS_HEAD_LEN);
head.writeLong(meta.readableBytes() + size);
head.writeByte(cmd());
head.writeByte(FastdfsConstants.ERRNO_OK);
CompositeByteBuf cbb = alloc.compositeBuffer();
cbb.addComponents(head, meta);
cbb.writerIndex(head.readableBytes() + meta.readableBytes());
List<Object> requests = new LinkedList<>();
requests.add(cbb);
requests.add(content);
return requests;
}
示例2: encode
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Override
public List<Object> encode(ByteBufAllocator alloc) {
ByteBuf meta = metadata(alloc);
ByteBuf head = alloc.buffer(FDFS_HEAD_LEN);
head.writeLong(meta.readableBytes() + size);
head.writeByte(cmd());
head.writeByte(ERRNO_OK);
CompositeByteBuf cbb = alloc.compositeBuffer();
cbb.addComponents(head, meta);
cbb.writerIndex(head.readableBytes() + meta.readableBytes());
List<Object> requests = new LinkedList<>();
requests.add(cbb);
requests.add(content);
return requests;
}
示例3: createContent
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
protected ByteBuf createContent(ByteBuf pathByteBuf, ByteBuf... restOfContent) {
if (restOfContent == null || restOfContent.length == 0) {
return pathByteBuf;
} else {
CompositeByteBuf composite = Unpooled.compositeBuffer(1 + restOfContent.length);
composite.addComponent(pathByteBuf);
composite.writerIndex(composite.writerIndex() + pathByteBuf.readableBytes());
for (ByteBuf component : restOfContent) {
composite.addComponent(component);
composite.writerIndex(composite.writerIndex() + component.readableBytes());
}
return composite;
}
}
示例4: encode
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private static ByteBuf encode(List<LookupCommand> commands) {
CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator?
for (LookupCommand command : commands) {
byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8);
short pathLength = (short) pathBytes.length;
ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator?
commandBuf.writeByte(command.opCode());
//flags
if (command.xattr()) {
commandBuf.writeByte(SUBDOC_FLAG_XATTR_PATH);
} else {
commandBuf.writeByte(0);
}
commandBuf.writeShort(pathLength);
//no value length
commandBuf.writeBytes(pathBytes);
compositeBuf.addComponent(commandBuf);
compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes());
}
return compositeBuf;
}
示例5: encode
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private static ByteBuf encode(EmbeddedChannel ch, Envelope... envelopes) {
for (Envelope env : envelopes) {
ch.writeOutbound(env);
if (env.getBuffer() != null) {
verify(env.getBuffer(), times(1)).recycleBuffer();
}
}
CompositeByteBuf encodedEnvelopes = new CompositeByteBuf(ByteBufAllocator.DEFAULT, false, envelopes.length);
ByteBuf buf;
while ((buf = (ByteBuf) ch.readOutbound()) != null) {
encodedEnvelopes.addComponent(buf);
}
return encodedEnvelopes.writerIndex(encodedEnvelopes.capacity());
}
示例6: encodePacket
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
public static ByteBuf encodePacket(final Packet packet) throws IOException {
ByteBuf dataBytes = packet.getData();
boolean hasData = dataBytes != null;
CompositeByteBuf compositeByteBuf = PooledByteBufAllocator.DEFAULT.compositeBuffer(hasData ? 1 : 2);
byte[] typeBytes = packet.getType().getValueAsBytes();
int headerCapacity = typeBytes.length + DELIMITER_LENGTH + DELIMITER_LENGTH + (hasData ? DELIMITER_LENGTH : 0);
ByteBuf headerByteBuf = PooledByteBufAllocator.DEFAULT.buffer(headerCapacity, headerCapacity);
headerByteBuf.writeBytes(typeBytes);
headerByteBuf.writeBytes(DELIMITER_BYTES);
headerByteBuf.writeBytes(DELIMITER_BYTES);
if (hasData) {
headerByteBuf.writeBytes(DELIMITER_BYTES);
}
compositeByteBuf.addComponent(headerByteBuf);
int compositeReadableBytes = headerByteBuf.readableBytes();
if (hasData) {
compositeByteBuf.addComponent(dataBytes);
compositeReadableBytes += dataBytes.readableBytes();
}
compositeByteBuf.writerIndex(compositeReadableBytes);
return compositeByteBuf;
}
示例7: encode
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("ChunkCreationHandler called with msg {} of size {} with chunkSize {}",
msg, msg.readableBytes(), chunkSize);
}
if (!ctx.channel().isOpen()) {
logger.debug("Channel closed, skipping encode inside {}.", RpcConstants.CHUNK_CREATION_HANDLER);
msg.release();
return;
}
// Calculate the number of chunks based on configured chunk size and input msg size
int numChunks = (int) Math.ceil((double) msg.readableBytes() / chunkSize);
// Initialize a composite buffer to hold numChunks chunk.
final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(numChunks);
int cbbWriteIndex = 0;
int currentChunkLen = min(msg.readableBytes(), chunkSize);
// Create slices of chunkSize from input msg and add it to the composite buffer.
while (numChunks > 0) {
final ByteBuf chunkBuf = msg.slice(msg.readerIndex(), currentChunkLen);
chunkBuf.retain();
cbb.addComponent(chunkBuf);
cbbWriteIndex += currentChunkLen;
msg.skipBytes(currentChunkLen);
--numChunks;
currentChunkLen = min(msg.readableBytes(), chunkSize);
}
// Update the writerIndex of composite byte buffer. Netty doesn't do it automatically.
cbb.writerIndex(cbbWriteIndex);
// Add the final composite bytebuf into output buffer.
out.add(cbb);
}
示例8: testSimpleSendCompositeDirectByteBuf
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
public void testSimpleSendCompositeDirectByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
CompositeByteBuf buf = Unpooled.compositeBuffer();
buf.addComponent(Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf.addComponent(Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
buf.writerIndex(4);
testSimpleSend0(sb, cb, buf, true, BYTES, 1);
CompositeByteBuf buf2 = Unpooled.compositeBuffer();
buf2.addComponent(Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf2.addComponent(Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
buf2.writerIndex(4);
testSimpleSend0(sb, cb, buf2, true, BYTES, 4);
}
示例9: testSimpleSendCompositeHeapByteBuf
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
public void testSimpleSendCompositeHeapByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
CompositeByteBuf buf = Unpooled.compositeBuffer();
buf.addComponent(Unpooled.buffer().writeBytes(BYTES, 0, 2));
buf.addComponent(Unpooled.buffer().writeBytes(BYTES, 2, 2));
buf.writerIndex(4);
testSimpleSend0(sb, cb, buf, true, BYTES, 1);
CompositeByteBuf buf2 = Unpooled.compositeBuffer();
buf2.addComponent(Unpooled.buffer().writeBytes(BYTES, 0, 2));
buf2.addComponent(Unpooled.buffer().writeBytes(BYTES, 2, 2));
buf2.writerIndex(4);
testSimpleSend0(sb, cb, buf2, true, BYTES, 4);
}
示例10: testSimpleSendCompositeMixedByteBuf
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
public void testSimpleSendCompositeMixedByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
CompositeByteBuf buf = Unpooled.compositeBuffer();
buf.addComponent(Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf.addComponent(Unpooled.buffer().writeBytes(BYTES, 2, 2));
buf.writerIndex(4);
testSimpleSend0(sb, cb, buf, true, BYTES, 1);
CompositeByteBuf buf2 = Unpooled.compositeBuffer();
buf2.addComponent(Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf2.addComponent(Unpooled.buffer().writeBytes(BYTES, 2, 2));
buf2.writerIndex(4);
testSimpleSend0(sb, cb, buf2, true, BYTES, 4);
}
示例11: testStreamStartIsOnlyWrittenOnce
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Test
public void testStreamStartIsOnlyWrittenOnce() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
'n', 'e', 't', 't', 'y'
});
channel.writeOutbound(in.copy());
in.readerIndex(0); // rewind the buffer to write the same data
channel.writeOutbound(in.copy());
assertTrue(channel.finish());
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
});
CompositeByteBuf actual = Unpooled.compositeBuffer();
for (;;) {
ByteBuf m = (ByteBuf) channel.readOutbound();
if (m == null) {
break;
}
actual.addComponent(m);
actual.writerIndex(actual.writerIndex() + m.readableBytes());
}
assertEquals(releaseLater(expected), releaseLater(actual));
in.release();
}
示例12: add
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private void add(CompositeByteBuf buf, HttpObject httpObject) {
if (httpObject instanceof HttpContent) {
HttpContent httpContent = (HttpContent) httpObject;
ByteBuf content = httpContent.content();
if (content.isReadable()) {
buf.addComponent(content.retain());
buf.writerIndex(buf.writerIndex() + content.readableBytes());
}
}
}
示例13: encode
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
public static ByteBuf encode(ByteBufAllocator allocator, TFrame frame) {
ByteBuf buffer = allocator.buffer(TFrame.FRAME_HEADER_LENGTH, TFrame.FRAME_HEADER_LENGTH);
// size:2
buffer.writeShort(frame.size + TFrame.FRAME_HEADER_LENGTH);
// type:1
buffer.writeByte(frame.type);
// reserved:1
buffer.writeZero(1);
// id:4
buffer.writeInt((int) frame.id);
// reserved:8
buffer.writeZero(8);
// TODO: refactor
if (frame.payload instanceof CompositeByteBuf) {
CompositeByteBuf cbf = (CompositeByteBuf) frame.payload;
cbf.addComponent(0, buffer);
cbf.writerIndex(cbf.writerIndex() + TFrame.FRAME_HEADER_LENGTH);
return cbf;
}
return Unpooled.wrappedBuffer(buffer, frame.payload);
}
示例14: writeArgs
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
public static @NotNull ByteBuf writeArgs(
@NotNull ByteBufAllocator allocator,
@NotNull ByteBuf header,
@NotNull List<ByteBuf> args
) {
int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes();
List<ByteBuf> bufs = new ArrayList<>(7);
bufs.add(header);
while (!args.isEmpty()) {
ByteBuf arg = args.get(0);
int len = writeArg(allocator, arg, writableBytes, bufs);
writableBytes -= len;
if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
break;
}
if (arg.readableBytes() == 0) {
args.remove(0);
}
}
CompositeByteBuf comp = allocator.compositeBuffer();
comp.addComponents(bufs);
comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes);
return comp;
}
示例15: testStreamStartIsOnlyWrittenOnce
import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Test
public void testStreamStartIsOnlyWrittenOnce() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
'n', 'e', 't', 't', 'y'
});
channel.writeOutbound(in.copy());
in.readerIndex(0); // rewind the buffer to write the same data
channel.writeOutbound(in.copy());
assertTrue(channel.finish());
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y',
});
CompositeByteBuf actual = Unpooled.compositeBuffer();
for (;;) {
ByteBuf m = (ByteBuf) channel.readOutbound();
if (m == null) {
break;
}
actual.addComponent(m);
actual.writerIndex(actual.writerIndex() + m.readableBytes());
}
assertEquals(releaseLater(expected), releaseLater(actual));
in.release();
}