Java Code Examples for io.netty.buffer.CompositeByteBuf#addComponent()
The following examples show how to use
io.netty.buffer.CompositeByteBuf#addComponent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleTest.java From x-pipe with Apache License 2.0 | 6 votes |
@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)); }
Example 2
Source File: TcpConnection.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
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)); } }
Example 3
Source File: AbstractIntegrationTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
protected void testIdentity(final byte[] data) { final ByteBuf in = Unpooled.wrappedBuffer(data); assertTrue(encoder.writeOutbound(in.retain())); assertTrue(encoder.finish()); final CompositeByteBuf compressed = Unpooled.compositeBuffer(); ByteBuf msg; while ((msg = encoder.readOutbound()) != null) { compressed.addComponent(true, msg); } assertThat(compressed, is(notNullValue())); decoder.writeInbound(compressed.retain()); assertFalse(compressed.isReadable()); final CompositeByteBuf decompressed = Unpooled.compositeBuffer(); while ((msg = decoder.readInbound()) != null) { decompressed.addComponent(true, msg); } assertEquals(in.resetReaderIndex(), decompressed); compressed.release(); decompressed.release(); in.release(); }
Example 4
Source File: LargeMessageSlicer.java From r2dbc-mysql with Apache License 2.0 | 6 votes |
private ByteBuf mergeNow() { int size = now.size(); if (size == 1) { return now.get(0); } int i = 0; CompositeByteBuf result = allocator.compositeBuffer(size); try { for (; i < size; ++i) { result.addComponent(true, now.get(i)); } return result; } catch (Throwable e) { ReferenceCountUtil.safeRelease(result); for (; i < size; ++i) { ReferenceCountUtil.safeRelease(now.get(i)); } throw e; } }
Example 5
Source File: LargeMessageSlicer.java From r2dbc-mysql with Apache License 2.0 | 6 votes |
private ByteBuf mergeNow() { int size = now.size(); if (size == 1) { return now.get(0); } int i = 0; CompositeByteBuf result = allocator.compositeBuffer(size); try { for (; i < size; ++i) { result.addComponent(true, now.get(i)); } return result; } catch (Throwable e) { ReferenceCountUtil.safeRelease(result); for (; i < size; ++i) { ReferenceCountUtil.safeRelease(now.get(i)); } throw e; } }
Example 6
Source File: CoalescingBufferQueue.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Override protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) { if (cumulation instanceof CompositeByteBuf) { CompositeByteBuf composite = (CompositeByteBuf) cumulation; composite.addComponent(true, next); return composite; } return composeIntoComposite(alloc, cumulation, next); }
Example 7
Source File: AbstractCoalescingBufferQueue.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Compose {@code cumulation} and {@code next} into a new {@link CompositeByteBuf}. */ protected final ByteBuf composeIntoComposite(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) { // Create a composite buffer to accumulate this pair and potentially all the buffers // in the queue. Using +2 as we have already dequeued current and next. CompositeByteBuf composite = alloc.compositeBuffer(size() + 2); try { composite.addComponent(true, cumulation); composite.addComponent(true, next); } catch (Throwable cause) { composite.release(); safeRelease(next); throwException(cause); } return composite; }
Example 8
Source File: SubMultiMutationRequest.java From couchbase-jvm-core with Apache License 2.0 | 5 votes |
private static ByteBuf encode(List<MutationCommand> commands) { //FIXME a way of using the pooled allocator? CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); for (MutationCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength + command.fragment().readableBytes()); commandBuf.writeByte(command.opCode()); byte subdocFlags = 0; if (command.createIntermediaryPath()) { subdocFlags |= KeyValueHandler.SUBDOC_BITMASK_MKDIR_P; } if (command.xattr()) { subdocFlags |= KeyValueHandler.SUBDOC_FLAG_XATTR_PATH; } if (command.expandMacros()) { subdocFlags |= KeyValueHandler.SUBDOC_FLAG_EXPAND_MACROS; } commandBuf.writeByte(subdocFlags); commandBuf.writeShort(pathLength); commandBuf.writeInt(command.fragment().readableBytes()); commandBuf.writeBytes(pathBytes); //copy the fragment but don't move indexes (in case it is retained and reused) commandBuf.writeBytes(command.fragment(), command.fragment().readerIndex(), command.fragment().readableBytes()); //eagerly release the fragment once it's been copied command.fragment().release(); //add the command to the composite buffer compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); } return compositeBuf; }
Example 9
Source File: DatagramUnicastTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
public void testSimpleSendCompositeHeapByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable { CompositeByteBuf buf = Unpooled.compositeBuffer(); buf.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 0, 2)); buf.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 2, 2)); testSimpleSend(sb, cb, buf, true, BYTES, 1); CompositeByteBuf buf2 = Unpooled.compositeBuffer(); buf2.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 0, 2)); buf2.addComponent(true, Unpooled.buffer().writeBytes(BYTES, 2, 2)); testSimpleSend(sb, cb, buf2, true, BYTES, 4); }
Example 10
Source File: RetainingAsyncWritableChannelTest.java From ambry with Apache License 2.0 | 5 votes |
/** * Test that {@link RetainingAsyncWritableChannel} behaves as expected: chunks are copied, callback completed * immediately after {@link RetainingAsyncWritableChannel#write} method completes. */ @Test public void basicsTestWithNettyByteBuf() throws Exception { for (boolean useCompositeByteBuf : Arrays.asList(false, true)) { List<byte[]> inputBuffers = getBuffers(1000, 20, 201, 0, 79, 1005); RetainingAsyncWritableChannel channel = new RetainingAsyncWritableChannel(); for (int i = 0; i < inputBuffers.size(); i++) { byte[] data = inputBuffers.get(i); ByteBuf chunk; if (data.length == 0) { chunk = Unpooled.wrappedBuffer(data); } else if (!useCompositeByteBuf) { chunk = ByteBufAllocator.DEFAULT.heapBuffer(data.length); chunk.writeBytes(data); } else { CompositeByteBuf composite = ByteBufAllocator.DEFAULT.compositeHeapBuffer(100); ByteBuf c = ByteBufAllocator.DEFAULT.heapBuffer(data.length / 2); c.writeBytes(data, 0, data.length / 2); composite.addComponent(true, c); c = ByteBufAllocator.DEFAULT.heapBuffer(data.length - data.length / 2); c.writeBytes(data, data.length / 2, data.length - data.length / 2); composite.addComponent(true, c); chunk = composite; } writeAndCheckCallback(chunk, channel, chunk.readableBytes(), null, null); } checkStream(inputBuffers, channel); channel.close(); writeAndCheckCallback(ByteBuffer.allocate(0), channel, 0, ClosedChannelException.class, null); } }
Example 11
Source File: CompositeBufferGatheringWriteTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private static ByteBuf newCompositeBuffer(ByteBufAllocator alloc) { CompositeByteBuf compositeByteBuf = alloc.compositeBuffer(); compositeByteBuf.addComponent(true, alloc.directBuffer(4).writeInt(100)); compositeByteBuf.addComponent(true, alloc.directBuffer(8).writeLong(123)); compositeByteBuf.addComponent(true, alloc.directBuffer(8).writeLong(456)); assertEquals(EXPECTED_BYTES, compositeByteBuf.readableBytes()); return compositeByteBuf; }
Example 12
Source File: NettyHandlerTestBase.java From grpc-java with Apache License 2.0 | 5 votes |
protected final ByteBuf captureWrite(ChannelHandlerContext ctx) { ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class); verify(ctx, atLeastOnce()).write(captor.capture(), any(ChannelPromise.class)); CompositeByteBuf composite = Unpooled.compositeBuffer(); for (ByteBuf buf : captor.getAllValues()) { composite.addComponent(buf); composite.writerIndex(composite.writerIndex() + buf.readableBytes()); } return composite; }
Example 13
Source File: ReactiveGrpcMethodMetadata.java From alibaba-rsocket-broker with Apache License 2.0 | 5 votes |
public ReactiveGrpcMethodMetadata(Method method, String group, String serviceName, String version) { this.serviceName = serviceName; this.name = method.getName(); this.returnType = method.getReturnType(); this.inferredClassForReturn = getInferredClassForGeneric(method.getGenericReturnType()); Class<?> parameterType = method.getParameterTypes()[0]; if (parameterType.isAssignableFrom(Mono.class) && returnType.isAssignableFrom(Mono.class)) { this.rpcType = UNARY; } else if (parameterType.isAssignableFrom(Mono.class) && returnType.isAssignableFrom(Flux.class)) { this.rpcType = SERVER_STREAMING; } else if (parameterType.isAssignableFrom(Flux.class) && returnType.isAssignableFrom(Mono.class)) { this.rpcType = CLIENT_STREAMING; } else if (parameterType.isAssignableFrom(Flux.class) && returnType.isAssignableFrom(Flux.class)) { this.rpcType = BIDIRECTIONAL_STREAMING; } this.serviceId = MurmurHash3.hash32(ServiceLocator.serviceId(group, serviceName, version)); this.handlerId = MurmurHash3.hash32(serviceName + "." + name); GSVRoutingMetadata routingMetadata = new GSVRoutingMetadata(group, serviceName, this.name, version); //payload binary routing metadata BinaryRoutingMetadata binaryRoutingMetadata = new BinaryRoutingMetadata(this.serviceId, this.handlerId, routingMetadata.assembleRoutingKey().getBytes(StandardCharsets.UTF_8)); //add param encoding MessageMimeTypeMetadata messageMimeTypeMetadata = new MessageMimeTypeMetadata(WellKnownMimeType.APPLICATION_PROTOBUF); RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(routingMetadata, messageMimeTypeMetadata); CompositeByteBuf compositeMetadataContent = (CompositeByteBuf) compositeMetadata.getContent(); //add BinaryRoutingMetadata as first compositeMetadataContent.addComponent(true, 0, binaryRoutingMetadata.getHeaderAndContent()); this.compositeMetadataByteBuf = Unpooled.copiedBuffer(compositeMetadataContent); ReferenceCountUtil.safeRelease(compositeMetadataContent); }
Example 14
Source File: HandshakeV10Request.java From r2dbc-mysql with Apache License 2.0 | 5 votes |
static HandshakeV10Request decodeV10(ByteBuf buf, HandshakeHeader header) { Builder builder = new Builder().header(header); CompositeByteBuf salt = buf.alloc().compositeBuffer(2); try { // After handshake header, MySQL give salt first part (should be 8-bytes always). salt.addComponent(true, readCStringRetainedSlice(buf)); int serverCapabilities; CompositeByteBuf capabilities = buf.alloc().compositeBuffer(2); try { // After salt first part, MySQL give the Server Capabilities first part (always 2-bytes). capabilities.addComponent(true, buf.readRetainedSlice(2)); // New protocol with 16 bytes to describe server character, but MySQL give lower 8-bits only. builder.collationLow8Bits(buf.readByte()) .serverStatuses(buf.readShortLE()); // No need release `capabilities` second part, it will release with `capabilities` serverCapabilities = capabilities.addComponent(true, buf.readRetainedSlice(2)) .readIntLE(); builder.serverCapabilities(serverCapabilities); } finally { capabilities.release(); } return afterCapabilities(builder, buf, serverCapabilities, salt); } finally { salt.release(); } }
Example 15
Source File: TaggingMetadataCodec.java From rsocket-java with Apache License 2.0 | 5 votes |
/** * create tagging content * * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. * @param tags tag values * @return tagging content */ public static ByteBuf createTaggingContent(ByteBufAllocator allocator, Collection<String> tags) { CompositeByteBuf taggingContent = allocator.compositeBuffer(); for (String key : tags) { int length = ByteBufUtil.utf8Bytes(key); if (length == 0 || length > TAG_LENGTH_MAX) { continue; } ByteBuf byteBuf = allocator.buffer().writeByte(length); byteBuf.writeCharSequence(key, StandardCharsets.UTF_8); taggingContent.addComponent(true, byteBuf); } return taggingContent; }
Example 16
Source File: HandshakeV10Request.java From r2dbc-mysql with Apache License 2.0 | 5 votes |
static HandshakeV10Request decodeV10(ByteBuf buf, HandshakeHeader header) { Builder builder = new Builder().header(header); CompositeByteBuf salt = buf.alloc().compositeBuffer(2); try { // After handshake header, MySQL give salt first part (should be 8-bytes always). salt.addComponent(true, readCStringRetainedSlice(buf)); int serverCapabilities; CompositeByteBuf capabilities = buf.alloc().compositeBuffer(2); try { // After salt first part, MySQL give the Server Capabilities first part (always 2-bytes). capabilities.addComponent(true, buf.readRetainedSlice(2)); // New protocol with 16 bytes to describe server character, but MySQL give lower 8-bits only. builder.collationLow8Bits(buf.readByte()) .serverStatuses(buf.readShortLE()); // No need release `capabilities` second part, it will release with `capabilities` serverCapabilities = capabilities.addComponent(true, buf.readRetainedSlice(2)) .readIntLE(); builder.serverCapabilities(serverCapabilities); } finally { capabilities.release(); } return afterCapabilities(builder, buf, serverCapabilities, salt); } finally { salt.release(); } }
Example 17
Source File: HandshakeV10Request.java From r2dbc-mysql with Apache License 2.0 | 4 votes |
private static HandshakeV10Request afterCapabilities( Builder builder, ByteBuf buf, int serverCapabilities, CompositeByteBuf salt ) { short saltSize; boolean isPluginAuth = (serverCapabilities & Capabilities.PLUGIN_AUTH) != 0; if (isPluginAuth) { saltSize = buf.readUnsignedByte(); } else { saltSize = 0; // If PLUGIN_AUTH flag not exists, MySQL server will return 0x00 always. buf.skipBytes(1); } // Reserved field, all bytes are 0x00. buf.skipBytes(RESERVED_SIZE); if ((serverCapabilities & Capabilities.SECURE_CONNECTION) != 0) { int saltSecondPartSize = Math.max(MIN_SALT_SECOND_PART_SIZE, saltSize - salt.readableBytes() - 1); ByteBuf saltSecondPart = buf.readSlice(saltSecondPartSize); // Always 0x00, and it is not the part of salt, ignore. buf.skipBytes(1); // No need release salt second part, it will release with `salt`. salt.addComponent(true, saltSecondPart.retain()); } builder.salt(ByteBufUtil.getBytes(salt)); if (isPluginAuth) { // See also MySQL bug 59453, auth type native name has no terminal character in // version less than 5.5.10, or version greater than 5.6.0 and less than 5.6.2 // And MySQL only support "mysql_native_password" in those versions that has the // bug, maybe just use constant "mysql_native_password" without read? int length = buf.bytesBefore(TERMINAL); String authType = length < 0 ? buf.toString(StandardCharsets.US_ASCII) : (length == 0 ? "" : buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII)); builder.authType(authType); } else { builder.authType(MySqlAuthProvider.NO_AUTH_PROVIDER); } return builder.build(); }
Example 18
Source File: ChunkDecoder.java From opc-ua-stack with Apache License 2.0 | 4 votes |
private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers) throws UaException { CompositeByteBuf composite = BufferUtil.compositeBuffer(); int signatureSize = delegate.getSignatureSize(channel); int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); boolean encrypted = delegate.isEncryptionEnabled(channel); boolean signed = delegate.isSigningEnabled(channel); for (ByteBuf chunkBuffer : chunkBuffers) { char chunkType = (char) chunkBuffer.getByte(3); chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE); delegate.readSecurityHeader(channel, chunkBuffer); if (encrypted) { decryptChunk(delegate, channel, chunkBuffer); } int encryptedStart = chunkBuffer.readerIndex(); chunkBuffer.readerIndex(0); if (signed) { delegate.verifyChunk(channel, chunkBuffer); } int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0; int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize; chunkBuffer.readerIndex(encryptedStart); SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer); long sequenceNumber = sequenceHeader.getSequenceNumber(); lastRequestId = sequenceHeader.getRequestId(); if (lastSequenceNumber == -1) { lastSequenceNumber = sequenceNumber; } else { if (lastSequenceNumber + 1 != sequenceNumber) { String message = String.format("expected sequence number %s but received %s", lastSequenceNumber + 1, sequenceNumber); logger.error(message); logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex())); throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message); } lastSequenceNumber = sequenceNumber; } ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex()); if (chunkType == 'A') { ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer); throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason()); } composite.addComponent(bodyBuffer); composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes()); } return composite.order(ByteOrder.LITTLE_ENDIAN); }
Example 19
Source File: HandshakeV10Request.java From r2dbc-mysql with Apache License 2.0 | 4 votes |
private static HandshakeV10Request afterCapabilities( Builder builder, ByteBuf buf, int serverCapabilities, CompositeByteBuf salt ) { short saltSize; boolean isPluginAuth = (serverCapabilities & Capabilities.PLUGIN_AUTH) != 0; if (isPluginAuth) { saltSize = buf.readUnsignedByte(); } else { saltSize = 0; // If PLUGIN_AUTH flag not exists, MySQL server will return 0x00 always. buf.skipBytes(1); } // Reserved field, all bytes are 0x00. buf.skipBytes(RESERVED_SIZE); if ((serverCapabilities & Capabilities.SECURE_CONNECTION) != 0) { int saltSecondPartSize = Math.max(MIN_SALT_SECOND_PART_SIZE, saltSize - salt.readableBytes() - 1); ByteBuf saltSecondPart = buf.readSlice(saltSecondPartSize); // Always 0x00, and it is not the part of salt, ignore. buf.skipBytes(1); // No need release salt second part, it will release with `salt`. salt.addComponent(true, saltSecondPart.retain()); } builder.salt(ByteBufUtil.getBytes(salt)); if (isPluginAuth) { // See also MySQL bug 59453, auth type native name has no terminal character in // version less than 5.5.10, or version greater than 5.6.0 and less than 5.6.2 // And MySQL only support "mysql_native_password" in those versions that has the // bug, maybe just use constant "mysql_native_password" without read? int length = buf.bytesBefore(TERMINAL); String authType = length < 0 ? buf.toString(StandardCharsets.US_ASCII) : (length == 0 ? "" : buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII)); builder.authType(authType); } else { builder.authType(MySqlAuthProvider.NO_AUTH_PROVIDER); } return builder.build(); }
Example 20
Source File: DecodeRunnable.java From Mycat-Balance with Apache License 2.0 | 4 votes |
/** * @param args */ public static void main(String[] args) { byte[] bs1 = new byte[] { 1, 10, 11, 12 }; byte[] bs2 = new byte[] { 2, 2, 2, 2 }; byte[] bs3 = new byte[] { 3, 3, 3, 3 }; byte[] bs4 = new byte[] { 4, 4, 4, 4 }; byte[] bs5 = new byte[] { 5, 5, 5, 5 }; byte[] bs6 = new byte[] { 6, 6, 6, 6 }; ByteBuffer buffer1 = ByteBuffer.allocate(1024); buffer1.put(bs1); buffer1.flip(); ByteBuf buf1 = Unpooled.copiedBuffer(buffer1);// .copiedBuffer(bs1); buffer1.put(bs3); ByteBuf buf2 = Unpooled.copiedBuffer(bs2); ByteBuf buf3 = Unpooled.copiedBuffer(bs3); ByteBuf buf4 = Unpooled.copiedBuffer(bs4); ByteBuf buf5 = Unpooled.copiedBuffer(bs5); ByteBuf buf6 = Unpooled.copiedBuffer(bs6); CompositeByteBuf cb = Unpooled.compositeBuffer(); cb.addComponents(buf1, buf2, buf3); byte dd = cb.getByte(0); CompositeByteBuf cb2 = Unpooled.compositeBuffer(); cb.addComponents(buf4, buf5, buf6); // cb.c // cb2.writerIndex(128 * 1024); cb.addComponent(cb2); Long number = cb2.readLong(); // causes IllegalBufferAccessException // here! }