當前位置: 首頁>>代碼示例>>Java>>正文


Java CompositeByteBuf.addComponent方法代碼示例

本文整理匯總了Java中io.netty.buffer.CompositeByteBuf.addComponent方法的典型用法代碼示例。如果您正苦於以下問題:Java CompositeByteBuf.addComponent方法的具體用法?Java CompositeByteBuf.addComponent怎麽用?Java CompositeByteBuf.addComponent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.netty.buffer.CompositeByteBuf的用法示例。


在下文中一共展示了CompositeByteBuf.addComponent方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: writeInContext

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
protected void writeInContext() {
  CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
  for (;;) {
    ByteBuf buf = writeQueue.poll();
    if (buf == null) {
      break;
    }

    writeQueueSize.decrementAndGet();
    cbb.addComponent(true, buf);

    if (cbb.numComponents() == cbb.maxNumComponents()) {
      netSocket.write(Buffer.buffer(cbb));
      cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
    }
  }
  if (cbb.isReadable()) {
    netSocket.write(Buffer.buffer(cbb));
  }
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:21,代碼來源:TcpConnection.java

示例2: dotStuff

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private String dotStuff(String testString, int chunkSize) throws Exception {
  ByteArrayInputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
  DotStuffingChunkedStream chunkedStream = new DotStuffingChunkedStream(stream, chunkSize);

  CompositeByteBuf destBuffer = ALLOCATOR.compositeBuffer();

  while (!chunkedStream.isEndOfInput()) {
    destBuffer.addComponent(true, chunkedStream.readChunk(ALLOCATOR));
  }

  byte[] bytes = new byte[destBuffer.readableBytes()];
  destBuffer.getBytes(0, bytes);

  ReferenceCountUtil.release(destBuffer);
  return new String(bytes, CharsetUtil.UTF_8);
}
 
開發者ID:HubSpot,項目名稱:NioSmtpClient,代碼行數:17,代碼來源:DotStuffingChunkedStreamTest.java

示例3: testNetty

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Test
public void testNetty(){
	
	CompositeByteBuf byteBuf = ByteBufAllocator.DEFAULT.compositeBuffer();
	byteBuf.addComponent(Unpooled.wrappedBuffer("12345".getBytes()));
	byteBuf.addComponent(Unpooled.wrappedBuffer("abcde".getBytes()));

	System.out.println(ByteBufUtils.readToString(byteBuf));
	
	ByteBuf buf = Unpooled.wrappedBuffer(Unpooled.wrappedBuffer("134".getBytes()), Unpooled.wrappedBuffer("abc".getBytes()));
	System.out.println(buf.readableBytes());
	byte []result = new byte[buf.readableBytes()];
	buf.readBytes(result);
	System.out.println(new String(result));
	
}
 
開發者ID:ctripcorp,項目名稱:x-pipe,代碼行數:17,代碼來源:SimpleTest.java

示例4: writeCompressed

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private ByteBuf writeCompressed(ByteBuf message) throws IOException {
    CompositeByteBuf compressed = alloc.compositeBuffer();
    try (OutputStream compressingStream = compressor.compress(new ByteBufOutputStream(compressed))) {
        compressingStream.write(ByteBufUtil.getBytes(message));
    } finally {
        message.release();
    }

    int numCompressedBytes = compressed.readableBytes();
    if (maxOutboundMessageSize >= 0 && numCompressedBytes > maxOutboundMessageSize) {
        compressed.release();
        throw Status.RESOURCE_EXHAUSTED
                .withDescription(
                        String.format(
                                "message too large %d > %d", numCompressedBytes, maxOutboundMessageSize))
                .asRuntimeException();
    }

    ByteBuf header = alloc.buffer(HEADER_LENGTH);
    header.writeByte(COMPRESSED);
    header.writeInt(numCompressedBytes);
    compressed.addComponent(true, 0, header);

    return compressed;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:26,代碼來源:ArmeriaMessageFramer.java

示例5: writeUncompressed

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private ByteBuf writeUncompressed(ByteBuf message) {
    int messageLength = message.readableBytes();
    if (maxOutboundMessageSize >= 0 && messageLength > maxOutboundMessageSize) {
        throw Status.RESOURCE_EXHAUSTED
                .withDescription(
                        String.format("message too large %d > %d", messageLength, maxOutboundMessageSize))
                .asRuntimeException();
    }
    CompositeByteBuf buf = alloc.compositeBuffer();
    ByteBuf header = alloc.buffer(HEADER_LENGTH);
    header.writeByte(UNCOMPRESSED);
    header.writeInt(messageLength);
    buf.addComponent(true, header);
    buf.addComponent(true, message);

    return buf;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:18,代碼來源:ArmeriaMessageFramer.java

示例6: fetchDecoderOutput

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private byte[] fetchDecoderOutput() {
    CompositeByteBuf decoded = Unpooled.compositeBuffer();
    for (;;) {
        ByteBuf buf = decoder.readInbound();
        if (buf == null) {
            break;
        }
        if (!buf.isReadable()) {
            buf.release();
            continue;
        }
        decoded.addComponent(true, buf);
    }
    byte[] ret = ByteBufUtil.getBytes(decoded);
    decoded.release();
    return ret;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:18,代碼來源:ZlibStreamDecoder.java

示例7: 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;
    }
}
 
開發者ID:couchbase,項目名稱:couchbase-jvm-core,代碼行數:17,代碼來源:AbstractSubdocRequest.java

示例8: 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;
}
 
開發者ID:couchbase,項目名稱:couchbase-jvm-core,代碼行數:24,代碼來源:SubMultiLookupRequest.java

示例9: 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());
}
 
開發者ID:citlab,項目名稱:vs.msc.ws14,代碼行數:19,代碼來源:InboundEnvelopeDecoderTest.java

示例10: 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;
}
 
開發者ID:scalecube,項目名稱:socketio,代碼行數:27,代碼來源:PacketEncoder.java

示例11: createDotStuffedBuffer

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
/**
 * Returns a {@link CompositeByteBuf} that contains the same data as {@code sourceBuffer}, but with
 * SMTP dot-stuffing applied, and (if {@code} appendCRLF is true) a CRLF appended.
 *
 * <p>If dot-stuffing is not required, and {@code appendCRLF} is false, {@code sourceBuffer} is
 * returned. In all other cases, {@code allocator} will be used to create a new {@code ByteBuf}
 * with a {@code refCnt} of one.
 *
 * <p>The {@code previousBytes} parameter is used to maintain dot-stuffing across a series
 * of buffers. Pass the last two bytes of a previous buffer here to ensure an initial dot
 * will be escaped if necessary. Passing null indicates this is the first or only buffer
 * for this message.
 *
 * @param allocator the {@code ByteBufAllocator} to use for new {@code ByteBuf}s
 * @param sourceBuffer the source message data
 * @param previousBytes the previous two bytes of the message, or null
 * @param termination whether to append CRLF to the end of the returned buffer
 */
public static ByteBuf createDotStuffedBuffer(ByteBufAllocator allocator, ByteBuf sourceBuffer, byte[] previousBytes, MessageTermination termination) {
  int dotIndex = findDotAtBeginningOfLine(sourceBuffer, 0, normalisePreviousBytes(previousBytes));

  try {
    if (dotIndex == -1) {
      if (termination == MessageTermination.ADD_CRLF) {
        return allocator.compositeBuffer(2).addComponents(true, sourceBuffer.retainedSlice(), CR_LF_BUFFER.slice());
      } else {
        return sourceBuffer.retainedSlice();
      }
    }

    // Build a CompositeByteBuf to avoid copying
    CompositeByteBuf compositeByteBuf = allocator.compositeBuffer();
    compositeByteBuf.addComponents(true, sourceBuffer.retainedSlice(0, dotIndex), DOT_DOT_BUFFER.slice());

    int nextDotIndex;
    while ((nextDotIndex = findDotAtBeginningOfLine(sourceBuffer, dotIndex + 1, NOT_CR_LF)) != -1) {
      compositeByteBuf.addComponents(true, sourceBuffer.retainedSlice(dotIndex + 1, nextDotIndex - dotIndex - 1), DOT_DOT_BUFFER.slice());
      dotIndex = nextDotIndex;
    }

    compositeByteBuf.addComponent(true, sourceBuffer.retainedSlice(dotIndex + 1, sourceBuffer.readableBytes() - dotIndex - 1));

    if (termination == MessageTermination.ADD_CRLF) {
      compositeByteBuf.addComponent(true, CR_LF_BUFFER.slice());
    }

    return compositeByteBuf;
  } finally {
    sourceBuffer.release();
  }
}
 
開發者ID:HubSpot,項目名稱:NioSmtpClient,代碼行數:52,代碼來源:DotStuffing.java

示例12: terminate

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
private String terminate(String testString, int chunkSize) throws Exception {
  ByteArrayInputStream stream = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
  CrlfTerminatingChunkedStream chunkedStream = new CrlfTerminatingChunkedStream(stream, chunkSize);

  CompositeByteBuf destBuffer = ALLOCATOR.compositeBuffer();
  while (!chunkedStream.isEndOfInput()) {
    destBuffer.addComponent(true, chunkedStream.readChunk(ALLOCATOR));
  }

  byte[] bytes = new byte[destBuffer.readableBytes()];
  destBuffer.getBytes(0, bytes);
  destBuffer.release();
  return new String(bytes, CharsetUtil.UTF_8);
}
 
開發者ID:HubSpot,項目名稱:NioSmtpClient,代碼行數:15,代碼來源:CrlfTerminatingChunkedStreamTest.java

示例13: 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);
}
 
開發者ID:axbaretto,項目名稱:drill,代碼行數:41,代碼來源:ChunkCreationHandler.java

示例14: getWriteByteBuf

import io.netty.buffer.CompositeByteBuf; //導入方法依賴的package包/類
@Override
protected ByteBuf getWriteByteBuf() {
	
	int length = payload.length;
	CompositeByteBuf result = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, payload.length + 1);
	String prefix = String.format("%c%d\r\n", ASTERISK_BYTE, length);
	result.addComponent(Unpooled.wrappedBuffer(prefix.getBytes()));
	for(Object o : payload){
		ByteBuf buff = ParserManager.parse(o);
		result.addComponent(buff);
	}
	result.setIndex(0, result.capacity());
	return result;
}
 
開發者ID:ctripcorp,項目名稱:x-pipe,代碼行數:15,代碼來源:ArrayParser.java

示例15: 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);
}
 
開發者ID:wuyinxian124,項目名稱:netty4.0.27Learn,代碼行數:14,代碼來源:DatagramUnicastTest.java


注:本文中的io.netty.buffer.CompositeByteBuf.addComponent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。