• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp / 22540418587

01 Mar 2026 09:23AM UTC coverage: 76.557% (+0.003%) from 76.554%
22540418587

Pull #990

github

web-flow
Merge 4f2ea7da4 into 90684cc55
Pull Request #990: issue-989 Refactor DXF polyline boundary

8202 of 11664 branches covered (70.32%)

Branch coverage included in aggregate %.

25 of 29 new or added lines in 1 file covered. (86.21%)

9 existing lines in 1 file now uncovered.

29715 of 37864 relevant lines covered (78.48%)

150233.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.35
/src/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs
1
using CSMath;
2
using CSUtilities.Converters;
3
using CSUtilities.IO;
4
using System;
5
using System.IO;
6
using System.Text;
7

8
namespace ACadSharp.IO.DWG
9
{
10
        /// <summary>
11
        /// Writer equivalent to reader <see cref="DwgStreamReaderBase"/>
12
        /// </summary>
13
        internal abstract class DwgStreamWriterBase : StreamIO, IDwgStreamWriter
14
        {
15
                public IDwgStreamWriter Main { get { return this; } }
432✔
16

17
                public long PositionInBits { get { return this.Position * 8 + this.BitShift; } }
226,299✔
18

19
                public long SavedPositionInBits { get; } = 0;
5,125✔
20

21
                public int BitShift { get; private set; } = 0;
8,406,197✔
22

23
                private byte _lastByte;
24

25
                public DwgStreamWriterBase(Stream stream, Encoding encoding) : base(stream)
5,125✔
26
                {
5,125✔
27
                        this.Encoding = encoding;
5,125✔
28
                }
5,125✔
29

30
                public static IDwgStreamWriter GetStreamWriter(ACadVersion version, Stream stream, Encoding encoding)
31
                {
2,650✔
32
                        switch (version)
2,650!
33
                        {
34
                                case ACadVersion.Unknown:
35
                                        throw new Exception();
×
36
                                case ACadVersion.MC0_0:
37
                                case ACadVersion.AC1_2:
38
                                case ACadVersion.AC1_4:
39
                                case ACadVersion.AC1_50:
40
                                case ACadVersion.AC2_10:
41
                                case ACadVersion.AC1002:
42
                                case ACadVersion.AC1003:
43
                                case ACadVersion.AC1004:
44
                                case ACadVersion.AC1006:
45
                                case ACadVersion.AC1009:
46
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
47
                                case ACadVersion.AC1012:
48
                                case ACadVersion.AC1014:
49
                                        return new DwgStreamWriterAC12(stream, encoding);
22✔
50
                                case ACadVersion.AC1015:
51
                                        return new DwgStreamWriterAC15(stream, encoding);
410✔
52
                                case ACadVersion.AC1018:
53
                                        return new DwgStreamWriterAC18(stream, encoding);
554✔
54
                                case ACadVersion.AC1021:
55
                                        return new DwgStreamWriterAC21(stream, encoding);
2✔
56
                                case ACadVersion.AC1024:
57
                                case ACadVersion.AC1027:
58
                                case ACadVersion.AC1032:
59
                                        return new DwgStreamWriterAC24(stream, encoding);
1,662✔
60
                                default:
61
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
62
                        }
63
                }
2,650✔
64

65
                public static IDwgStreamWriter GetMergedWriter(ACadVersion version, Stream stream, Encoding encoding)
66
                {
921✔
67
                        switch (version)
921!
68
                        {
69
                                case ACadVersion.Unknown:
70
                                        throw new Exception();
×
71
                                case ACadVersion.MC0_0:
72
                                case ACadVersion.AC1_2:
73
                                case ACadVersion.AC1_4:
74
                                case ACadVersion.AC1_50:
75
                                case ACadVersion.AC2_10:
76
                                case ACadVersion.AC1002:
77
                                case ACadVersion.AC1003:
78
                                case ACadVersion.AC1004:
79
                                case ACadVersion.AC1006:
80
                                case ACadVersion.AC1009:
81
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
82
                                case ACadVersion.AC1012:
83
                                case ACadVersion.AC1014:
84
                                        return new DwgmMergedStreamWriterAC14(
10✔
85
                                                stream,
10✔
86
                                                new DwgStreamWriterAC12(stream, encoding),
10✔
87
                                                new DwgStreamWriterAC12(new MemoryStream(), encoding));
10✔
88
                                case ACadVersion.AC1015:
89
                                        return new DwgmMergedStreamWriterAC14(
138✔
90
                                                        stream,
138✔
91
                                                        new DwgStreamWriterAC15(stream, encoding),
138✔
92
                                                        new DwgStreamWriterAC15(new MemoryStream(), encoding));
138✔
93
                                case ACadVersion.AC1018:
94
                                        return new DwgmMergedStreamWriterAC14(
140✔
95
                                                        stream,
140✔
96
                                                        new DwgStreamWriterAC18(stream, encoding),
140✔
97
                                                        new DwgStreamWriterAC18(new MemoryStream(), encoding));
140✔
98
                                case ACadVersion.AC1021:
99
                                        return new DwgMergedStreamWriter(
3✔
100
                                                        stream,
3✔
101
                                                        new DwgStreamWriterAC21(stream, encoding),
3✔
102
                                                        new DwgStreamWriterAC21(new MemoryStream(), encoding),
3✔
103
                                                        new DwgStreamWriterAC21(new MemoryStream(), encoding));
3✔
104
                                case ACadVersion.AC1024:
105
                                case ACadVersion.AC1027:
106
                                case ACadVersion.AC1032:
107
                                        return new DwgMergedStreamWriter(
630✔
108
                                                        stream,
630✔
109
                                                        new DwgStreamWriterAC24(stream, encoding),
630✔
110
                                                        new DwgStreamWriterAC24(new MemoryStream(), encoding),
630✔
111
                                                        new DwgStreamWriterAC24(new MemoryStream(), encoding));
630✔
112
                                default:
113
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
114
                        }
115
                }
921✔
116

117
                public void WriteInt(int value)
118
                {
30,084✔
119
                        this.Write(value, LittleEndianConverter.Instance);
30,084✔
120
                }
30,084✔
121

122
                public virtual void WriteObjectType(short value)
123
                {
11,736✔
124
                        this.WriteBitShort(value);
11,736✔
125
                }
11,736✔
126

127
                public void WriteObjectType(ObjectType value)
128
                {
19,421✔
129
                        this.WriteObjectType((short)value);
19,421✔
130
                }
19,421✔
131

132
                public void WriteRawLong(long value)
133
                {
34,868✔
134
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes((int)value));
34,868✔
135
                }
34,868✔
136

137
                public override void WriteBytes(byte[] arr)
138
                {
188,221✔
139
                        if (this.BitShift == 0)
188,221✔
140
                        {
55,124✔
141
                                for (int i = 0; i < arr.Length; i++)
1,301,184✔
142
                                {
595,468✔
143
                                        this.Stream.WriteByte(arr[i]);
595,468✔
144
                                }
595,468✔
145
                                return;
55,124✔
146
                        }
147

148
                        int num = 8 - this.BitShift;
133,097✔
149
                        foreach (byte b in arr)
3,666,315✔
150
                        {
1,633,512✔
151
                                this.Stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
1,633,512✔
152
                                this._lastByte = (byte)(b << num);
1,633,512✔
153
                        }
1,633,512✔
154
                }
188,221✔
155

156
                public void WriteBytes(byte[] arr, int initialIndex, int length)
157
                {
39,687✔
158
                        if (this.BitShift == 0)
39,687✔
159
                        {
6,142✔
160
                                for (int i = 0, j = initialIndex; i < length; i++, j++)
280,704✔
161
                                {
87,426✔
162
                                        this._stream.WriteByte(arr[j]);
87,426✔
163
                                }
87,426✔
164

165
                                return;
6,142✔
166
                        }
167

168
                        int num = 8 - this.BitShift;
33,545✔
169
                        for (int i = 0, j = initialIndex; i < length; i++, j++)
3,902,622✔
170
                        {
1,267,329✔
171
                                byte b = arr[j];
1,267,329✔
172
                                this._stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
1,267,329✔
173
                                this._lastByte = (byte)(b << num);
1,267,329✔
174
                        }
1,267,329✔
175
                }
39,687✔
176

177
                public void WriteBitShort(short value)
178
                {
249,785✔
179
                        if (value == 0)
249,785✔
180
                        {
91,951✔
181
                                this.Write2Bits(2);
91,951✔
182
                        }
91,951✔
183
                        else if (value > 0 && value < 256)
157,834✔
184
                        {
104,194✔
185
                                this.Write2Bits(1);
104,194✔
186
                                this.WriteByte((byte)value);
104,194✔
187
                        }
104,194✔
188
                        else if (value == 256)
53,640✔
189
                        {
4,881✔
190
                                this.Write2Bits(3);
4,881✔
191
                        }
4,881✔
192
                        else
193
                        {
48,759✔
194
                                this.Write2Bits(0);
48,759✔
195
                                this.WriteByte((byte)value);
48,759✔
196
                                this.WriteByte((byte)(value >> 8));
48,759✔
197
                        }
48,759✔
198
                }
249,785✔
199

200
                public void WriteBitDouble(double value)
201
                {
133,994✔
202
                        if (value == 0.0)
133,994✔
203
                        {
78,485✔
204
                                this.Write2Bits(2);
78,485✔
205
                                return;
78,485✔
206
                        }
207

208
                        if (value == 1.0)
55,509✔
209
                        {
25,795✔
210
                                this.Write2Bits(1);
25,795✔
211
                                return;
25,795✔
212
                        }
213

214
                        this.Write2Bits(0);
29,714✔
215
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
29,714✔
216
                }
133,994✔
217

218
                public void WriteBitLong(int value)
219
                {
111,889✔
220
                        if (value == 0)
111,889✔
221
                        {
87,052✔
222
                                this.Write2Bits(2);
87,052✔
223
                                return;
87,052✔
224
                        }
225

226
                        if (value > 0 && value < 256)
24,837✔
227
                        {
17,263✔
228
                                this.Write2Bits(1);
17,263✔
229
                                this.WriteByte((byte)value);
17,263✔
230
                                return;
17,263✔
231
                        }
232

233
                        this.Write2Bits(0);
7,574✔
234
                        this.WriteByte((byte)value);
7,574✔
235
                        this.WriteByte((byte)(value >> 8));
7,574✔
236
                        this.WriteByte((byte)(value >> 16));
7,574✔
237
                        this.WriteByte((byte)(value >> 24));
7,574✔
238
                }
111,889✔
239

240
                public void WriteBitLongLong(long value)
241
                {
140✔
242
                        byte size = 0;
140✔
243
                        ulong unsignedValue = (ulong)value;
140✔
244

245
                        ulong hold = unsignedValue;
140✔
246
                        while (hold != 0)
140!
247
                        {
×
248
                                hold >>= 8;
×
249
                                size = (byte)(size + 1);
×
250
                        }
×
251

252
                        this.write3Bits(size);
140✔
253

254
                        hold = unsignedValue;
140✔
255
                        for (int i = 0; i < size; i++)
280!
256
                        {
×
257
                                this.WriteByte((byte)(hold & 0xFF));
×
258
                                hold >>= 8;
×
259
                        }
×
260
                }
140✔
261

262
                public virtual void WriteVariableText(string value)
263
                {
35,005✔
264
                        if (string.IsNullOrEmpty(value))
35,005✔
265
                        {
3,492✔
266
                                this.WriteBitShort(0);
3,492✔
267
                                return;
3,492✔
268
                        }
269

270
                        byte[] bytes = this.Encoding.GetBytes(value);
31,513✔
271
                        this.WriteBitShort((short)bytes.Length);
31,513✔
272
                        this.WriteBytes(bytes);
31,513✔
273
                }
35,005✔
274

275
                public virtual void WriteTextUnicode(string value)
276
                {
828✔
277
                        byte[] bytes = this.Encoding.GetBytes(string.IsNullOrEmpty(value) ? string.Empty : value);
828✔
278
                        this.WriteRawShort((ushort)(bytes.Length + 1));
828✔
279
                        this._stream.Write(bytes, 0, bytes.Length);
828✔
280
                        this._stream.WriteByte(0);
828✔
281
                }
828✔
282

283
                public void Write2Bits(byte value)
284
                {
532,481✔
285
                        if (this.BitShift < 6)
532,481✔
286
                        {
419,697✔
287
                                this._lastByte |= (byte)(value << 6 - this.BitShift);
419,697✔
288
                                this.BitShift += 2;
419,697✔
289
                        }
419,697✔
290
                        else if (this.BitShift == 6)
112,784✔
291
                        {
61,181✔
292
                                this._lastByte |= value;
61,181✔
293
                                this.Stream.WriteByte(this._lastByte);
61,181✔
294
                                this.resetShift();
61,181✔
295
                        }
61,181✔
296
                        else
297
                        {
51,603✔
298
                                this._lastByte |= (byte)(value >> 1);
51,603✔
299
                                this.Stream.WriteByte(this._lastByte);
51,603✔
300
                                this._lastByte = (byte)(value << 7);
51,603✔
301
                                this.BitShift = 1;
51,603✔
302
                        }
51,603✔
303
                }
532,481✔
304

305
                public void WriteBit(bool value)
306
                {
416,640✔
307
                        if (this.BitShift < 7)
416,640✔
308
                        {
329,829✔
309
                                if (value)
329,829✔
310
                                {
42,261✔
311
                                        this._lastByte |= (byte)(1 << 7 - this.BitShift);
42,261✔
312
                                }
42,261✔
313
                                this.BitShift++;
329,829✔
314
                                return;
329,829✔
315
                        }
316

317
                        if (value)
86,811✔
318
                        {
1,599✔
319
                                this._lastByte |= 1;
1,599✔
320
                        }
1,599✔
321

322
                        this.Stream.WriteByte(this._lastByte);
86,811✔
323
                        this.resetShift();
86,811✔
324
                }
416,640✔
325

326
                public void WriteByte(byte value)
327
                {
640,106✔
328
                        if (this.BitShift == 0)
640,106✔
329
                        {
249,711✔
330
                                this.Stream.WriteByte(value);
249,711✔
331
                                return;
249,711✔
332
                        }
333

334
                        int shift = 8 - this.BitShift;
390,395✔
335
                        this.Stream.WriteByte((byte)(this._lastByte | (value >> this.BitShift)));
390,395✔
336
                        this._lastByte = (byte)(value << shift);
390,395✔
337
                }
640,106✔
338

339
                private void resetShift()
340
                {
233,051✔
341
                        this.BitShift = 0;
233,051✔
342
                        this._lastByte = 0;
233,051✔
343
                }
233,051✔
344

345
                public void WriteDateTime(DateTime value)
346
                {
710✔
347
                        CadUtils.DateToJulian(value, out int jdate, out int miliseconds);
710✔
348

349
                        this.WriteBitLong(jdate);
710✔
350
                        this.WriteBitLong(miliseconds);
710✔
351
                }
710✔
352

353
                public void WriteTimeSpan(TimeSpan value)
354
                {
710✔
355
                        this.WriteBitLong(value.Days);
710✔
356
                        this.WriteBitLong(value.Milliseconds);
710✔
357
                }
710✔
358

359
                public void Write8BitJulianDate(DateTime value)
360
                {
1,246✔
361
                        CadUtils.DateToJulian(value, out int jdate, out int miliseconds);
1,246✔
362
                        this.WriteRawLong(jdate);
1,246✔
363
                        this.WriteRawLong(miliseconds);
1,246✔
364
                }
1,246✔
365

366
                public virtual void WriteCmColor(Color value)
367
                {
2,222✔
368
                        //R15 and earlier: BS color index
369
                        short index = 0;
2,222✔
370
                        if (value.IsTrueColor)
2,222✔
371
                        {
105✔
372
                                index = value.GetApproxIndex();
105✔
373
                        }
105✔
374
                        else
375
                        {
2,117✔
376
                                index = value.Index;
2,117✔
377
                        }
2,117✔
378

379
                        this.WriteBitShort(index);
2,222✔
380
                }
2,222✔
381

382
                public virtual void WriteEnColor(Color color, Transparency transparency)
383
                {
×
384
                        this.WriteCmColor(color);
×
385
                }
×
386

387
                public virtual void WriteEnColor(Color color, Transparency transparency, bool isBookColor)
388
                {
1,132✔
389
                        this.WriteCmColor(color);
1,132✔
390
                }
1,132✔
391

392
                public void Write2BitDouble(XY value)
393
                {
731✔
394
                        this.WriteBitDouble(value.X);
731✔
395
                        this.WriteBitDouble(value.Y);
731✔
396
                }
731✔
397

398
                public void Write3BitDouble(XYZ value)
399
                {
20,043✔
400
                        this.WriteBitDouble(value.X);
20,043✔
401
                        this.WriteBitDouble(value.Y);
20,043✔
402
                        this.WriteBitDouble(value.Z);
20,043✔
403
                }
20,043✔
404

405
                public void Write2RawDouble(XY value)
406
                {
5,535✔
407
                        this.WriteRawDouble(value.X);
5,535✔
408
                        this.WriteRawDouble(value.Y);
5,535✔
409
                }
5,535✔
410

411
                public void WriteRawShort(short value)
412
                {
8,314✔
413
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
8,314✔
414
                }
8,314✔
415

416
                public void WriteRawShort(ushort value)
417
                {
1,451✔
418
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
1,451✔
419
                }
1,451✔
420

421
                public void WriteRawDouble(double value)
422
                {
12,599✔
423
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
12,599✔
424
                }
12,599✔
425

426
                public void HandleReference(IHandledCadObject cadObject)
427
                {
28,363✔
428
                        this.HandleReference(DwgReferenceType.Undefined, cadObject);
28,363✔
429
                }
28,363✔
430

431
                public void HandleReference(DwgReferenceType type, IHandledCadObject cadObject)
432
                {
78,662✔
433
                        if (cadObject == null)
78,662✔
434
                        {
19,475✔
435
                                this.HandleReference(type, 0uL);
19,475✔
436
                        }
19,475✔
437
                        else
438
                        {
59,187✔
439
                                this.HandleReference(type, cadObject.Handle);
59,187✔
440
                        }
59,187✔
441
                }
78,662✔
442

443
                public void HandleReference(ulong handle)
444
                {
355✔
445
                        this.HandleReference(DwgReferenceType.Undefined, handle);
355✔
446
                }
355✔
447

448
                public void HandleReference(DwgReferenceType type, ulong handle)
449
                {
122,820✔
450
                        byte b = (byte)((uint)type << 4);
122,820✔
451

452
                        if (handle == 0)
122,820✔
453
                        {
29,040✔
454
                                this.WriteByte(b);
29,040✔
455
                        }
29,040✔
456
                        else if (handle < 0x100)
93,780✔
457
                        {
92,541✔
458
                                this.WriteByte((byte)(b | 1u));
92,541✔
459
                                this.WriteByte((byte)handle);
92,541✔
460
                        }
92,541✔
461
                        else if (handle < 0x10000)
1,239✔
462
                        {
857✔
463
                                this.WriteByte((byte)(b | 2u));
857✔
464
                                this.WriteByte((byte)(handle >> 8));
857✔
465
                                this.WriteByte((byte)handle);
857✔
466
                        }
857✔
467
                        else if (handle < 0x1000000)
382✔
468
                        {
2✔
469
                                this.WriteByte((byte)(b | 3u));
2✔
470
                                this.WriteByte((byte)(handle >> 16));
2✔
471
                                this.WriteByte((byte)(handle >> 8));
2✔
472
                                this.WriteByte((byte)handle);
2✔
473
                        }
2✔
474
                        else if (handle < 0x100000000L)
380!
475
                        {
380✔
476
                                this.WriteByte((byte)(b | 4u));
380✔
477
                                this.WriteByte((byte)(handle >> 24));
380✔
478
                                this.WriteByte((byte)(handle >> 16));
380✔
479
                                this.WriteByte((byte)(handle >> 8));
380✔
480
                                this.WriteByte((byte)handle);
380✔
481
                        }
380✔
482
                        else if (handle < 0x10000000000L)
×
483
                        {
×
484
                                this.WriteByte((byte)(b | 5u));
×
485
                                this.WriteByte((byte)(handle >> 32));
×
486
                                this.WriteByte((byte)(handle >> 24));
×
487
                                this.WriteByte((byte)(handle >> 16));
×
488
                                this.WriteByte((byte)(handle >> 8));
×
489
                                this.WriteByte((byte)handle);
×
490
                        }
×
491
                        else if (handle < 0x1000000000000L)
×
492
                        {
×
493
                                this.WriteByte((byte)(b | 6u));
×
494
                                this.WriteByte((byte)(handle >> 40));
×
495
                                this.WriteByte((byte)(handle >> 32));
×
496
                                this.WriteByte((byte)(handle >> 24));
×
497
                                this.WriteByte((byte)(handle >> 16));
×
498
                                this.WriteByte((byte)(handle >> 8));
×
499
                                this.WriteByte((byte)handle);
×
500
                        }
×
501
                        else if (handle < 0x100000000000000L)
×
502
                        {
×
503
                                this.WriteByte((byte)(b | 7u));
×
504
                                this.WriteByte((byte)(handle >> 48));
×
505
                                this.WriteByte((byte)(handle >> 40));
×
506
                                this.WriteByte((byte)(handle >> 32));
×
507
                                this.WriteByte((byte)(handle >> 24));
×
508
                                this.WriteByte((byte)(handle >> 16));
×
509
                                this.WriteByte((byte)(handle >> 8));
×
510
                                this.WriteByte((byte)handle);
×
511
                        }
×
512
                        else
513
                        {
×
514
                                this.WriteByte((byte)(b | 8u));
×
515
                                this.WriteByte((byte)(handle >> 56));
×
516
                                this.WriteByte((byte)(handle >> 48));
×
517
                                this.WriteByte((byte)(handle >> 40));
×
518
                                this.WriteByte((byte)(handle >> 32));
×
519
                                this.WriteByte((byte)(handle >> 24));
×
520
                                this.WriteByte((byte)(handle >> 16));
×
521
                                this.WriteByte((byte)(handle >> 8));
×
522
                                this.WriteByte((byte)handle);
×
523
                        }
×
524
                }
122,820✔
525

526
                public void WriteSpearShift()
527
                {
107,884✔
528
                        if (this.BitShift > 0)
107,884✔
529
                        {
67,083✔
530
                                for (int i = this.BitShift; i < 8; i++)
712,244✔
531
                                {
289,039✔
532
                                        this.WriteBit(value: false);
289,039✔
533
                                }
289,039✔
534
                        }
67,083✔
535
                }
107,884✔
536

537
                public virtual void WriteBitThickness(double thickness)
538
                {
10✔
539
                        //For R13-R14, this is a BD.
540
                        this.WriteBitDouble(thickness);
10✔
541
                }
10✔
542

543
                public virtual void WriteBitExtrusion(XYZ normal)
544
                {
10✔
545
                        //For R13-R14 this is 3BD.
546
                        this.Write3BitDouble(normal);
10✔
547
                }
10✔
548

549
                public void Write2BitDoubleWithDefault(XY def, XY value)
550
                {
20✔
551
                        this.WriteBitDoubleWithDefault(def.X, value.X);
20✔
552
                        this.WriteBitDoubleWithDefault(def.Y, value.Y);
20✔
553
                }
20✔
554

555
                public void Write3BitDoubleWithDefault(XYZ def, XYZ value)
556
                {
×
557
                        this.WriteBitDoubleWithDefault(def.X, value.X);
×
558
                        this.WriteBitDoubleWithDefault(def.Y, value.Y);
×
559
                        this.WriteBitDoubleWithDefault(def.Z, value.Z);
×
560
                }
×
561

562
                public void WriteBitDoubleWithDefault(double def, double value)
563
                {
677✔
564
                        if (def == value)
677✔
565
                        {
165✔
566
                                //00 No more data present, use the value of the default double.
567
                                this.Write2Bits(0);
165✔
568
                                return;
165✔
569
                        }
570

571
                        byte[] defBytes = LittleEndianConverter.Instance.GetBytes(def);
512✔
572
                        byte[] valueBytes = LittleEndianConverter.Instance.GetBytes(value);
512✔
573

574
                        //Compare the 2 sets of bytes by it's simetry
575
                        int first = 0;
512✔
576
                        int last = 7;
512✔
577
                        while (last >= 0 && defBytes[last] == valueBytes[last])
734✔
578
                        {
222✔
579
                                first++;
222✔
580
                                last--;
222✔
581
                        }
222✔
582

583
                        if (first >= 4)
512✔
584
                        {
15✔
585
                                //01 4 bytes of data are present. The result is the default double, with the 4 data bytes patched in
586
                                //replacing the first 4 bytes of the default double(assuming little endian).
587
                                this.Write2Bits(1);
15✔
588
                                this.WriteBytes(defBytes, 0, 4);
15✔
589
                        }
15✔
590
                        else if (first >= 2)
497!
UNCOV
591
                        {
×
592
                                //10 6 bytes of data are present. The result is the default double, with the first 2 data bytes patched in
593
                                //replacing bytes 5 and 6 of the default double, and the last 4 data bytes patched in replacing the first 4
594
                                //bytes of the default double(assuming little endian).
UNCOV
595
                                this.Write2Bits(2);
×
UNCOV
596
                                this.WriteByte(defBytes[4]);
×
UNCOV
597
                                this.WriteByte(defBytes[5]);
×
UNCOV
598
                                this.WriteByte(defBytes[0]);
×
UNCOV
599
                                this.WriteByte(defBytes[1]);
×
UNCOV
600
                                this.WriteByte(defBytes[2]);
×
UNCOV
601
                                this.WriteByte(defBytes[3]);
×
UNCOV
602
                        }
×
603
                        else
604
                        {
497✔
605
                                //11 A full RD follows.
606
                                this.Write2Bits(3);
497✔
607
                                this.WriteBytes(defBytes);
497✔
608
                        }
497✔
609
                }
677✔
610

611
                public void ResetStream()
612
                {
85,059✔
613
                        this._stream.Position = 0L;
85,059✔
614
                        this.resetShift();
85,059✔
615
                        this._stream.SetLength(0L);
85,059✔
616
                }
85,059✔
617

618
                public void SavePositonForSize()
619
                {
×
620
                        this.WriteRawLong(0);
×
621
                }
×
622

623
                public void SetPositionByFlag(long pos)
624
                {
10,538✔
625
                        if (pos >= 0x8000)
10,538!
626
                        {
×
627
                                if (pos >= 0x40000000)
×
628
                                {
×
629
                                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes((ushort)((pos >> 30) & 0xFFFF)));
×
630
                                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes((ushort)(((pos >> 15) & 0x7FFF) | 0x8000)));
×
631
                                }
×
632
                                else
633
                                {
×
634
                                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes((ushort)((pos >> 15) & 0xFFFF)));
×
635
                                }
×
636

637
                                this.WriteBytes(LittleEndianConverter.Instance.GetBytes((ushort)((pos & 0x7FFF) | 0x8000)));
×
638
                        }
×
639
                        else
640
                        {
10,538✔
641
                                this.WriteBytes(LittleEndianConverter.Instance.GetBytes((ushort)pos));
10,538✔
642
                        }
10,538✔
643
                }
10,538✔
644

645
                public void SetPositionInBits(long posInBits)
646
                {
51,463✔
647
                        long position = posInBits / 8;
51,463✔
648
                        this.BitShift = (int)(posInBits % 8);
51,463✔
649
                        this._stream.Position = position;
51,463✔
650

651
                        if (this.BitShift > 0)
51,463✔
652
                        {
43,234✔
653
                                int value = this._stream.ReadByte();
43,234✔
654
                                if (value < 0)
43,234!
655
                                {
×
656
                                        throw new EndOfStreamException();
×
657
                                }
658
                                this._lastByte = (byte)value;
43,234✔
659
                        }
43,234✔
660
                        else
661
                        {
8,229✔
662
                                this._lastByte = 0;
8,229✔
663
                        }
8,229✔
664

665
                        this._stream.Position = position;
51,463✔
666
                }
51,463✔
667

668
                public void WriteShiftValue()
669
                {
12,154✔
670
                        if (this.BitShift > 0)
12,154✔
671
                        {
11,736✔
672
                                long position = this._stream.Position;
11,736✔
673
                                int lastValue = this._stream.ReadByte();
11,736✔
674
                                byte currValue = (byte)(this._lastByte | ((byte)lastValue & (0b11111111 >> this.BitShift)));
11,736✔
675
                                this._stream.Position = position;
11,736✔
676
                                this._stream.WriteByte(currValue);
11,736✔
677
                        }
11,736✔
678
                }
12,154✔
679

680
                private void write3Bits(byte value)
681
                {
140✔
682
                        this.WriteBit((value & 4) != 0);
140✔
683
                        this.WriteBit((value & 2) != 0);
140✔
684
                        this.WriteBit((value & 1) != 0);
140✔
685
                }
140✔
686
        }
687
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc