payload_size += payload[i].remaining( bss );
}
//create and fill header buffer
DirectByteBuffer header;
if ( version >= MESSAGE_VERSION_SUPPORTS_PADDING ){
boolean enable_padding = padding_mode != AZMessageEncoder.PADDING_MODE_NONE;
short padding_length;
if ( enable_padding ){
if ( padding_mode == AZMessageEncoder.PADDING_MODE_MINIMAL ){
padding_length = (short)(( Math.random() * SMALL_PAD_MAX ));
}else{
padding_length = (short)(( Math.random() * ( payload_size>256?SMALL_PAD_MAX:BIG_PAD_MAX )));
}
if ( padding_length == 0 ){
enable_padding = false;
}
}else{
padding_length = 0;
}
byte flags = enable_padding?(byte)0x01:(byte)0x00;
int header_size = 4 + 4 + id_bytes.length + 1 + (enable_padding?(2+padding_length):0);
header = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_AZ_HEADER, header_size );
header.putInt( bss, header_size - 4 + payload_size );
header.putInt( bss, id_bytes.length );
header.put( bss, id_bytes );
byte version_and_flags = (byte)( ( flags << 4 ) | version );
header.put( bss, version_and_flags );
if ( enable_padding ){
byte[] padding = new byte[padding_length];
header.putShort( bss, padding_length );
header.put( bss, padding );
}
}else{
int header_size = 4 + 4 + id_bytes.length + 1;
header = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_AZ_HEADER, header_size );
header.putInt( bss, header_size - 4 + payload_size );
header.putInt( bss, id_bytes.length );
header.put( bss, id_bytes );
header.put( bss, version );
}
header.flip( bss );
DirectByteBuffer[] raw_buffs = new DirectByteBuffer[ payload.length + 1 ];
raw_buffs[0] = header;
for( int i=0; i < payload.length; i++ ) {
raw_buffs[i+1] = payload[i];