io.netty.buffer.ByteBufAllocator Java Examples

The following examples show how to use io.netty.buffer.ByteBufAllocator. 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: AbstractGraphSONMessageSerializerV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf serializeResponseAsBinary(final ResponseMessage responseMessage, final ByteBufAllocator allocator) throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
        final byte[] payload = mapper.writeValueAsBytes(responseMessage);
        encodedMessage = allocator.buffer(payload.length);
        encodedMessage.writeBytes(payload);

        return encodedMessage;
    } catch (Exception ex) {
        if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

        logger.warn(String.format("Response [%s] could not be serialized by %s.", responseMessage, AbstractGraphSONMessageSerializerV2d0.class.getName()), ex);
        throw new SerializationException(ex);
    }
}
 
Example #2
Source File: KryoCodec.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf encode(Object in) throws IOException {
    Kryo kryo = null;
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream baos = new ByteBufOutputStream(out);
        Output output = new Output(baos);
        kryo = get();
        kryo.writeClassAndObject(output, in);
        output.close();
        return baos.buffer();
    } catch (Exception e) {
        out.release();
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RedissonKryoCodecException(e);
    } finally {
        if (kryo != null) {
            yield(kryo);
        }
    }
}
 
Example #3
Source File: SpdyHeaderBlockZlibDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
void decode(ByteBufAllocator alloc, ByteBuf headerBlock, SpdyHeadersFrame frame) throws Exception {
    int len = setInput(headerBlock);

    int numBytes;
    do {
        numBytes = decompress(alloc, frame);
    } while (numBytes > 0);

    // z_stream has an internal 64-bit hold buffer
    // it is always capable of consuming the entire input
    if (decompressor.getRemaining() != 0) {
        // we reached the end of the deflate stream
        throw INVALID_HEADER_BLOCK;
    }

    headerBlock.skipBytes(len);
}
 
Example #4
Source File: RequestFormatTest.java    From tchannel-java with MIT License 6 votes vote down vote up
@Test
public void testReleaseArg1Fail() throws Exception {
    RawRequest request = new RawRequest.Builder("keyvalue-service", "setValue").setBody("Body").setArg2(
        ByteBufAllocator.DEFAULT.buffer())
        .build();
    assertNotNull( request.arg1);
    assertNotNull( request.arg2);
    assertNotNull( request.arg3);

    //forcefully force release of arg1 to fail
    request.arg1.release();

    try {
        request.release();
        fail();
    } catch (IllegalReferenceCountException ex) {
        //expected
    }

    assertNotNull( request.arg1); // tried , but failed
    assertNull( request.arg2);
    assertNull( request.arg3);
}
 
Example #5
Source File: SpdyHeaderBlockZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private ByteBuf encode(ByteBufAllocator alloc, int len) {
    ByteBuf compressed = alloc.heapBuffer(len);
    boolean release = true;
    try {
        while (compressInto(compressed)) {
            // Although unlikely, it's possible that the compressed size is larger than the decompressed size
            compressed.ensureWritable(compressed.capacity() << 1);
        }
        release = false;
        return compressed;
    } finally {
        if (release) {
            compressed.release();
        }
    }
}
 
Example #6
Source File: DotStuffingChunkedStream.java    From NioSmtpClient with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
  ByteBuf chunk = super.readChunk(allocator);
  if (chunk == null) {
    return null;
  }

  byte[] prevChunkTrailingBytes = new byte[2];
  prevChunkTrailingBytes[0] = trailingBytes[0];
  prevChunkTrailingBytes[1] = trailingBytes[1];

  updateTrailingBytes(chunk);

  boolean appendCRLF = isEndOfInput() && !(trailingBytes[0] == CR && trailingBytes[1] == LF);

  return DotStuffing.createDotStuffedBuffer(allocator, chunk, prevChunkTrailingBytes,
      appendCRLF ? MessageTermination.ADD_CRLF : MessageTermination.DO_NOT_TERMINATE);
}
 
Example #7
Source File: SpdyHeaderBlockZlibEncoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf encode(ByteBufAllocator alloc, SpdyHeadersFrame frame) throws Exception {
    if (frame == null) {
        throw new IllegalArgumentException("frame");
    }

    if (finished) {
        return Unpooled.EMPTY_BUFFER;
    }

    ByteBuf decompressed = super.encode(alloc, frame);
    try {
        if (!decompressed.isReadable()) {
            return Unpooled.EMPTY_BUFFER;
        }

        int len = setInput(decompressed);
        return encode(alloc, len);
    } finally {
        decompressed.release();
    }
}
 
Example #8
Source File: FileOperationEncoder.java    From fastdfs-client with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #9
Source File: CallResponseFrameContinueCodecTest.java    From tchannel-java with MIT License 6 votes vote down vote up
@Test
public void testEncodeWithError() throws Exception {

    CallResponseContinueFrame callResponseContinueFrame = new CallResponseContinueFrame(
        42,
         (byte) 1,
        null,
        0,
        Unpooled.wrappedBuffer("Hello, World!".getBytes(StandardCharsets.UTF_8))
    );
    ByteBufAllocator spy = spy(ByteBufAllocator.DEFAULT);
    ResultCaptor<ByteBuf> byteBufResultCaptor = new ResultCaptor<>();
    doAnswer(byteBufResultCaptor).when(spy).buffer(anyInt(), anyInt());

    try {
        callResponseContinueFrame.encodeHeader(spy);
        fail();
    } catch (Exception ex) {
        //expected
    }
    assertEquals(0, byteBufResultCaptor.getResult().refCnt());
}
 
Example #10
Source File: PrometheusMetricsGenerator.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static void generate(PulsarService pulsar, boolean includeTopicMetrics, boolean includeConsumerMetrics, OutputStream out) throws IOException {
    ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
    try {
        SimpleTextOutputStream stream = new SimpleTextOutputStream(buf);

        generateSystemMetrics(stream, pulsar.getConfiguration().getClusterName());

        NamespaceStatsAggregator.generate(pulsar, includeTopicMetrics, includeConsumerMetrics, stream);

        FunctionsStatsGenerator.generate(pulsar.getWorkerService(),
                pulsar.getConfiguration().getClusterName(), stream);

        generateBrokerBasicMetrics(pulsar, stream);

        generateManagedLedgerBookieClientMetrics(pulsar, stream);

        out.write(buf.array(), buf.arrayOffset(), buf.readableBytes());
    } finally {
        buf.release();
    }
}
 
Example #11
Source File: PingPongApp.java    From spring-cloud-rsocket with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("Duplicates")
RSocket accept(RSocket rSocket) {
	RSocket pong = new RSocketProxy(rSocket) {

		@Override
		public Flux<Payload> requestChannel(Publisher<Payload> payloads) {
			return Flux.from(payloads).map(Payload::getDataUtf8).doOnNext(str -> {
				int received = pingsReceived.incrementAndGet();
				log.info("received " + str + "(" + received + ") in Pong");
			}).map(PingPongApp::reply).map(reply -> {
				ByteBuf data = ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT,
						reply);
				ByteBuf routingMetadata = getForwardingMetadata(strategies,
						"ping", 1L);
				return DefaultPayload.create(data, routingMetadata);
			});
		}
	};
	return pong;
}
 
Example #12
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleValuesEndsWithNull() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(28));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(12);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}
 
Example #13
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testTruncatedHeaderValue() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(maxHeaderSize + 13));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(13);
    for (int i = 0; i < maxHeaderSize - 3; i++) {
        headerBlock.writeByte('a');
    }
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isTruncated());
    assertFalse(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}
 
Example #14
Source File: FileModifyEncoder.java    From azeroth with Apache License 2.0 5 votes vote down vote up
@Override
protected ByteBuf metadata(ByteBufAllocator alloc) {
    byte[] pathBytes = fileId.pathBytes();
    int metaLen = 3 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length;
    ByteBuf buf = alloc.buffer(metaLen);
    buf.writeLong(pathBytes.length);
    buf.writeLong(offset);
    buf.writeLong(size());
    buf.writeBytes(pathBytes);
    return buf;
}
 
Example #15
Source File: GraphBinaryMessageSerializerV1.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf serializeRequestAsBinary(final RequestMessage requestMessage, final ByteBufAllocator allocator) throws SerializationException {
    final ByteBuf buffer = allocator.buffer().writeByte(header.length).writeBytes(header);

    try {
        requestSerializer.writeValue(requestMessage, buffer, writer);
    } catch (Exception ex) {
        buffer.release();
        throw ex;
    }

    return buffer;
}
 
Example #16
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroNameLength() throws Exception {
    ByteBuf headerBlock = Unpooled.buffer(8);
    headerBlock.writeInt(1);
    headerBlock.writeInt(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
    headerBlock.release();
}
 
Example #17
Source File: MessageMemTable.java    From qmq with Apache License 2.0 5 votes vote down vote up
public MessageMemTable(final long tabletId, final long beginOffset, final int capacity) {
    super(tabletId, beginOffset, capacity);
    this.firstSequences = new HashMap<>();
    this.indexes = new HashMap<>();
    this.mem = ByteBufAllocator.DEFAULT.ioBuffer(capacity);
    this.rwLock = new ReentrantReadWriteLock();
}
 
Example #18
Source File: TypedJsonJacksonCodec.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf encode(Object in) throws IOException {
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
        ByteBufOutputStream os = new ByteBufOutputStream(out);
        mapObjectMapper.writeValue((OutputStream) os, in);
        return os.buffer();
    } catch (IOException e) {
        out.release();
        throw e;
    }
}
 
Example #19
Source File: ImpersonatingMitmManager.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public SSLEngine clientSslEngineFor(HttpRequest httpRequest, SSLSession sslSession) {
    String requestedHostname = HttpUtil.getHostFromRequest(httpRequest);

    try {
        SslContext ctx = getHostnameImpersonatingSslContext(requestedHostname, sslSession);

        return ctx.newEngine(ByteBufAllocator.DEFAULT);
    } catch (RuntimeException e) {
        throw new MitmException("Error creating SSLEngine for connection to client to impersonate upstream host: " + requestedHostname, e);
    }
}
 
Example #20
Source File: QueryPacketHandler.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Sends the token to the sender
 */
private void sendToken() {
    ByteBuf reply = ByteBufAllocator.DEFAULT.ioBuffer(10);
    reply.writeByte(HANDSHAKE);
    reply.writeInt(sessionId);
    reply.writeBytes(getTokenString(this.token, this.sender.getAddress()));
    reply.writeByte(0);

    sendPacket(reply);
}
 
Example #21
Source File: HttpClientFinalizer.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBufFlux responseContent() {
	ByteBufAllocator alloc = (ByteBufAllocator) configuration().options()
	                                                           .get(ChannelOption.ALLOCATOR);
	if (alloc == null) {
		alloc = ByteBufAllocator.DEFAULT;
	}

	@SuppressWarnings("unchecked")
	Mono<ChannelOperations<?, ?>> connector = (Mono<ChannelOperations<?, ?>>) connect();
	return ByteBufFlux.fromInbound(connector.flatMapMany(contentReceiver), alloc);
}
 
Example #22
Source File: PemPrivateKey.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link PemEncoded} value from the {@link PrivateKey}.
 */
static PemEncoded toPEM(ByteBufAllocator allocator, boolean useDirect, PrivateKey key) {
    // We can take a shortcut if the private key happens to be already
    // PEM/PKCS#8 encoded. This is the ideal case and reason why all
    // this exists. It allows the user to pass pre-encoded bytes straight
    // into OpenSSL without having to do any of the extra work.
    if (key instanceof PemEncoded) {
        return ((PemEncoded) key).retain();
    }

    ByteBuf encoded = Unpooled.wrappedBuffer(key.getEncoded());
    try {
        ByteBuf base64 = SslUtils.toBase64(allocator, encoded);
        try {
            int size = BEGIN_PRIVATE_KEY.length + base64.readableBytes() + END_PRIVATE_KEY.length;

            boolean success = false;
            final ByteBuf pem = useDirect ? allocator.directBuffer(size) : allocator.buffer(size);
            try {
                pem.writeBytes(BEGIN_PRIVATE_KEY);
                pem.writeBytes(base64);
                pem.writeBytes(END_PRIVATE_KEY);

                PemValue value = new PemValue(pem, true);
                success = true;
                return value;
            } finally {
                // Make sure we never leak that PEM ByteBuf if there's an Exception.
                if (!success) {
                    SslUtils.zerooutAndRelease(pem);
                }
            }
        } finally {
            SslUtils.zerooutAndRelease(base64);
        }
    } finally {
        SslUtils.zerooutAndRelease(encoded);
    }
}
 
Example #23
Source File: HttpUtilsTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void convertContentChunksToRawBytes_returns_null_if_total_bytes_is_zero() {
    // given
    Collection<HttpContent> chunkCollection = Arrays.asList(new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)),
            new DefaultHttpContent(new EmptyByteBuf(ByteBufAllocator.DEFAULT)));

    // when
    byte[] resultBytes = HttpUtils.convertContentChunksToRawBytes(chunkCollection);

    // then
    assertThat(resultBytes, nullValue());
}
 
Example #24
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingName() throws Exception {
    ByteBuf headerBlock = Unpooled.buffer(8);
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
    headerBlock.release();
}
 
Example #25
Source File: SetupRejectionTest.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
void requesterNewStreamsTerminatedAfterZeroErrorFrame() {
  LeaksTrackingByteBufAllocator allocator =
      LeaksTrackingByteBufAllocator.instrument(ByteBufAllocator.DEFAULT);
  TestDuplexConnection conn = new TestDuplexConnection(allocator);
  RSocketRequester rSocket =
      new RSocketRequester(
          conn,
          DefaultPayload::create,
          StreamIdSupplier.clientSupplier(),
          0,
          0,
          0,
          null,
          RequesterLeaseHandler.None,
          TestScheduler.INSTANCE);

  conn.addToReceivedBuffer(
      ErrorFrameCodec.encode(ByteBufAllocator.DEFAULT, 0, new RejectedSetupException("error")));

  StepVerifier.create(
          rSocket
              .requestResponse(DefaultPayload.create("test"))
              .delaySubscription(Duration.ofMillis(100)))
      .expectErrorMatches(
          err -> err instanceof RejectedSetupException && "error".equals(err.getMessage()))
      .verify(Duration.ofSeconds(5));
}
 
Example #26
Source File: HttpSendFileTests.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
TestCompletionHandler(AsynchronousFileChannel channel, FluxSink<ByteBuf> sink,
					  ByteBufAllocator allocator, int chunk) {
	this.channel = channel;
	this.sink = sink;
	this.allocator = allocator;
	this.chunk = chunk;
}
 
Example #27
Source File: SpdyFrameEncoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public ByteBuf encodePingFrame(ByteBufAllocator allocator, int id) {
    byte flags = 0;
    int length = 4;
    ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
    writeControlFrameHeader(frame, SPDY_PING_FRAME, flags, length);
    frame.writeInt(id);
    return frame;
}
 
Example #28
Source File: SpdyHeaderBlockRawDecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeNameLength() throws Exception {
    ByteBuf headerBlock = Unpooled.buffer(8);
    headerBlock.writeInt(1);
    headerBlock.writeInt(-1);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);

    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
    headerBlock.release();
}
 
Example #29
Source File: RetainingAsyncWritableChannel.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * @inheritDoc
 *
 * This method requires copying the buffer because {@link ByteBuffer}s do not have the concept of a reference count,
 * so the other side is free to reuse the buffer after the callback has been called (as opposed to when the ref count
 * reaches 0).
 */
@Override
public Future<Long> write(ByteBuffer src, Callback<Long> callback) {
  // still need to copy the buffer since the writer may decide to reuse the buffer after the callback is called.
  return writeInternal(() -> {
    ByteBuf copy = src.isDirect() ? ByteBufAllocator.DEFAULT.directBuffer(src.remaining())
        : PooledByteBufAllocator.DEFAULT.heapBuffer(src.remaining());
    copy.writeBytes(src);
    src.position(src.limit());
    return copy;
  }, callback);
}
 
Example #30
Source File: UnreleasableDirectByteBuf.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
UnreleasableDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
    super(alloc, initialBuffer, maxCapacity);
    // ServiceTalk buffers are unreleasable. There are some optimizations in Netty which use `refCnt() > 1` to
    // judge if a ByteBuf maybe shared, and if not shared Netty may assume is is safe to make changes to the
    // underlying storage (e.g. write reallocation, compact data in place) of the ByteBuf which may lead to
    // visibility issues across threads and data corruption. We retain() here to imply the ByteBuf maybe shared and
    // these optimizations are not safe.
    super.retain();
}