services-gateway-netty/src/main/java/io/scalecube/services/gateway/ServiceMessageCodec.java
/**
   * Encode a message, transform it to T.
   *
   * @param message the message to transform
   * @param transformer a function that accepts data and header {@link ByteBuf} and return the
   *     required T
   * @return the object (transformed message)
   * @throws MessageCodecException when encoding cannot be done.
   */
Encode a message, transform it to T.
@param message the message to transform
@param transformer a function that accepts data and header  ByteBuf and return the
required T
@return the object (transformed message)
@throws MessageCodecException when encoding cannot be done.
Encode a message transform it to T . @param message the message to transform @param transformer a function that accepts data and header ByteBuf and return the required T @return the object ( transformed message ) @throws MessageCodecException when encoding cannot be done .
public <T> T encodeAndTransform(
      ServiceMessage message, BiFunction<ByteBuf, ByteBuf, T> transformer)
      throws MessageCodecException {
    ByteBuf dataBuffer = Unpooled.EMPTY_BUFFER;
    ByteBuf headersBuffer = Unpooled.EMPTY_BUFFER;
    if (message.hasData(ByteBuf.class)) {
      dataBuffer = message.data();
    } else if (message.hasData()) {
      dataBuffer = ByteBufAllocator.DEFAULT.buffer();
      try {
        DataCodec dataCodec = DataCodec.getInstance(message.dataFormatOrDefault());
        dataCodec.encode(new ByteBufOutputStream(dataBuffer), message.data());
      } catch (Throwable ex) {
        ReferenceCountUtil.safestRelease(dataBuffer);
        LOGGER.error("Failed to encode data on: {}, cause: {}", message, ex);
        throw new MessageCodecException(
            "Failed to encode data on message q=" + message.qualifier(), ex);
      }
    }
    if (!message.headers().isEmpty()) {
      headersBuffer = ByteBufAllocator.DEFAULT.buffer();
      try {
        headersCodec.encode(new ByteBufOutputStream(headersBuffer), message.headers());
      } catch (Throwable ex) {
        ReferenceCountUtil.safestRelease(headersBuffer);
        ReferenceCountUtil.safestRelease(dataBuffer); 
        LOGGER.error("Failed to encode headers on: {}, cause: {}", message, ex);
        throw new MessageCodecException(
            "Failed to encode headers on message q=" + message.qualifier(), ex);
      }
    }
    return transformer.apply(dataBuffer, headersBuffer);
  }
public < T > T encodeAndTransform ( ServiceMessage message , BiFunction < ByteBuf , ByteBuf , T > transformer ) throws MessageCodecException { ByteBuf dataBuffer = Unpooled . EMPTY_BUFFER ; ByteBuf headersBuffer = Unpooled . EMPTY_BUFFER ; if ( message . hasData ( ByteBuf . class ) ) { dataBuffer = message . data ( ) ; } else if ( message . hasData ( ) ) { dataBuffer = ByteBufAllocator . DEFAULT . buffer ( ) ; try { DataCodec dataCodec = DataCodec . getInstance ( message . dataFormatOrDefault ( ) ) ; dataCodec . encode ( new ByteBufOutputStream ( dataBuffer ) , message . data ( ) ) ; } catch ( Throwable ex ) { ReferenceCountUtil . safestRelease ( dataBuffer ) ; LOGGER . error ( "Failed to encode data on: {}, cause: {}" , message , ex ) ; throw new MessageCodecException ( "Failed to encode data on message q=" + message . qualifier ( ) , ex ) ; } } if ( ! message . headers ( ) . isEmpty ( ) ) { headersBuffer = ByteBufAllocator . DEFAULT . buffer ( ) ; try { headersCodec . encode ( new ByteBufOutputStream ( headersBuffer ) , message . headers ( ) ) ; } catch ( Throwable ex ) { ReferenceCountUtil . safestRelease ( headersBuffer ) ; ReferenceCountUtil . safestRelease ( dataBuffer ) ; LOGGER . error ( "Failed to encode headers on: {}, cause: {}" , message , ex ) ; throw new MessageCodecException ( "Failed to encode headers on message q=" + message . qualifier ( ) , ex ) ; } } return transformer . apply ( dataBuffer , headersBuffer ) ; }
Encode a message, transform it to T.
 @param message the message to transform
 @param transformer a function that accepts data and header {@link ByteBuf} and return the
     required T
 @return the object (transformed message)
 @throws MessageCodecException when encoding cannot be done.
Encode a message transform it to T . @param message the message to transform @param transformer a function that accepts data and header { @link ByteBuf } and return the required T @return the object ( transformed message ) @throws MessageCodecException when encoding cannot be done .
// release data buf as well