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

DomCR / ACadSharp / 22351644713

24 Feb 2026 12:52PM UTC coverage: 77.295% (-0.02%) from 77.315%
22351644713

push

github

web-flow
Merge pull request #985 from DomCR/issue-983_currententitycolorbylayer-fix

issue-983

8179 of 11464 branches covered (71.35%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

9 existing lines in 1 file now uncovered.

29564 of 37366 relevant lines covered (79.12%)

151250.35 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; } }
420✔
16

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

19
                public long SavedPositionInBits { get; } = 0;
4,979✔
20

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

23
                private byte _lastByte;
24

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

30
                public static IDwgStreamWriter GetStreamWriter(ACadVersion version, Stream stream, Encoding encoding)
31
                {
2,574✔
32
                        switch (version)
2,574!
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);
398✔
52
                                case ACadVersion.AC1018:
53
                                        return new DwgStreamWriterAC18(stream, encoding);
538✔
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,614✔
60
                                default:
61
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
62
                        }
63
                }
2,574✔
64

65
                public static IDwgStreamWriter GetMergedWriter(ACadVersion version, Stream stream, Encoding encoding)
66
                {
895✔
67
                        switch (version)
895!
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(
134✔
90
                                                        stream,
134✔
91
                                                        new DwgStreamWriterAC15(stream, encoding),
134✔
92
                                                        new DwgStreamWriterAC15(new MemoryStream(), encoding));
134✔
93
                                case ACadVersion.AC1018:
94
                                        return new DwgmMergedStreamWriterAC14(
136✔
95
                                                        stream,
136✔
96
                                                        new DwgStreamWriterAC18(stream, encoding),
136✔
97
                                                        new DwgStreamWriterAC18(new MemoryStream(), encoding));
136✔
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(
612✔
108
                                                        stream,
612✔
109
                                                        new DwgStreamWriterAC24(stream, encoding),
612✔
110
                                                        new DwgStreamWriterAC24(new MemoryStream(), encoding),
612✔
111
                                                        new DwgStreamWriterAC24(new MemoryStream(), encoding));
612✔
112
                                default:
113
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
114
                        }
115
                }
895✔
116

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

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

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

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

137
                public override void WriteBytes(byte[] arr)
138
                {
183,281✔
139
                        if (this.BitShift == 0)
183,281✔
140
                        {
53,602✔
141
                                for (int i = 0; i < arr.Length; i++)
1,264,832✔
142
                                {
578,814✔
143
                                        this.Stream.WriteByte(arr[i]);
578,814✔
144
                                }
578,814✔
145
                                return;
53,602✔
146
                        }
147

148
                        int num = 8 - this.BitShift;
129,679✔
149
                        foreach (byte b in arr)
3,555,729✔
150
                        {
1,583,346✔
151
                                this.Stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
1,583,346✔
152
                                this._lastByte = (byte)(b << num);
1,583,346✔
153
                        }
1,583,346✔
154
                }
183,281✔
155

156
                public void WriteBytes(byte[] arr, int initialIndex, int length)
157
                {
38,657✔
158
                        if (this.BitShift == 0)
38,657✔
159
                        {
5,984✔
160
                                for (int i = 0, j = initialIndex; i < length; i++, j++)
268,221✔
161
                                {
83,423✔
162
                                        this._stream.WriteByte(arr[j]);
83,423✔
163
                                }
83,423✔
164

165
                                return;
5,984✔
166
                        }
167

168
                        int num = 8 - this.BitShift;
32,673✔
169
                        for (int i = 0, j = initialIndex; i < length; i++, j++)
3,783,603✔
170
                        {
1,228,528✔
171
                                byte b = arr[j];
1,228,528✔
172
                                this._stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
1,228,528✔
173
                                this._lastByte = (byte)(b << num);
1,228,528✔
174
                        }
1,228,528✔
175
                }
38,657✔
176

177
                public void WriteBitShort(short value)
178
                {
242,992✔
179
                        if (value == 0)
242,992✔
180
                        {
89,529✔
181
                                this.Write2Bits(2);
89,529✔
182
                        }
89,529✔
183
                        else if (value > 0 && value < 256)
153,463✔
184
                        {
101,295✔
185
                                this.Write2Bits(1);
101,295✔
186
                                this.WriteByte((byte)value);
101,295✔
187
                        }
101,295✔
188
                        else if (value == 256)
52,168✔
189
                        {
4,798✔
190
                                this.Write2Bits(3);
4,798✔
191
                        }
4,798✔
192
                        else
193
                        {
47,370✔
194
                                this.Write2Bits(0);
47,370✔
195
                                this.WriteByte((byte)value);
47,370✔
196
                                this.WriteByte((byte)(value >> 8));
47,370✔
197
                        }
47,370✔
198
                }
242,992✔
199

200
                public void WriteBitDouble(double value)
201
                {
130,550✔
202
                        if (value == 0.0)
130,550✔
203
                        {
76,307✔
204
                                this.Write2Bits(2);
76,307✔
205
                                return;
76,307✔
206
                        }
207

208
                        if (value == 1.0)
54,243✔
209
                        {
25,157✔
210
                                this.Write2Bits(1);
25,157✔
211
                                return;
25,157✔
212
                        }
213

214
                        this.Write2Bits(0);
29,086✔
215
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
29,086✔
216
                }
130,550✔
217

218
                public void WriteBitLong(int value)
219
                {
108,787✔
220
                        if (value == 0)
108,787✔
221
                        {
84,641✔
222
                                this.Write2Bits(2);
84,641✔
223
                                return;
84,641✔
224
                        }
225

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

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

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

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

252
                        this.write3Bits(size);
136✔
253

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

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

270
                        byte[] bytes = this.Encoding.GetBytes(value);
30,635✔
271
                        this.WriteBitShort((short)bytes.Length);
30,635✔
272
                        this.WriteBytes(bytes);
30,635✔
273
                }
34,037✔
274

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

283
                public void Write2Bits(byte value)
284
                {
518,407✔
285
                        if (this.BitShift < 6)
518,407✔
286
                        {
408,598✔
287
                                this._lastByte |= (byte)(value << 6 - this.BitShift);
408,598✔
288
                                this.BitShift += 2;
408,598✔
289
                        }
408,598✔
290
                        else if (this.BitShift == 6)
109,809✔
291
                        {
59,549✔
292
                                this._lastByte |= value;
59,549✔
293
                                this.Stream.WriteByte(this._lastByte);
59,549✔
294
                                this.resetShift();
59,549✔
295
                        }
59,549✔
296
                        else
297
                        {
50,260✔
298
                                this._lastByte |= (byte)(value >> 1);
50,260✔
299
                                this.Stream.WriteByte(this._lastByte);
50,260✔
300
                                this._lastByte = (byte)(value << 7);
50,260✔
301
                                this.BitShift = 1;
50,260✔
302
                        }
50,260✔
303
                }
518,407✔
304

305
                public void WriteBit(bool value)
306
                {
405,834✔
307
                        if (this.BitShift < 7)
405,834✔
308
                        {
321,285✔
309
                                if (value)
321,285✔
310
                                {
41,201✔
311
                                        this._lastByte |= (byte)(1 << 7 - this.BitShift);
41,201✔
312
                                }
41,201✔
313
                                this.BitShift++;
321,285✔
314
                                return;
321,285✔
315
                        }
316

317
                        if (value)
84,549✔
318
                        {
1,560✔
319
                                this._lastByte |= 1;
1,560✔
320
                        }
1,560✔
321

322
                        this.Stream.WriteByte(this._lastByte);
84,549✔
323
                        this.resetShift();
84,549✔
324
                }
405,834✔
325

326
                public void WriteByte(byte value)
327
                {
623,247✔
328
                        if (this.BitShift == 0)
623,247✔
329
                        {
243,232✔
330
                                this.Stream.WriteByte(value);
243,232✔
331
                                return;
243,232✔
332
                        }
333

334
                        int shift = 8 - this.BitShift;
380,015✔
335
                        this.Stream.WriteByte((byte)(this._lastByte | (value >> this.BitShift)));
380,015✔
336
                        this._lastByte = (byte)(value << shift);
380,015✔
337
                }
623,247✔
338

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

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

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

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

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

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

379
                        this.WriteBitShort(index);
2,175✔
380
                }
2,175✔
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,115✔
389
                        this.WriteCmColor(color);
1,115✔
390
                }
1,115✔
391

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

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

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

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

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

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

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

431
                public void HandleReference(DwgReferenceType type, IHandledCadObject cadObject)
432
                {
76,737✔
433
                        if (cadObject == null)
76,737✔
434
                        {
18,951✔
435
                                this.HandleReference(type, 0uL);
18,951✔
436
                        }
18,951✔
437
                        else
438
                        {
57,786✔
439
                                this.HandleReference(type, cadObject.Handle);
57,786✔
440
                        }
57,786✔
441
                }
76,737✔
442

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

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

452
                        if (handle == 0)
119,661✔
453
                        {
28,242✔
454
                                this.WriteByte(b);
28,242✔
455
                        }
28,242✔
456
                        else if (handle < 0x100)
91,419✔
457
                        {
90,180✔
458
                                this.WriteByte((byte)(b | 1u));
90,180✔
459
                                this.WriteByte((byte)handle);
90,180✔
460
                        }
90,180✔
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
                        {
8✔
469
                                this.WriteByte((byte)(b | 3u));
8✔
470
                                this.WriteByte((byte)(handle >> 16));
8✔
471
                                this.WriteByte((byte)(handle >> 8));
8✔
472
                                this.WriteByte((byte)handle);
8✔
473
                        }
8✔
474
                        else if (handle < 0x100000000L)
374!
475
                        {
374✔
476
                                this.WriteByte((byte)(b | 4u));
374✔
477
                                this.WriteByte((byte)(handle >> 24));
374✔
478
                                this.WriteByte((byte)(handle >> 16));
374✔
479
                                this.WriteByte((byte)(handle >> 8));
374✔
480
                                this.WriteByte((byte)handle);
374✔
481
                        }
374✔
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
                }
119,661✔
525

526
                public void WriteSpearShift()
527
                {
105,101✔
528
                        if (this.BitShift > 0)
105,101✔
529
                        {
65,328✔
530
                                for (int i = this.BitShift; i < 8; i++)
693,468✔
531
                                {
281,406✔
532
                                        this.WriteBit(value: false);
281,406✔
533
                                }
281,406✔
534
                        }
65,328✔
535
                }
105,101✔
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
                {
82,908✔
613
                        this._stream.Position = 0L;
82,908✔
614
                        this.resetShift();
82,908✔
615
                        this._stream.SetLength(0L);
82,908✔
616
                }
82,908✔
617

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

623
                public void SetPositionByFlag(long pos)
624
                {
10,247✔
625
                        if (pos >= 0x8000)
10,247!
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,247✔
641
                                this.WriteBytes(LittleEndianConverter.Instance.GetBytes((ushort)pos));
10,247✔
642
                        }
10,247✔
643
                }
10,247✔
644

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

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

665
                        this._stream.Position = position;
50,143✔
666
                }
50,143✔
667

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

680
                private void write3Bits(byte value)
681
                {
136✔
682
                        this.WriteBit((value & 4) != 0);
136✔
683
                        this.WriteBit((value & 2) != 0);
136✔
684
                        this.WriteBit((value & 1) != 0);
136✔
685
                }
136✔
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