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


Java CompositeByteBuf類代碼示例

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


CompositeByteBuf類屬於io.netty.buffer包,在下文中一共展示了CompositeByteBuf類的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: 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;
}
 
開發者ID:rodbate,項目名稱:fastdfs-spring-boot,代碼行數:19,代碼來源:FileOperationEncoder.java

示例3: toByteBuf

import io.netty.buffer.CompositeByteBuf; //導入依賴的package包/類
/**
 * Turns the given BytesReference into a ByteBuf. Note: the returned ByteBuf will reference the internal
 * pages of the BytesReference. Don't free the bytes of reference before the ByteBuf goes out of scope.
 */
public static ByteBuf toByteBuf(final BytesReference reference) {
    if (reference.length() == 0) {
        return Unpooled.EMPTY_BUFFER;
    }
    if (reference instanceof ByteBufBytesReference) {
        return ((ByteBufBytesReference) reference).toByteBuf();
    } else {
        final BytesRefIterator iterator = reference.iterator();
        // usually we have one, two, or three components from the header, the message, and a buffer
        final List<ByteBuf> buffers = new ArrayList<>(3);
        try {
            BytesRef slice;
            while ((slice = iterator.next()) != null) {
                buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
            }
            final CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
            composite.addComponents(true, buffers);
            return composite;
        } catch (IOException ex) {
            throw new AssertionError("no IO happens here", ex);
        }
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:28,代碼來源:Netty4Utils.java

示例4: 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;
}
 
開發者ID:warlock-china,項目名稱:azeroth,代碼行數:19,代碼來源:FileOperationEncoder.java

示例5: 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

示例6: readToBytes

import io.netty.buffer.CompositeByteBuf; //導入依賴的package包/類
public static byte[] readToBytes(ByteBuf byteBuf){

		
		if(byteBuf instanceof CompositeByteBuf){
			CompositeByteBuf compositeByteBuf = (CompositeByteBuf) byteBuf;
			ByteArrayOutputStream baous = new ByteArrayOutputStream();
			for(ByteBuf single : compositeByteBuf){
				try {
					baous.write(readToBytes(single));
				} catch (IOException e) {
					throw new IllegalStateException("write to ByteArrayOutputStream error", e);
				}
			}
			return baous.toByteArray();
		}else{
			byte []result = new byte[byteBuf.readableBytes()];
			byteBuf.readBytes(result);
			return result;
		}
	}
 
開發者ID:ctripcorp,項目名稱:x-pipe,代碼行數:21,代碼來源:ByteBufUtils.java

示例7: 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

示例8: add

import io.netty.buffer.CompositeByteBuf; //導入依賴的package包/類
/**
 * Try to add the given {@link CompositeByteBuf}. Returns {@code true} on success,
 * {@code false} otherwise.
 */
boolean add(CompositeByteBuf buf) {
    ByteBuffer[] buffers = buf.nioBuffers();
    if (count + buffers.length >= Native.IOV_MAX) {
        // No more room!
        return false;
    }
    for (int i = 0; i < buffers.length; i++) {
        ByteBuffer nioBuffer = buffers[i];
        int offset = nioBuffer.position();
        int len = nioBuffer.limit() - nioBuffer.position();
        if (len == 0) {
            // No need to add an empty buffer so just continue
            continue;
        }
        long addr = PlatformDependent.directBufferAddress(nioBuffer);

        add(addr, offset, len);
    }
    return true;
}
 
開發者ID:wuyinxian124,項目名稱:netty4.0.27Learn,代碼行數:25,代碼來源:IovArray.java

示例9: 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

示例10: 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

示例11: 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

示例12: 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

示例13: 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

示例14: 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

示例15: 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


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