本文整理匯總了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));
}
}
示例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;
}
示例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);
}
}
}
示例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;
}
示例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);
}
示例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;
}
}
示例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));
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
示例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;
}
示例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());
}
示例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;
}