Prepend fixed or variable length header in netty message

Prepend fixed or variable length header in netty message

By : Yuping Luo
Date : November 17 2020, 11:55 AM
may help you . Here's what I came up with; it supports variable length headers. I'm still a n00b at this, so comments welcome :-D
code :
   public class HeaderFieldEncoder extends MessageToByteEncoder<ByteBuf> { 
      public HeaderFieldEncoder(String header){
         this.header = header;
      protected void encode(
         ChannelHandlerContext ctx,
         ByteBuf msg,
         ByteBuf out
      )throws Exception {                           
         //header = "nChat v1.0"
         ByteBuf headerBuf = Unpooled.copiedBuffer(
         out.writeBytes(msg, msg.readerIndex(), msg.readableBytes());      

   public class HeaderFieldDecoder extends MessageToMessageDecoder<ByteBuf> {
      public HeaderFieldDecoder(String header){
         this.header = header;
      protected void decode(
         ChannelHandlerContext ctx,
         ByteBuf msg,
         List<Object> out
      )throws Exception {
         String extractedHeader = extractHeader(msg);
         //if desired, validate header
         String content = msg.toString(charset);

      private String extractHeader(ByteBuf msg){
         String headerField = null;
         if(msg.readableBytes() >= header.length()){
            byte[] headerBytes = new byte[header.length()];
            msg.readBytes(headerBytes, 0, header.length());
            headerField = new String(headerBytes, charset);
         return headerField;
String header = "nChat v1.0";
pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,0,2,0,2));
pipeline.addLast(new HeaderFieldDecoder(header));
pipeline.addLast(new StringDecoder());

pipeline.addLast(new LengthFieldPrepender(2)); 
pipeline.addLast(new HeaderFieldEncoder(header));
pipeline.addLast(new StringEncoder()); 

Share : facebook icon twitter icon
Regular Expression which matches fixed length chunk with variable length elements

Regular Expression which matches fixed length chunk with variable length elements

By : shushushushoes
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You can use a lookahead assertion (and you don't need the parentheses):
code :
Why would one implement a stack using a fixed length array instead of a variable length data structure?

Why would one implement a stack using a fixed length array instead of a variable length data structure?

By : aoraki
Date : March 29 2020, 07:55 AM
Does that help If you have the choice, typically you would be doing this to make it faster.
More information could be given depending on your programming language, but typically the advantages would be speed. By using a fixed size, the size of the reserved block of memory no longer needs to be jumbled around with any changes. Your memory can get fragmented (so a little bit of your data is over here, a little bit over there, and every time you change the array this can get worse.) so using a fixed size will keep it all in one place.
how to prepend ISO8583 message length to an ISO8583 message using j8583 library

how to prepend ISO8583 message length to an ISO8583 message using j8583 library

By : Anomised
Date : March 29 2020, 07:55 AM
may help you . Well you already use IsoMessage.write(outputStream, 2) to write a message to a stream, with its length prepended as a 2-byte header. In other words, that method does exactly what you're asking for, but you're already using it, so I'm not sure what the problem is; is the message not property received on the other side? Perhaps you should wrap the socket's outputStream in a BufferedOutputStream; IsoMessage.write will flush the stream after writing the message.
IsoMessage.writeData() gives you the byte array without any length headers.
Netty: How to receive variable length message on the server without any header / field indicating length

Netty: How to receive variable length message on the server without any header / field indicating length

By : Marielle Ong
Date : March 29 2020, 07:55 AM
this one helps. I would accept data in channelRead() and accumulate it in a buffer. Before return from channelRead() I would invoke read() on a Channel. You may need to record other data, as per your needs. When netty invokes channelReadComplete(), there is a moment to send whole buffer to your ResponseManager.
code :
private final AttributeKey<StringBuffer> dataKey = AttributeKey.valueOf("dataBuf");

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf byteBuf;
    String data, hostAddress;

    StringBuffer dataBuf = ctx.attr(dataKey).get();
    boolean allocBuf = dataBuf == null;
    if (allocBuf) dataBuf = new StringBuffer();

    byteBuf = (ByteBuf) msg;
    data = byteBuf.toString(CharsetUtil.UTF_8);
    hostAddress = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();

    if (!data.isEmpty()) {
        this.logger.info(String.format("Data received %s from %s", data, hostAddress));
    else {
        logger.info(String.format("NO Data received from %s", hostAddress));

    if (allocBuf) ctx.attr(dataKey).set(dataBuf);


public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    StringBuffer dataBuf = ctx.attr(dataKey).get();
    if (dataBuf != null) {
        String reply;

        reply = this.responseManager.reply(dataBuf.toString());

        if (reply != null) {
            ctx.write(Unpooled.copiedBuffer(reply, CharsetUtil.UTF_8));

Send variable length vector with constant header in one message

Send variable length vector with constant header in one message

By : Jerry Adair
Date : March 29 2020, 07:55 AM
I wish this helpful for you This program uses MPI_Pack and MPI_Unpack to send two different types in the same message:
code :
#include <mpi.h>
#include <stdio.h>

#define ARRAY_SIZE(array) \
    (sizeof(array) / sizeof(array[0]))

struct data_header {
    int32_t x;
    int32_t y;

MPI_Datatype dt_header;
MPI_Datatype dt_vector;

void sendmsg(void) {
    struct data_header header = { 1, 2 };
    float example[] = { 1.0, 2.0, 3.0, 4.0 };
    char buffer[4096];
    int position;

    MPI_Pack(&header, 1, dt_header, buffer, sizeof(buffer), &position, MPI_COMM_WORLD);
    MPI_Pack(example, 1, dt_vector, buffer, sizeof(buffer), &position, MPI_COMM_WORLD);
    MPI_Send(buffer, position, MPI_PACKED, 0, 0, MPI_COMM_WORLD);

void recvmsg(void) {
    struct data_header header;
    float example[4];
    char buffer[4096];
    int position = 0;

    MPI_Recv(buffer, sizeof(buffer), MPI_PACKED, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    MPI_Unpack(buffer, sizeof(buffer), &position, &header, 1, dt_header, MPI_COMM_WORLD);
    MPI_Unpack(buffer, sizeof(buffer), &position, example, 1, dt_vector, MPI_COMM_WORLD);

    printf("x = %d, y = %d\n", header.x, header.y);
    for (int index = 0; index < ARRAY_SIZE(example); index++) {
        printf("%f ", example[index]);

int main(void) {
    int world_size;
    int world_rank;

    MPI_Init(NULL, NULL);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    MPI_Type_contiguous(2, MPI_INT, &dt_header);
    MPI_Type_contiguous(4, MPI_FLOAT, &dt_vector);

    if (0 == world_rank) {
    else {


    return 0;
x = 1, y = 2
1.000000 2.000000 3.000000 4.000000 
Related Posts Related Posts :
  • Passing HttpServletResponse in Spring Boot controller method
  • Failed to load libraries: [netty_tcnative_linux_arm_32, netty_tcnative_linux_arm_32_fedora, netty_tcnative_arm_32, netty
  • How to build a small documents storage?
  • Getting pixel position of a text in an image
  • How do I get a list of Methods called from a Class in Eclipse IDE?
  • Convert string to day of week (not exact date)
  • Unable to understand how rounding is working for decimal digits (Java)
  • How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)
  • JTable Renderer not working with RowSorter?
  • From java.util.concurrent.Future<T> to play.libs.F.Promise<T>: How to do that in Java?
  • jUnit tests work in Eclipse, but fail in ant - java.lang.RuntimeException: Stub
  • How do i add a tooltip to existing gwt Textbox?
  • actor replying to non-actor
  • Google Cloud Messaging - server connection
  • Which classes are unmodifiable and cannot be intrumented and is there a way to bypass it?
  • Java & Jersey 1.17: Custom default response headers
  • Java rounding error
  • Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?
  • Arquillian test class variables lose value
  • java.util.ConcurrentModificationException while inserting in ArrayList
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • Java fx binding styles
  • Summing weights based on string in large file
  • Android ListView setOnItemClickListener not working
  • How is the Java Bootstrap Classloader loaded?
  • Check object is blank
  • Add image between text in JEditorPane
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org