Search code examples
javabuffernettyhandlerdecoding

Do I have to set buffer sizes accordingly to the size of the payload in netty?


I read the netty.io tutorial for netty 4.1 and the object echo example from GitHub, but they did not go into detail about buffer size when sending objects that vary in size.

Let's assume I want to send an object that contains one int[] and one float[]. It's a DTO, so it's just constructor and getters.

How do I decide on a buffer size? I can't seem to find any answer, so I'm assuming it's a stupid question. But what to Google for to understand where I went off on this? I'm fairly new to networking in general.

I know that I need to add a decoder- and encoderHandlers to the respective channel pipelines, but I don't know how to implement them with varying sizes in objects in mind. I know about the classes ObjectDecoder and ObjectEncoder, but I don't know how big I should make the buffer because the arrays inside the DTO get created dynamically.


Solution

  • The usual way to serialize an array is to first record the length of the array, and then serialize the elements one by one I'm not sure if you meant to ask this, hope this helps

    package com.ljy.netty.codec;
    
    import java.util.Arrays;
    
    /**
     * Using big-endian byte ordering, reference to the Bits class of the java library
     */
    public class IntArrayCodecExample {
    
        public static byte[] encode(int[] ia) {
            byte[] bytes = new byte[ia.length * 4 + 4];
            writeInt(bytes, 0, ia.length);
            for (int i = 0; i < ia.length; i++) {
                writeInt(bytes, (i + 1) * 4, ia[i]);
            }
    
            return bytes;
        }
    
        public static int[] decode(byte[] bytes) {
            int length = readInt(bytes, 0);
            int[] ia = new int[length];
            int offset = 4;
            while(offset < bytes.length) {
                ia[offset / 4 - 1] = readInt(bytes, offset);
                offset += 4;
            }
    
            return ia;
        }
    
    
        private static void writeInt(byte[] bytes, int offset, int val) {
            bytes[offset + 3] = (byte) (val       );
            bytes[offset + 2] = (byte) (val >>>  8);
            bytes[offset + 1] = (byte) (val >>> 16);
            bytes[offset    ] = (byte) (val >>> 24);
        }
    
        private static int readInt(byte[] b, int off) {
            return ((b[off + 3] & 0xFF)      ) +
                    ((b[off + 2] & 0xFF) <<  8) +
                    ((b[off + 1] & 0xFF) << 16) +
                    ((b[off    ]       ) << 24);
        }
    
    
    
        public static void main(String[] args) {
            int[] ia = new int[]{1, 3, 2, 4, 5};
            System.out.println(Arrays.toString(decode(encode(ia))));
        }
    }