zhao
2021-07-02 23ee356c6f260ecc1a48bbb8bd60932b979e4698
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
 
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) Under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You Under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed Under the License is distributed on an "AS Is" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations Under the License.
==================================================================== */
 
 
namespace HH.WMS.Utils.NPOI.HSSF.Record
{
 
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using HH.WMS.Utils.NPOI.Util;
    using HH.WMS.Utils.NPOI.Util.Collections;
    using HH.WMS.Utils.NPOI.HSSF.Record.Cont;
 
 
    /**
     * Title:        Static String Table Record
     * 
     * Description:  This holds all the strings for LabelSSTRecords.
     * 
     * REFERENCE:    PG 389 Microsoft Excel 97 Developer's Kit (ISBN:
     *               1-57231-498-2)
     * 
     * @author Andrew C. Oliver (acoliver at apache dot org)
     * @author Marc Johnson (mjohnson at apache dot org)
     * @author Glen Stampoultzis (glens at apache.org)
     *
     * @see org.apache.poi.hssf.record.LabelSSTRecord
     * @see org.apache.poi.hssf.record.ContinueRecord
     */
 
    public class SSTRecord : ContinuableRecord
    {
        public const short sid = 0x00FC;
        private static UnicodeString EMPTY_STRING = new UnicodeString("");
 
        /** how big can an SST record be? As big as any record can be: 8228 bytes */
        public const int MAX_RECORD_SIZE = 8228;
 
        /** standard record overhead: two shorts (record id plus data space size)*/
        public const int STD_RECORD_OVERHEAD =  2 * LittleEndianConsts.SHORT_SIZE;
 
        /** SST overhead: the standard record overhead, plus the number of strings and the number of Unique strings -- two ints */
        public const int SST_RECORD_OVERHEAD =                (STD_RECORD_OVERHEAD + (2 * LittleEndianConsts.INT_SIZE));
 
        /** how much data can we stuff into an SST record? That would be _max minus the standard SST record overhead */
        public const int MAX_DATA_SPACE = RecordInputStream.MAX_RECORD_DATA_SIZE - 8;//MAX_RECORD_SIZE - SST_RECORD_OVERHEAD;
 
        // /** overhead for each string includes the string's Char count (a short) and the flag describing its Charistics (a byte) */
        //public const int STRING_MINIMAL_OVERHEAD = LittleEndianConsts.SHORT_SIZE + LittleEndianConsts.BYTE_SIZE;
 
        /** Union of strings in the SST and EXTSST */
        private int field_1_num_strings;
 
        /** according to docs ONLY SST */
        private int field_2_num_unique_strings;
        private IntMapper<UnicodeString> field_3_strings;
 
        private SSTDeserializer deserializer;
 
        /** Offsets from the beginning of the SST record (even across continuations) */
        int[] bucketAbsoluteOffsets;
        /** Offsets relative the start of the current SST or continue record */
        int[] bucketRelativeOffsets;
 
        /**
         * default constructor
         */
        public SSTRecord()
        {
            field_1_num_strings = 0;
            field_2_num_unique_strings = 0;
            field_3_strings = new IntMapper<UnicodeString>();
            deserializer = new SSTDeserializer(field_3_strings);
        }
 
        /**
         * Constructs an SST record and Sets its fields appropriately.
         *
         * @param in the RecordInputstream to Read the record from
         */
 
        public SSTRecord(RecordInputStream in1)
        {
            // this method Is ALWAYS called after construction -- using
            // the nontrivial constructor, of course -- so this Is where
            // we initialize our fields
            field_1_num_strings = in1.ReadInt();
            field_2_num_unique_strings = in1.ReadInt();
            field_3_strings = new IntMapper<UnicodeString>();
            deserializer = new SSTDeserializer(field_3_strings);
            deserializer.ManufactureStrings(field_2_num_unique_strings, in1);
        }
 
        /**
         * Add a string.
         *
         * @param string string to be Added
         *
         * @return the index of that string in the table
         */
 
        public int AddString(UnicodeString str)
        {
            field_1_num_strings++;
            UnicodeString ucs = (str == null) ? EMPTY_STRING
                    : str;
            int rval;
            int index = field_3_strings.GetIndex(ucs);
 
            if (index != -1)
            {
                rval = index;
            }
            else
            {
                // This is a new string -- we didn't see it among the
                // strings we've already collected
                rval = field_3_strings.Size;
                field_2_num_unique_strings++;
                SSTDeserializer.AddToStringTable(field_3_strings, ucs);
            }
            return rval;
        }
 
        /**
         * @return number of strings
         */
 
        public int NumStrings
        {
            get { return field_1_num_strings; }
            set { field_1_num_strings = value; }
        }
 
        /**
         * @return number of Unique strings
         */
 
        public int NumUniqueStrings
        {
            get { return field_2_num_unique_strings; }
            set { field_2_num_unique_strings = value; }
        }
 
        /**
         * Get a particular string by its index
         *
         * @param id index into the array of strings
         *
         * @return the desired string
         */
 
        public UnicodeString GetString(int id)
        {
            return (UnicodeString)field_3_strings[id];
        }
 
        public bool IsString16bit(int id)
        {
            UnicodeString unicodeString = ((UnicodeString)field_3_strings[id]);
            return ((unicodeString.OptionFlags & 0x01) == 1);
        }
 
        /**
         * Return a debugging string representation
         *
         * @return string representation
         */
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
 
            buffer.Append("[SST]\n");
            buffer.Append("    .numstrings     = ")
                    .Append(StringUtil.ToHexString(NumStrings)).Append("\n");
            buffer.Append("    .uniquestrings  = ")
                    .Append(StringUtil.ToHexString(NumUniqueStrings)).Append("\n");
            for (int k = 0; k < field_3_strings.Size; k++)
            {
                UnicodeString s = (UnicodeString)field_3_strings[k];
                buffer.Append("    .string_" + k + "      = ")
                        .Append(s.GetDebugInfo()).Append("\n");
            }
            buffer.Append("[/SST]\n");
            return buffer.ToString();
        }
 
        /**
         * @return sid
         */
        public override short Sid
        {
            get { return sid; }
        }
 
        /**
         * @return hashcode
         */
        public override int GetHashCode()
    {
        return field_2_num_unique_strings;
    }
 
        public override bool Equals(Object o)
        {
            if ((o == null) || (o.GetType() != this.GetType()))
            {
                return false;
            }
            SSTRecord other = (SSTRecord)o;
 
            return ((field_1_num_strings == other
                    .field_1_num_strings) && (field_2_num_unique_strings == other
                    .field_2_num_unique_strings) && field_3_strings
                    .Equals(other.field_3_strings));
        }
 
        /**
         * @return an iterator of the strings we hold. All instances are
         *         UnicodeStrings
         */
 
        public IEnumerator GetStrings()
        {
            return field_3_strings.GetEnumerator(); 
        }
 
        /**
         * @return count of the strings we hold.
         */
 
        public int CountStrings
        {
            get { return field_3_strings.Size; }
        }
 
        /**
         * called by the class that Is responsible for writing this sucker.
         * Subclasses should implement this so that their data Is passed back in a
         * byte array.
         *
         * @return size
         */
 
    protected override void Serialize(ContinuableRecordOutput out1) {
        SSTSerializer serializer = new SSTSerializer(field_3_strings, NumStrings, NumUniqueStrings );
        serializer.Serialize(out1);
        bucketAbsoluteOffsets = serializer.BucketAbsoluteOffsets;
        bucketRelativeOffsets = serializer.BucketRelativeOffsets;
    }
 
        SSTDeserializer GetDeserializer()
        {
            return deserializer;
        }
 
        /**
         * Creates an extended string record based on the current contents of
         * the current SST record.  The offset within the stream to the SST record
         * Is required because the extended string record points directly to the
         * strings in the SST record.
         * 
         * NOTE: THIS FUNCTION MUST ONLY BE CALLED AFTER THE SST RECORD HAS BEEN
         *       SERIALIZED.
         *
         * @param sstOffset     The offset in the stream to the start of the
         *                      SST record.
         * @return  The new SST record.
         */
        public ExtSSTRecord CreateExtSSTRecord(int sstOffset)
        {
            if (bucketAbsoluteOffsets == null || bucketAbsoluteOffsets == null)
                throw new InvalidOperationException("SST record has not yet been Serialized.");
 
            ExtSSTRecord extSST = new ExtSSTRecord();
            extSST.NumStringsPerBucket=((short)8);
            int[] absoluteOffsets = (int[])bucketAbsoluteOffsets.Clone();
            int[] relativeOffsets = (int[])bucketRelativeOffsets.Clone();
            for (int i = 0; i < absoluteOffsets.Length; i++)
                absoluteOffsets[i] += sstOffset;
            extSST.SetBucketOffsets(absoluteOffsets, relativeOffsets);
            return extSST;
        }
 
        /**
         * Calculates the size in bytes of the EXTSST record as it would be if the
         * record was Serialized.
         *
         * @return  The size of the ExtSST record in bytes.
         */
        public int CalcExtSSTRecordSize()
        {
            return ExtSSTRecord.GetRecordSizeForStrings(field_3_strings.Size);
        }
    }
}