Пример #1
0
        internal void FlattenData(Stream os)
        {
            DataOutputStream dos = new DataOutputStream(os);
            int i;

            //  Remove comments and whitespace from the rules to make it smaller.
            string strippedRules = RBBIRuleScanner.StripRules(fRules);

            // Calculate the size of each section in the data in bytes.
            //   Sizes here are padded up to a multiple of 8 for better memory alignment.
            //   Sections sizes actually stored in the header are for the actual data
            //     without the padding.
            //
            int headerSize       = 24 * 4; // align8(sizeof(RBBIDataHeader));
            int forwardTableSize = Align8(fForwardTables.GetTableSize());
            int reverseTableSize = Align8(fReverseTables.GetTableSize());
            // int safeFwdTableSize = Align8(fSafeFwdTables.getTableSize());
            int safeRevTableSize = Align8(fSafeRevTables.GetTableSize());
            int trieSize         = Align8(fSetBuilder.GetTrieSize());
            int statusTableSize  = Align8(fRuleStatusVals.Count * 4);
            int rulesSize        = Align8((strippedRules.Length) * 2);

            int totalSize = headerSize
                            + forwardTableSize
                            + /* reverseTableSize */ 0
                            + /* safeFwdTableSize */ 0
                            + (safeRevTableSize > 0 ? safeRevTableSize : reverseTableSize)
                            + statusTableSize + trieSize + rulesSize;
            int outputPos = 0;               // Track stream position, starting from RBBIDataHeader.

            //
            // Write out an ICU Data Header
            //
            ICUBinary.WriteHeader(RBBIDataWrapper.DATA_FORMAT, RBBIDataWrapper.FORMAT_VERSION, 0, dos);

            //
            // Write out the RBBIDataHeader
            //
            int[] header = new int[RBBIDataWrapper.DH_SIZE];                 // sizeof struct RBBIDataHeader
            header[RBBIDataWrapper.DH_MAGIC]         = 0xb1a0;
            header[RBBIDataWrapper.DH_FORMATVERSION] = RBBIDataWrapper.FORMAT_VERSION;
            header[RBBIDataWrapper.DH_LENGTH]        = totalSize;                     // fLength, the total size of all rule sections.
            header[RBBIDataWrapper.DH_CATCOUNT]      = fSetBuilder.NumCharCategories; // fCatCount.

            // Only save the forward table and the safe reverse table,
            // because these are the only ones used at run-time.
            //
            // For the moment, we still build the other tables if they are present in the rule source files,
            // for backwards compatibility. Old rule files need to work, and this is the simplest approach.
            //
            // Additional backwards compatibility consideration: if no safe rules are provided, consider the
            // reverse rules to actually be the safe reverse rules.

            header[RBBIDataWrapper.DH_FTABLE]    = headerSize;        // fFTable
            header[RBBIDataWrapper.DH_FTABLELEN] = forwardTableSize;  // fTableLen

            // Do not save Reverse Table.
            header[RBBIDataWrapper.DH_RTABLE]    = header[RBBIDataWrapper.DH_FTABLE] + forwardTableSize; // fRTable
            header[RBBIDataWrapper.DH_RTABLELEN] = 0;                                                    // fRTableLen

            // Do not save the Safe Forward table.
            header[RBBIDataWrapper.DH_SFTABLE] = header[RBBIDataWrapper.DH_RTABLE]
                                                 + 0;                     // fSTable
            header[RBBIDataWrapper.DH_SFTABLELEN] = 0;                    // fSTableLen

            // Safe reverse table. Use if present, otherwise save regular reverse table as the safe reverse.
            header[RBBIDataWrapper.DH_SRTABLE] = header[RBBIDataWrapper.DH_SFTABLE]
                                                 + 0;                        // fSRTable
            if (safeRevTableSize > 0)
            {
                header[RBBIDataWrapper.DH_SRTABLELEN] = safeRevTableSize;
            }
            else
            {
                Debug.Assert(reverseTableSize > 0);
                header[RBBIDataWrapper.DH_SRTABLELEN] = reverseTableSize;
            }

            header[RBBIDataWrapper.DH_TRIE] = header[RBBIDataWrapper.DH_SRTABLE]
                                              + header[RBBIDataWrapper.DH_SRTABLELEN]; // fTrie
            header[RBBIDataWrapper.DH_TRIELEN]     = fSetBuilder.GetTrieSize();        // fTrieLen
            header[RBBIDataWrapper.DH_STATUSTABLE] = header[RBBIDataWrapper.DH_TRIE]
                                                     + header[RBBIDataWrapper.DH_TRIELEN];
            header[RBBIDataWrapper.DH_STATUSTABLELEN] = statusTableSize; // fStatusTableLen
            header[RBBIDataWrapper.DH_RULESOURCE]     = header[RBBIDataWrapper.DH_STATUSTABLE]
                                                        + statusTableSize;
            header[RBBIDataWrapper.DH_RULESOURCELEN] = strippedRules.Length * 2;
            for (i = 0; i < header.Length; i++)
            {
                dos.WriteInt32(header[i]);
                outputPos += 4;
            }

            // Write out the actual state tables.
            short[] tableData;
            tableData = fForwardTables.ExportTable();
            Assert.Assrt(outputPos == header[4]);
            for (i = 0; i < tableData.Length; i++)
            {
                dos.WriteInt16(tableData[i]);
                outputPos += 2;
            }

            /* do not write the reverse table
             * tableData = fReverseTables.exportTable();
             * Assert.Assrt(outputPos == header[6]);
             * for (i = 0; i < tableData.length; i++) {
             *  dos.WriteInt16(tableData[i]);
             *  outputPos += 2;
             * }
             */

            /* do not write safe forwards table
             * Assert.Assrt(outputPos == header[8]);
             * tableData = fSafeFwdTables.exportTable();
             * for (i = 0; i < tableData.length; i++) {
             *  dos.WriteInt16(tableData[i]);
             *  outputPos += 2;
             * }
             */

            // Write the safe reverse table.
            // If not present, write the plain reverse table (old style rule compatibility)
            Assert.Assrt(outputPos == header[10]);
            if (safeRevTableSize > 0)
            {
                tableData = fSafeRevTables.ExportTable();
            }
            else
            {
                tableData = fReverseTables.ExportTable();
            }
            for (i = 0; i < tableData.Length; i++)
            {
                dos.WriteInt16(tableData[i]);
                outputPos += 2;
            }

            // write out the Trie table
            Assert.Assrt(outputPos == header[12]);
            fSetBuilder.SerializeTrie(os);
            outputPos += header[13];
            while (outputPos % 8 != 0)
            { // pad to an 8 byte boundary
                dos.Write(0);
                outputPos += 1;
            }

            // Write out the status {tag} table.
            Assert.Assrt(outputPos == header[16]);
            foreach (var val in fRuleStatusVals)
            {
                dos.WriteInt32(val);
                outputPos += 4;
            }

            while (outputPos % 8 != 0)
            { // pad to an 8 byte boundary
                dos.Write(0);
                outputPos += 1;
            }

            // Write out the stripped rules (rules with extra spaces removed
            //   These go last in the data area, even though they are not last in the header.
            Assert.Assrt(outputPos == header[14]);
            dos.WriteChars(strippedRules);
            outputPos += strippedRules.Length * 2;
            while (outputPos % 8 != 0)
            { // pad to an 8 byte boundary
                dos.Write(0);
                outputPos += 1;
            }
        }
Пример #2
0
 private static void InvokeMethod(HttpSessionState session, Int32 requestID, DataInputStream input, DataOutputStream output)
 {
     _methods[requestID].Invoke(session, input);
     output.WriteInt16(HttpProcessor.RESULT_SUCCESSFUL);
     _methods[requestID].Return(output);
 }
Пример #3
0
 private static void InvokeMethod(HttpSessionState session, Int32 requestID, DataInputStream input, DataOutputStream output)
 {
     _methods[requestID].Invoke(session, input);
     output.WriteInt16(HttpProcessor.RESULT_SUCCESSFUL);
     _methods[requestID].Return(output);
 }