public int serialize ( OutputStream os , boolean reduceTo16Bits , TrieBuilder . DataManipulate datamanipulate ) throws IOException { if ( datamanipulate == null ) { throw new IllegalArgumentException ( "Parameters can not be null" ) ; } // fold and compact if necessary, also checks that indexLength is // within limits if ( ! m_isCompacted_ ) { // compact once without overlap to improve folding compact ( false ) ; // fold the supplementary part of the index array fold ( datamanipulate ) ; // compact again with overlap for minimum data array length compact ( true ) ; m_isCompacted_ = true ; } // is dataLength within limits? int length ; if ( reduceTo16Bits ) { length = m_dataLength_ + m_indexLength_ ; } else { length = m_dataLength_ ; } if ( length >= MAX_DATA_LENGTH_ ) { throw new ArrayIndexOutOfBoundsException ( "Data length too small" ) ; } // struct UTrieHeader { // int32_t signature; // int32_t options (a bit field) // int32_t indexLength // int32_t dataLength length = Trie . HEADER_LENGTH_ + 2 * m_indexLength_ ; if ( reduceTo16Bits ) { length += 2 * m_dataLength_ ; } else { length += 4 * m_dataLength_ ; } if ( os == null ) { // No output stream. Just return the length of the serialized Trie, in bytes. return length ; } DataOutputStream dos = new DataOutputStream ( os ) ; dos . writeInt ( Trie . HEADER_SIGNATURE_ ) ; int options = Trie . INDEX_STAGE_1_SHIFT_ | ( Trie . INDEX_STAGE_2_SHIFT_ << Trie . HEADER_OPTIONS_INDEX_SHIFT_ ) ; if ( ! reduceTo16Bits ) { options |= Trie . HEADER_OPTIONS_DATA_IS_32_BIT_ ; } if ( m_isLatin1Linear_ ) { options |= Trie . HEADER_OPTIONS_LATIN1_IS_LINEAR_MASK_ ; } dos . writeInt ( options ) ; dos . writeInt ( m_indexLength_ ) ; dos . writeInt ( m_dataLength_ ) ; /* write the index (stage 1) array and the 16/32-bit data (stage 2) array */ if ( reduceTo16Bits ) { /* write 16-bit index values shifted right by UTRIE_INDEX_SHIFT, after adding indexLength */ for ( int i = 0 ; i < m_indexLength_ ; i ++ ) { int v = ( m_index_ [ i ] + m_indexLength_ ) >>> Trie . INDEX_STAGE_2_SHIFT_ ; dos . writeChar ( v ) ; } /* write 16-bit data values */ for ( int i = 0 ; i < m_dataLength_ ; i ++ ) { int v = m_data_ [ i ] & 0x0000ffff ; dos . writeChar ( v ) ; } } else { /* write 16-bit index values shifted right by UTRIE_INDEX_SHIFT */ for ( int i = 0 ; i < m_indexLength_ ; i ++ ) { int v = ( m_index_ [ i ] ) >>> Trie . INDEX_STAGE_2_SHIFT_ ; dos . writeChar ( v ) ; } /* write 32-bit data values */ for ( int i = 0 ; i < m_dataLength_ ; i ++ ) { dos . writeInt ( m_data_ [ i ] ) ; } } return length ; }
Serializes the build table to an output stream .