@ Override public byte [ ] encode ( final NetworkConnectionServiceMessage obj ) { final Codec codec = connFactoryMap . get ( obj . getConnectionFactoryId ( ) ) . getCodec ( ) ; Boolean isStreamingCodec = isStreamingCodecMap . get ( codec ) ; if ( isStreamingCodec == null ) { isStreamingCodec = codec instanceof StreamingCodec ; isStreamingCodecMap . putIfAbsent ( codec , isStreamingCodec ) ; } try ( final ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ) { try ( final DataOutputStream daos = new DataOutputStream ( baos ) ) { daos . writeUTF ( obj . getConnectionFactoryId ( ) ) ; daos . writeUTF ( obj . getSrcId ( ) . toString ( ) ) ; daos . writeUTF ( obj . getDestId ( ) . toString ( ) ) ; daos . writeInt ( obj . getData ( ) . size ( ) ) ; if ( isStreamingCodec ) { for ( final Object rec : obj . getData ( ) ) { ( ( StreamingCodec ) codec ) . encodeToStream ( rec , daos ) ; } } else { final Iterable dataList = obj . getData ( ) ; for ( final Object message : dataList ) { final byte [ ] bytes = codec . encode ( message ) ; daos . writeInt ( bytes . length ) ; daos . write ( bytes ) ; } } return baos . toByteArray ( ) ; } } catch ( final IOException e ) { throw new RuntimeException ( "IOException" , e ) ; } }
Encodes a network connection service message to bytes .