zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
315
316
317
318
319
320
321
322
323
/* ====================================================================
   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.Cont
{
    using System;
 
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.Util;
 
    /**
     * An augmented {@link LittleEndianOutput} used for serialization of {@link ContinuableRecord}s.
     * This class keeps track of how much remaining space is available in the current BIFF record and
     * can start new {@link ContinueRecord}s as required. 
     * 
     * @author Josh Micich
     */
    public class ContinuableRecordOutput : ILittleEndianOutput
    {
 
        private ILittleEndianOutput _out;
        private UnknownLengthRecordOutput _ulrOutput;
        private int _totalPreviousRecordsSize;
 
        internal ContinuableRecordOutput(ILittleEndianOutput out1, int sid)
        {
            _ulrOutput = new UnknownLengthRecordOutput(out1, sid);
            _out = out1;
            _totalPreviousRecordsSize = 0;
        }
 
        public static ContinuableRecordOutput CreateForCountingOnly()
        {
            return new ContinuableRecordOutput(NOPOutput, -777); // fake sid
        }
 
        /**
         * @return total number of bytes written so far (including all BIFF headers) 
         */
        public int TotalSize
        {
            get
            {
                return _totalPreviousRecordsSize + _ulrOutput.TotalSize;
            }
        }
        /**
         * Terminates the last record (also updates its 'ushort size' field)
         */
        public void Terminate()
        {
            _ulrOutput.Terminate();
        }
        /**
         * @return number of remaining bytes of space in current record
         */
        public int AvailableSpace
        {
            get
            {
                return _ulrOutput.AvailableSpace;
            }
        }
 
        /**
         * Terminates the current record and starts a new {@link ContinueRecord} (regardless
         * of how much space is still available in the current record).
         */
        public void WriteContinue()
        {
            _ulrOutput.Terminate();
            _totalPreviousRecordsSize += _ulrOutput.TotalSize;
            _ulrOutput = new UnknownLengthRecordOutput(_out, ContinueRecord.sid);
        }
        public void WriteContinueIfRequired(int requiredContinuousSize)
        {
            if (_ulrOutput.AvailableSpace < requiredContinuousSize)
            {
                WriteContinue();
            }
        }
 
        /**
         * Writes the 'optionFlags' byte and encoded character data of a unicode string.  This includes:
         * <ul>
         * <li>byte optionFlags</li>
         * <li>encoded character data (in "ISO-8859-1" or "UTF-16LE" encoding)</li>
         * </ul>
         * 
         * Notes:
         * <ul>
         * <li>The value of the 'is16bitEncoded' flag is determined by the actual character data 
         * of <c>text</c></li>
         * <li>The string options flag is never separated (by a {@link ContinueRecord}) from the
         * first chunk of character data it refers to.</li>
         * <li>The 'ushort Length' field is assumed to have been explicitly written earlier.  Hence, 
         * there may be an intervening {@link ContinueRecord}</li>
         * </ul>
         */
        public void WriteStringData(String text)
        {
            bool is16bitEncoded = StringUtil.HasMultibyte(text);
            // calculate total size of the header and first encoded char
            int keepTogetherSize = 1 + 1; // ushort len, at least one character byte
            int optionFlags = 0x00;
            if (is16bitEncoded)
            {
                optionFlags |= 0x01;
                keepTogetherSize += 1; // one extra byte for first char
            }
            WriteContinueIfRequired(keepTogetherSize);
            WriteByte(optionFlags);
            WriteCharacterData(text, is16bitEncoded);
        }
        /**
         * Writes a unicode string complete with header and character data.  This includes:
         * <ul>
         * <li>ushort Length</li>
         * <li>byte optionFlags</li>
         * <li>ushort numberOfRichTextRuns (optional)</li>
         * <li>ushort extendedDataSize (optional)</li>
         * <li>encoded character data (in "ISO-8859-1" or "UTF-16LE" encoding)</li>
         * </ul>
         * 
         * The following bits of the 'optionFlags' byte will be set as appropriate:
         * <table border='1'>
         * <tr><th>Mask</th><th>Description</th></tr>
         * <tr><td>0x01</td><td>is16bitEncoded</td></tr>
         * <tr><td>0x04</td><td>hasExtendedData</td></tr>
         * <tr><td>0x08</td><td>isRichText</td></tr>
         * </table>
         * Notes:
         * <ul> 
         * <li>The value of the 'is16bitEncoded' flag is determined by the actual character data 
         * of <c>text</c></li>
         * <li>The string header fields are never separated (by a {@link ContinueRecord}) from the
         * first chunk of character data (i.e. the first character is always encoded in the same
         * record as the string header).</li>
         * </ul>
         */
        public void WriteString(String text, int numberOfRichTextRuns, int extendedDataSize)
        {
            bool is16bitEncoded = StringUtil.HasMultibyte(text);
            // calculate total size of the header and first encoded char
            int keepTogetherSize = 2 + 1 + 1; // ushort len, byte optionFlags, at least one character byte
            int optionFlags = 0x00;
            if (is16bitEncoded)
            {
                optionFlags |= 0x01;
                keepTogetherSize += 1; // one extra byte for first char
            }
            if (numberOfRichTextRuns > 0)
            {
                optionFlags |= 0x08;
                keepTogetherSize += 2;
            }
            if (extendedDataSize > 0)
            {
                optionFlags |= 0x04;
                keepTogetherSize += 4;
            }
            WriteContinueIfRequired(keepTogetherSize);
            WriteShort(text.Length);
            WriteByte(optionFlags);
            if (numberOfRichTextRuns > 0)
            {
                WriteShort(numberOfRichTextRuns);
            }
            if (extendedDataSize > 0)
            {
                WriteInt(extendedDataSize);
            }
            WriteCharacterData(text, is16bitEncoded);
        }
 
 
        private void WriteCharacterData(String text, bool is16bitEncoded)
        {
            int nChars = text.Length;
            int i = 0;
            if (is16bitEncoded)
            {
                while (true)
                {
                    int nWritableChars = Math.Min(nChars - i, _ulrOutput.AvailableSpace / 2);
                    for (; nWritableChars > 0; nWritableChars--)
                    {
                        _ulrOutput.WriteShort(text[i++]);
                    }
                    if (i >= nChars)
                    {
                        break;
                    }
                    WriteContinue();
                    WriteByte(0x01);
                }
            }
            else
            {
                while (true)
                {
                    int nWritableChars = Math.Min(nChars - i, _ulrOutput.AvailableSpace / 1);
                    for (; nWritableChars > 0; nWritableChars--)
                    {
                        _ulrOutput.WriteByte(text[i++]);
                    }
                    if (i >= nChars)
                    {
                        break;
                    }
                    WriteContinue();
                    WriteByte(0x00);
                }
            }
        }
 
        public void Write(byte[] b)
        {
            WriteContinueIfRequired(b.Length);
            _ulrOutput.Write(b);
        }
        public void Write(byte[] b, int offset, int len)
        {
            //WriteContinueIfRequired(len);
            //_ulrOutput.Write(b, offset, len);
            int i = 0;
            while (true)
            {
                int nWritableChars = Math.Min(len - i, _ulrOutput.AvailableSpace / 1);
                for (; nWritableChars > 0; nWritableChars--)
                {
                    _ulrOutput.WriteByte(b[offset + i++]);
                }
                if (i >= len)
                {
                    break;
                }
                WriteContinue();
            }
        }
        public void WriteByte(int v)
        {
            WriteContinueIfRequired(1);
            _ulrOutput.WriteByte(v);
        }
        public void WriteDouble(double v)
        {
            WriteContinueIfRequired(8);
            _ulrOutput.WriteDouble(v);
        }
        public void WriteInt(int v)
        {
            WriteContinueIfRequired(4);
            _ulrOutput.WriteInt(v);
        }
        public void WriteLong(long v)
        {
            WriteContinueIfRequired(8);
            _ulrOutput.WriteLong(v);
        }
        public void WriteShort(int v)
        {
            WriteContinueIfRequired(2);
            _ulrOutput.WriteShort(v);
        }
 
        ///**
        // * Allows optimised usage of {@link ContinuableRecordOutput} for sizing purposes only.
        // */
        private static ILittleEndianOutput NOPOutput = new DelayableLittleEndianOutput1();
 
        class DelayableLittleEndianOutput1 : IDelayableLittleEndianOutput
        {
 
            public ILittleEndianOutput CreateDelayedOutput(int size)
            {
                return this;
            }
            public void Write(byte[] b)
            {
                // does nothing
            }
            public void Write(byte[] b, int offset, int len)
            {
                // does nothing
            }
            public void WriteByte(int v)
            {
                // does nothing
            }
            public void WriteDouble(double v)
            {
                // does nothing
            }
            public void WriteInt(int v)
            {
                // does nothing
            }
            public void WriteLong(long v)
            {
                // does nothing
            }
            public void WriteShort(int v)
            {
                // does nothing
            }
        }
    }
}