@ Override public ByteArray encodeAsByteArray ( ) throws IOException { final int size = messages . size ( ) ; if ( size == 0 ) return ByteArray . EMPTY_BYTE_ARRAY ; AoByteArrayOutputStream bout = new AoByteArrayOutputStream ( ) ; try { try ( DataOutputStream out = new DataOutputStream ( bout ) ) { out . writeInt ( size ) ; int count = 0 ; for ( Message message : messages ) { count ++ ; ByteArray byteArray = message . encodeAsByteArray ( ) ; final int capacity = byteArray . size ; out . writeByte ( message . getMessageType ( ) . getTypeByte ( ) ) ; out . writeInt ( capacity ) ; out . write ( byteArray . array , 0 , capacity ) ; } if ( count != size ) throw new ConcurrentModificationException ( ) ; } } finally { bout . close ( ) ; } return new ByteArray ( bout . getInternalByteArray ( ) , bout . size ( ) ) ; }
Encodes the messages into a single ByteArray . There is likely a more efficient implementation that reads - through but this is a simple implementation .