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

DomCR / ACadSharp / 13540185526

26 Feb 2025 09:02AM UTC coverage: 76.085% (-0.006%) from 76.091%
13540185526

Pull #518

github

web-flow
Merge 73c554c71 into 69e89301c
Pull Request #518: Imrpove CI

5460 of 7879 branches covered (69.3%)

Branch coverage included in aggregate %.

21541 of 27609 relevant lines covered (78.02%)

75868.5 hits per line

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

71.61
/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; } }
42✔
16

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

19
                public long SavedPositionInBits { get; } = 0;
380✔
20

21
                public int BitShift { get; private set; } = 0;
590,354✔
22

23
                private byte _lastByte;
24

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

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

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

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

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

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

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

137
                public override void WriteBytes(byte[] arr)
138
                {
15,242✔
139
                        if (this.BitShift == 0)
15,242✔
140
                        {
3,834✔
141
                                for (int i = 0; i < arr.Length; i++)
81,856✔
142
                                {
37,094✔
143
                                        this.Stream.WriteByte(arr[i]);
37,094✔
144
                                }
37,094✔
145
                                return;
3,834✔
146
                        }
147

148
                        int num = 8 - this.BitShift;
11,408✔
149
                        foreach (byte b in arr)
247,712✔
150
                        {
106,744✔
151
                                this.Stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
106,744✔
152
                                this._lastByte = (byte)(b << num);
106,744✔
153
                        }
106,744✔
154
                }
15,242✔
155

156
                public void WriteBytes(byte[] arr, int initialIndex, int length)
157
                {
3,198✔
158
                        if (this.BitShift == 0)
3,198✔
159
                        {
470✔
160
                                for (int i = 0, j = initialIndex; i < length; i++, j++)
19,176✔
161
                                {
5,922✔
162
                                        this._stream.WriteByte(arr[j]);
5,922✔
163
                                }
5,922✔
164

165
                                return;
470✔
166
                        }
167

168
                        int num = 8 - this.BitShift;
2,728✔
169
                        for (int i = 0, j = initialIndex; i < length; i++, j++)
204,087✔
170
                        {
65,301✔
171
                                byte b = arr[j];
65,301✔
172
                                this._stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
65,301✔
173
                                this._lastByte = (byte)(b << num);
65,301✔
174
                        }
65,301✔
175
                }
3,198✔
176

177
                public void WriteBitShort(short value)
178
                {
18,436✔
179
                        if (value == 0)
18,436✔
180
                        {
7,205✔
181
                                this.Write2Bits(2);
7,205✔
182
                        }
7,205✔
183
                        else if (value > 0 && value < 256)
11,231✔
184
                        {
8,350✔
185
                                this.Write2Bits(1);
8,350✔
186
                                this.WriteByte((byte)value);
8,350✔
187
                        }
8,350✔
188
                        else if (value == 256)
2,881✔
189
                        {
189✔
190
                                this.Write2Bits(3);
189✔
191
                        }
189✔
192
                        else
193
                        {
2,692✔
194
                                this.Write2Bits(0);
2,692✔
195
                                this.WriteByte((byte)value);
2,692✔
196
                                this.WriteByte((byte)(value >> 8));
2,692✔
197
                        }
2,692✔
198
                }
18,436✔
199

200
                public void WriteBitDouble(double value)
201
                {
11,913✔
202
                        if (value == 0.0)
11,913✔
203
                        {
6,503✔
204
                                this.Write2Bits(2);
6,503✔
205
                                return;
6,503✔
206
                        }
207

208
                        if (value == 1.0)
5,410✔
209
                        {
1,910✔
210
                                this.Write2Bits(1);
1,910✔
211
                                return;
1,910✔
212
                        }
213

214
                        this.Write2Bits(0);
3,500✔
215
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
3,500✔
216
                }
11,913✔
217

218
                public void WriteBitLong(int value)
219
                {
5,862✔
220
                        if (value == 0)
5,862✔
221
                        {
4,888✔
222
                                this.Write2Bits(2);
4,888✔
223
                                return;
4,888✔
224
                        }
225

226
                        if (value > 0 && value < 256)
974✔
227
                        {
465✔
228
                                this.Write2Bits(1);
465✔
229
                                this.WriteByte((byte)value);
465✔
230
                                return;
465✔
231
                        }
232

233
                        this.Write2Bits(0);
509✔
234
                        this.WriteByte((byte)value);
509✔
235
                        this.WriteByte((byte)(value >> 8));
509✔
236
                        this.WriteByte((byte)(value >> 16));
509✔
237
                        this.WriteByte((byte)(value >> 24));
509✔
238
                }
5,862✔
239

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

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

252
                        this.write3Bits(size);
10✔
253

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

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

270
                        byte[] bytes = this.Encoding.GetBytes(value);
2,296✔
271
                        this.WriteBitShort((short)bytes.Length);
2,296✔
272
                        this.WriteBytes(bytes);
2,296✔
273
                }
2,697✔
274

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

283
                public void Write2Bits(byte value)
284
                {
38,277✔
285
                        if (this.BitShift < 6)
38,277✔
286
                        {
30,330✔
287
                                this._lastByte |= (byte)(value << 6 - this.BitShift);
30,330✔
288
                                this.BitShift += 2;
30,330✔
289
                        }
30,330✔
290
                        else if (this.BitShift == 6)
7,947✔
291
                        {
4,428✔
292
                                this._lastByte |= value;
4,428✔
293
                                this.Stream.WriteByte(this._lastByte);
4,428✔
294
                                this.resetShift();
4,428✔
295
                        }
4,428✔
296
                        else
297
                        {
3,519✔
298
                                this._lastByte |= (byte)(value >> 1);
3,519✔
299
                                this.Stream.WriteByte(this._lastByte);
3,519✔
300
                                this._lastByte = (byte)(value << 7);
3,519✔
301
                                this.BitShift = 1;
3,519✔
302
                        }
3,519✔
303
                }
38,277✔
304

305
                public void WriteBit(bool value)
306
                {
32,287✔
307
                        if (this.BitShift < 7)
32,287✔
308
                        {
25,409✔
309
                                if (value)
25,409✔
310
                                {
2,945✔
311
                                        this._lastByte |= (byte)(1 << 7 - this.BitShift);
2,945✔
312
                                }
2,945✔
313
                                this.BitShift++;
25,409✔
314
                                return;
25,409✔
315
                        }
316

317
                        if (value)
6,878✔
318
                        {
106✔
319
                                this._lastByte |= 1;
106✔
320
                        }
106✔
321

322
                        this.Stream.WriteByte(this._lastByte);
6,878✔
323
                        this.resetShift();
6,878✔
324
                }
32,287✔
325

326
                public void WriteByte(byte value)
327
                {
52,592✔
328
                        if (this.BitShift == 0)
52,592✔
329
                        {
24,763✔
330
                                this.Stream.WriteByte(value);
24,763✔
331
                                return;
24,763✔
332
                        }
333

334
                        int shift = 8 - this.BitShift;
27,829✔
335
                        this.Stream.WriteByte((byte)(this._lastByte | (value >> this.BitShift)));
27,829✔
336
                        this._lastByte = (byte)(value << shift);
27,829✔
337
                }
52,592✔
338

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

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

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

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

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

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

379
                        this.WriteBitShort(index);
196✔
380
                }
196✔
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
                {
100✔
389
                        this.WriteCmColor(color);
100✔
390
                }
100✔
391

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

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

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

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

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

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

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

431
                public void HandleReference(DwgReferenceType type, IHandledCadObject cadObject)
432
                {
6,398✔
433
                        if (cadObject == null)
6,398✔
434
                        {
1,938✔
435
                                this.HandleReference(type, 0uL);
1,938✔
436
                        }
1,938✔
437
                        else
438
                        {
4,460✔
439
                                this.HandleReference(type, cadObject.Handle);
4,460✔
440
                        }
4,460✔
441
                }
6,398✔
442

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

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

452
                        if (handle == 0)
10,435✔
453
                        {
2,957✔
454
                                this.WriteByte(b);
2,957✔
455
                        }
2,957✔
456
                        else if (handle < 0x100)
7,478✔
457
                        {
7,106✔
458
                                this.WriteByte((byte)(b | 1u));
7,106✔
459
                                this.WriteByte((byte)handle);
7,106✔
460
                        }
7,106✔
461
                        else if (handle < 0x10000)
372!
462
                        {
×
463
                                this.WriteByte((byte)(b | 2u));
×
464
                                this.WriteByte((byte)(handle >> 8));
×
465
                                this.WriteByte((byte)handle);
×
466
                        }
×
467
                        else if (handle < 0x1000000)
372✔
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)
364!
475
                        {
364✔
476
                                this.WriteByte((byte)(b | 4u));
364✔
477
                                this.WriteByte((byte)(handle >> 24));
364✔
478
                                this.WriteByte((byte)(handle >> 16));
364✔
479
                                this.WriteByte((byte)(handle >> 8));
364✔
480
                                this.WriteByte((byte)handle);
364✔
481
                        }
364✔
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
                }
10,435✔
525

526
                public void WriteSpearShift()
527
                {
8,796✔
528
                        if (this.BitShift > 0)
8,796✔
529
                        {
5,505✔
530
                                for (int i = this.BitShift; i < 8; i++)
57,290✔
531
                                {
23,140✔
532
                                        this.WriteBit(value: false);
23,140✔
533
                                }
23,140✔
534
                        }
5,505✔
535
                }
8,796✔
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
                {
×
551
                        this.WriteBitDoubleWithDefault(def.X, value.X);
×
552
                        this.WriteBitDoubleWithDefault(def.Y, value.Y);
×
553
                }
×
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
                {
57✔
564
                        if (def == value)
57!
565
                        {
×
566
                                //00 No more data present, use the value of the default double.
567
                                this.Write2Bits(0);
×
568
                                return;
×
569
                        }
570

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

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

583
                        if (first >= 4)
57!
584
                        {
×
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);
×
588
                                this.WriteBytes(defBytes, 0, 4);
×
589
                        }
×
590
                        else if (first >= 2)
57!
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).
595
                                this.Write2Bits(2);
×
596
                                this.WriteByte(defBytes[4]);
×
597
                                this.WriteByte(defBytes[5]);
×
598
                                this.WriteByte(defBytes[0]);
×
599
                                this.WriteByte(defBytes[1]);
×
600
                                this.WriteByte(defBytes[2]);
×
601
                                this.WriteByte(defBytes[3]);
×
602
                        }
×
603
                        else
604
                        {
57✔
605
                                //11 A full RD follows.
606
                                this.Write2Bits(3);
57✔
607
                                this.WriteBytes(defBytes);
57✔
608
                        }
57✔
609
                }
57✔
610

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

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

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

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

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

665
                        this._stream.Position = position;
4,467✔
666
                }
4,467✔
667

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

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