engine.io-server/src/main/java/io/socket/engineio/parser/ServerParser.java
/**
     * Encode an array of packets into a binary payload for transfer over transport.
     *
     * @param packets Array of packets to encode.
     * @param callback The callback to be called with the encoded data.
     */
Encode an array of packets into a binary payload for transfer over transport.
@param packets Array of packets to encode.
@param callback The callback to be called with the encoded data.
Encode an array of packets into a binary payload for transfer over transport . @param packets Array of packets to encode . @param callback The callback to be called with the encoded data .
@SuppressWarnings("Duplicates")
    public static void encodePayloadAsBinary(Packet[] packets, Parser.EncodeCallback<byte[]> callback) {
        if (packets.length == 0) {
            callback.call(new byte[0]);
            return;
        }
        final ArrayList<byte[]> results = new ArrayList<>(packets.length);
        for (Packet packet : packets) {
            encodePacket(packet, true, packet1 -> {
                if (packet1 instanceof String) {
                    String encodingLength = String.valueOf(((String) packet1).length());
                    byte[] sizeBuffer = new byte[encodingLength.length() + 2];
                    sizeBuffer[0] = (byte)0; 
                    for (int i = 0; i < encodingLength.length(); i ++) {
                        sizeBuffer[i + 1] = (byte)Character.getNumericValue(encodingLength.charAt(i));
                    }
                    sizeBuffer[sizeBuffer.length - 1] = (byte)255;
                    results.add(Buffer.concat(new byte[][] {
                            sizeBuffer,
                            ((String) packet1).getBytes(StandardCharsets.UTF_8)
                    }));
                } else {
                    String encodingLength = String.valueOf(((byte[]) packet1).length);
                    byte[] sizeBuffer = new byte[encodingLength.length() + 2];
                    sizeBuffer[0] = (byte)1; 
                    for (int i = 0; i < encodingLength.length(); i ++) {
                        sizeBuffer[i + 1] = (byte)Character.getNumericValue(encodingLength.charAt(i));
                    }
                    sizeBuffer[sizeBuffer.length - 1] = (byte)255;
                    results.add(Buffer.concat(new byte[][] {
                            sizeBuffer,
                            (byte[]) packet1
                    }));
                }
            });
        }
        callback.call(Buffer.concat(results.toArray(new byte[results.size()][])));
    }
@ SuppressWarnings ( "Duplicates" ) public static void encodePayloadAsBinary ( Packet [ ] packets , Parser . EncodeCallback < byte [ ] > callback ) { if ( packets . length == 0 ) { callback . call ( new byte [ 0 ] ) ; return ; } final ArrayList < byte [ ] > results = new ArrayList < > ( packets . length ) ; for ( Packet packet : packets ) { encodePacket ( packet , true , packet1 -> { if ( packet1 instanceof String ) { String encodingLength = String . valueOf ( ( ( String ) packet1 ) . length ( ) ) ; byte [ ] sizeBuffer = new byte [ encodingLength . length ( ) + 2 ] ; sizeBuffer [ 0 ] = ( byte ) 0 ; for ( int i = 0 ; i < encodingLength . length ( ) ; i ++ ) { sizeBuffer [ i + 1 ] = ( byte ) Character . getNumericValue ( encodingLength . charAt ( i ) ) ; } sizeBuffer [ sizeBuffer . length - 1 ] = ( byte ) 255 ; results . add ( Buffer . concat ( new byte [ ] [ ] { sizeBuffer , ( ( String ) packet1 ) . getBytes ( StandardCharsets . UTF_8 ) } ) ) ; } else { String encodingLength = String . valueOf ( ( ( byte [ ] ) packet1 ) . length ) ; byte [ ] sizeBuffer = new byte [ encodingLength . length ( ) + 2 ] ; sizeBuffer [ 0 ] = ( byte ) 1 ; for ( int i = 0 ; i < encodingLength . length ( ) ; i ++ ) { sizeBuffer [ i + 1 ] = ( byte ) Character . getNumericValue ( encodingLength . charAt ( i ) ) ; } sizeBuffer [ sizeBuffer . length - 1 ] = ( byte ) 255 ; results . add ( Buffer . concat ( new byte [ ] [ ] { sizeBuffer , ( byte [ ] ) packet1 } ) ) ; } } ) ; } callback . call ( Buffer . concat ( results . toArray ( new byte [ results . size ( ) ] [ ] ) ) ) ; }
Encode an array of packets into a binary payload for transfer over transport.
Encode an array of packets into a binary payload for transfer over transport .
// is a string // is binary