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

DomCR / ACadSharp / 17911350546

22 Sep 2025 09:46AM UTC coverage: 78.416% (-0.05%) from 78.47%
17911350546

Pull #191

github

web-flow
Merge 162578dff into 664767d0d
Pull Request #191: Solid3D entity

6700 of 9281 branches covered (72.19%)

Branch coverage included in aggregate %.

25899 of 32291 relevant lines covered (80.21%)

108633.97 hits per line

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

74.73
/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; } }
216✔
16

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

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

21
                public int BitShift { get; private set; } = 0;
5,876,370✔
22

23
                private byte _lastByte;
24

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

30
                public static IDwgStreamWriter GetStreamWriter(ACadVersion version, Stream stream, Encoding encoding)
31
                {
2,036✔
32
                        switch (version)
2,036!
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);
498✔
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,494✔
60
                                default:
61
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
62
                        }
63
                }
2,036✔
64

65
                public static IDwgStreamWriter GetMergedWriter(ACadVersion version, Stream stream, Encoding encoding)
66
                {
714✔
67
                        switch (version)
714!
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(
126✔
95
                                                        stream,
126✔
96
                                                        new DwgStreamWriterAC18(stream, encoding),
126✔
97
                                                        new DwgStreamWriterAC18(new MemoryStream(), encoding));
126✔
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(
567✔
108
                                                        stream,
567✔
109
                                                        new DwgStreamWriterAC24(stream, encoding),
567✔
110
                                                        new DwgStreamWriterAC24(new MemoryStream(), encoding),
567✔
111
                                                        new DwgStreamWriterAC24(new MemoryStream(), encoding));
567✔
112
                                default:
113
                                        throw new NotSupportedException($"Dwg version not supported: {version}");
×
114
                        }
115
                }
714✔
116

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

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

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

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

137
                public override void WriteBytes(byte[] arr)
138
                {
127,284✔
139
                        if (this.BitShift == 0)
127,284✔
140
                        {
39,343✔
141
                                for (int i = 0; i < arr.Length; i++)
959,030✔
142
                                {
440,172✔
143
                                        this.Stream.WriteByte(arr[i]);
440,172✔
144
                                }
440,172✔
145
                                return;
39,343✔
146
                        }
147

148
                        int num = 8 - this.BitShift;
87,941✔
149
                        foreach (byte b in arr)
2,400,457✔
150
                        {
1,068,317✔
151
                                this.Stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
1,068,317✔
152
                                this._lastByte = (byte)(b << num);
1,068,317✔
153
                        }
1,068,317✔
154
                }
127,284✔
155

156
                public void WriteBytes(byte[] arr, int initialIndex, int length)
157
                {
28,167✔
158
                        if (this.BitShift == 0)
28,167✔
159
                        {
3,357✔
160
                                for (int i = 0, j = initialIndex; i < length; i++, j++)
184,509✔
161
                                {
58,146✔
162
                                        this._stream.WriteByte(arr[j]);
58,146✔
163
                                }
58,146✔
164

165
                                return;
3,357✔
166
                        }
167

168
                        int num = 8 - this.BitShift;
24,810✔
169
                        for (int i = 0, j = initialIndex; i < length; i++, j++)
2,870,514✔
170
                        {
932,028✔
171
                                byte b = arr[j];
932,028✔
172
                                this._stream.WriteByte((byte)(this._lastByte | (b >> this.BitShift)));
932,028✔
173
                                this._lastByte = (byte)(b << num);
932,028✔
174
                        }
932,028✔
175
                }
28,167✔
176

177
                public void WriteBitShort(short value)
178
                {
166,558✔
179
                        if (value == 0)
166,558✔
180
                        {
65,594✔
181
                                this.Write2Bits(2);
65,594✔
182
                        }
65,594✔
183
                        else if (value > 0 && value < 256)
100,964✔
184
                        {
67,410✔
185
                                this.Write2Bits(1);
67,410✔
186
                                this.WriteByte((byte)value);
67,410✔
187
                        }
67,410✔
188
                        else if (value == 256)
33,554✔
189
                        {
2,897✔
190
                                this.Write2Bits(3);
2,897✔
191
                        }
2,897✔
192
                        else
193
                        {
30,657✔
194
                                this.Write2Bits(0);
30,657✔
195
                                this.WriteByte((byte)value);
30,657✔
196
                                this.WriteByte((byte)(value >> 8));
30,657✔
197
                        }
30,657✔
198
                }
166,558✔
199

200
                public void WriteBitDouble(double value)
201
                {
98,064✔
202
                        if (value == 0.0)
98,064✔
203
                        {
58,068✔
204
                                this.Write2Bits(2);
58,068✔
205
                                return;
58,068✔
206
                        }
207

208
                        if (value == 1.0)
39,996✔
209
                        {
18,673✔
210
                                this.Write2Bits(1);
18,673✔
211
                                return;
18,673✔
212
                        }
213

214
                        this.Write2Bits(0);
21,323✔
215
                        this.WriteBytes(LittleEndianConverter.Instance.GetBytes(value));
21,323✔
216
                }
98,064✔
217

218
                public void WriteBitLong(int value)
219
                {
83,696✔
220
                        if (value == 0)
83,696✔
221
                        {
65,408✔
222
                                this.Write2Bits(2);
65,408✔
223
                                return;
65,408✔
224
                        }
225

226
                        if (value > 0 && value < 256)
18,288✔
227
                        {
12,281✔
228
                                this.Write2Bits(1);
12,281✔
229
                                this.WriteByte((byte)value);
12,281✔
230
                                return;
12,281✔
231
                        }
232

233
                        this.Write2Bits(0);
6,007✔
234
                        this.WriteByte((byte)value);
6,007✔
235
                        this.WriteByte((byte)(value >> 8));
6,007✔
236
                        this.WriteByte((byte)(value >> 16));
6,007✔
237
                        this.WriteByte((byte)(value >> 24));
6,007✔
238
                }
83,696✔
239

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

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

252
                        this.write3Bits(size);
126✔
253

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

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

270
                        byte[] bytes = this.Encoding.GetBytes(value);
13,677✔
271
                        this.WriteBitShort((short)bytes.Length);
13,677✔
272
                        this.WriteBytes(bytes);
13,677✔
273
                }
15,454✔
274

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

283
                public void Write2Bits(byte value)
284
                {
375,600✔
285
                        if (this.BitShift < 6)
375,600✔
286
                        {
297,365✔
287
                                this._lastByte |= (byte)(value << 6 - this.BitShift);
297,365✔
288
                                this.BitShift += 2;
297,365✔
289
                        }
297,365✔
290
                        else if (this.BitShift == 6)
78,235✔
291
                        {
38,500✔
292
                                this._lastByte |= value;
38,500✔
293
                                this.Stream.WriteByte(this._lastByte);
38,500✔
294
                                this.resetShift();
38,500✔
295
                        }
38,500✔
296
                        else
297
                        {
39,735✔
298
                                this._lastByte |= (byte)(value >> 1);
39,735✔
299
                                this.Stream.WriteByte(this._lastByte);
39,735✔
300
                                this._lastByte = (byte)(value << 7);
39,735✔
301
                                this.BitShift = 1;
39,735✔
302
                        }
39,735✔
303
                }
375,600✔
304

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

317
                        if (value)
65,346✔
318
                        {
1,438✔
319
                                this._lastByte |= 1;
1,438✔
320
                        }
1,438✔
321

322
                        this.Stream.WriteByte(this._lastByte);
65,346✔
323
                        this.resetShift();
65,346✔
324
                }
311,751✔
325

326
                public void WriteByte(byte value)
327
                {
409,617✔
328
                        if (this.BitShift == 0)
409,617✔
329
                        {
134,303✔
330
                                this.Stream.WriteByte(value);
134,303✔
331
                                return;
134,303✔
332
                        }
333

334
                        int shift = 8 - this.BitShift;
275,314✔
335
                        this.Stream.WriteByte((byte)(this._lastByte | (value >> this.BitShift)));
275,314✔
336
                        this._lastByte = (byte)(value << shift);
275,314✔
337
                }
409,617✔
338

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

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

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

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

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

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

379
                        this.WriteBitShort(index);
232✔
380
                }
232✔
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
                {
544✔
394
                        this.WriteBitDouble(value.X);
544✔
395
                        this.WriteBitDouble(value.Y);
544✔
396
                }
544✔
397

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

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

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

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

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

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

431
                public void HandleReference(DwgReferenceType type, IHandledCadObject cadObject)
432
                {
51,450✔
433
                        if (cadObject == null)
51,450✔
434
                        {
11,440✔
435
                                this.HandleReference(type, 0uL);
11,440✔
436
                        }
11,440✔
437
                        else
438
                        {
40,010✔
439
                                this.HandleReference(type, cadObject.Handle);
40,010✔
440
                        }
40,010✔
441
                }
51,450✔
442

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

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

452
                        if (handle == 0)
81,567✔
453
                        {
18,533✔
454
                                this.WriteByte(b);
18,533✔
455
                        }
18,533✔
456
                        else if (handle < 0x100)
63,034✔
457
                        {
62,132✔
458
                                this.WriteByte((byte)(b | 1u));
62,132✔
459
                                this.WriteByte((byte)handle);
62,132✔
460
                        }
62,132✔
461
                        else if (handle < 0x10000)
902✔
462
                        {
524✔
463
                                this.WriteByte((byte)(b | 2u));
524✔
464
                                this.WriteByte((byte)(handle >> 8));
524✔
465
                                this.WriteByte((byte)handle);
524✔
466
                        }
524✔
467
                        else if (handle < 0x1000000)
378✔
468
                        {
5✔
469
                                this.WriteByte((byte)(b | 3u));
5✔
470
                                this.WriteByte((byte)(handle >> 16));
5✔
471
                                this.WriteByte((byte)(handle >> 8));
5✔
472
                                this.WriteByte((byte)handle);
5✔
473
                        }
5✔
474
                        else if (handle < 0x100000000L)
373!
475
                        {
373✔
476
                                this.WriteByte((byte)(b | 4u));
373✔
477
                                this.WriteByte((byte)(handle >> 24));
373✔
478
                                this.WriteByte((byte)(handle >> 16));
373✔
479
                                this.WriteByte((byte)(handle >> 8));
373✔
480
                                this.WriteByte((byte)handle);
373✔
481
                        }
373✔
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
                }
81,567✔
525

526
                public void WriteSpearShift()
527
                {
75,906✔
528
                        if (this.BitShift > 0)
75,906✔
529
                        {
49,710✔
530
                                for (int i = this.BitShift; i < 8; i++)
530,206✔
531
                                {
215,393✔
532
                                        this.WriteBit(value: false);
215,393✔
533
                                }
215,393✔
534
                        }
49,710✔
535
                }
75,906✔
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
                {
16✔
551
                        this.WriteBitDoubleWithDefault(def.X, value.X);
16✔
552
                        this.WriteBitDoubleWithDefault(def.Y, value.Y);
16✔
553
                }
16✔
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
                {
481✔
564
                        if (def == value)
481✔
565
                        {
108✔
566
                                //00 No more data present, use the value of the default double.
567
                                this.Write2Bits(0);
108✔
568
                                return;
108✔
569
                        }
570

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

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

583
                        if (first >= 4)
373✔
584
                        {
12✔
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);
12✔
588
                                this.WriteBytes(defBytes, 0, 4);
12✔
589
                        }
12✔
590
                        else if (first >= 2)
361!
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
                        {
361✔
605
                                //11 A full RD follows.
606
                                this.Write2Bits(3);
361✔
607
                                this.WriteBytes(defBytes);
361✔
608
                        }
361✔
609
                }
481✔
610

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

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

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

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

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

665
                        this._stream.Position = position;
33,939✔
666
                }
33,939✔
667

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

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

© 2025 Coveralls, Inc