public byte [ ] toByteArray ( ) { final byte [ ] bytes = new byte [ getSerializedSizeBytes ( ) ] ; final boolean isSingleItem = n_ == 1 ; bytes [ PREAMBLE_INTS_BYTE ] = ( byte ) ( isEmpty ( ) || isSingleItem ? PREAMBLE_INTS_SHORT : PREAMBLE_INTS_FULL ) ; bytes [ SER_VER_BYTE ] = isSingleItem ? serialVersionUID2 : serialVersionUID1 ; bytes [ FAMILY_BYTE ] = ( byte ) Family . KLL . getID ( ) ; bytes [ FLAGS_BYTE ] = ( byte ) ( ( isEmpty ( ) ? 1 << Flags . IS_EMPTY . ordinal ( ) : 0 ) | ( isLevelZeroSorted_ ? 1 << Flags . IS_LEVEL_ZERO_SORTED . ordinal ( ) : 0 ) | ( isSingleItem ? 1 << Flags . IS_SINGLE_ITEM . ordinal ( ) : 0 ) ) ; ByteArrayUtil . putShortLE ( bytes , K_SHORT , ( short ) k_ ) ; bytes [ M_BYTE ] = ( byte ) m_ ; if ( isEmpty ( ) ) { return bytes ; } int offset = DATA_START_SINGLE_ITEM ; if ( ! isSingleItem ) { ByteArrayUtil . putLongLE ( bytes , N_LONG , n_ ) ; ByteArrayUtil . putShortLE ( bytes , MIN_K_SHORT , ( short ) minK_ ) ; bytes [ NUM_LEVELS_BYTE ] = ( byte ) numLevels_ ; offset = DATA_START ; // the last integer in levels_ is not serialized because it can be derived for ( int i = 0 ; i < numLevels_ ; i ++ ) { ByteArrayUtil . putIntLE ( bytes , offset , levels_ [ i ] ) ; offset += Integer . BYTES ; } ByteArrayUtil . putFloatLE ( bytes , offset , minValue_ ) ; offset += Float . BYTES ; ByteArrayUtil . putFloatLE ( bytes , offset , maxValue_ ) ; offset += Float . BYTES ; } final int numItems = getNumRetained ( ) ; for ( int i = 0 ; i < numItems ; i ++ ) { ByteArrayUtil . putFloatLE ( bytes , offset , items_ [ levels_ [ 0 ] + i ] ) ; offset += Float . BYTES ; } return bytes ; }
Returns serialized sketch in a byte array form .