This repository has been archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBitStream.cs
1213 lines (1113 loc) · 38 KB
/
BitStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace BitStreams
{
/// <summary>
/// Stream wrapper to use bit-level operations
/// </summary>
public class BitStream
{
private long offset { get; set; }
private int bit { get; set; }
private bool MSB { get; set; }
private Stream stream;
private Encoding encoding;
/// <summary>
/// Allows the <see cref="BitStream"/> auto increase in size when needed
/// </summary>
public bool AutoIncreaseStream { get; set; }
/// <summary>
/// Get the stream length
/// </summary>
public long Length
{
get
{
return stream.Length;
}
}
/// <summary>
/// Get the current bit position in the stream
/// </summary>
public long BitPosition
{
get
{
return bit;
}
}
/// <summary>
/// Check if <see cref="BitStream"/> offset is inside the stream length
/// </summary>
private bool ValidPosition
{
get
{
return offset < Length;
}
}
#region Constructors
/// <summary>
/// Creates a <see cref="BitStream"/> using a Stream
/// </summary>
/// <param name="stream">Stream to use</param>
/// <param name="MSB">true if Most Significant Bit will be used, if false LSB will be used</param>
public BitStream(Stream stream, bool MSB = false)
{
this.stream = new MemoryStream();
stream.CopyTo(this.stream);
this.MSB = MSB;
offset = 0;
bit = 0;
encoding = Encoding.UTF8;
AutoIncreaseStream = false;
}
/// <summary>
/// Creates a <see cref="BitStream"/> using a Stream
/// </summary>
/// <param name="stream">Stream to use</param>
/// <param name="encoding">Encoding to use with chars</param>
/// <param name="MSB">true if Most Significant Bit will be used, if false LSB will be used</param>
public BitStream(Stream stream, Encoding encoding, bool MSB = false)
{
this.stream = new MemoryStream();
stream.CopyTo(this.stream);
this.MSB = MSB;
offset = 0;
bit = 0;
this.encoding = encoding;
AutoIncreaseStream = false;
}
/// <summary>
/// Creates a <see cref="BitStream"/> using a byte[]
/// </summary>
/// <param name="buffer">byte[] to use</param>
/// <param name="MSB">true if Most Significant Bit will be used, if false LSB will be used</param>
public BitStream(byte[] buffer, bool MSB = false)
{
this.stream = new MemoryStream();
MemoryStream m = new MemoryStream(buffer);
m.CopyTo(this.stream);
this.MSB = MSB;
offset = 0;
bit = 0;
encoding = Encoding.UTF8;
AutoIncreaseStream = false;
}
/// <summary>
/// Creates a <see cref="BitStream"/> using a byte[]
/// </summary>
/// <param name="buffer">byte[] to use</param>
/// <param name="encoding">Encoding to use with chars</param>
/// <param name="MSB">true if Most Significant Bit will be used, if false LSB will be used</param>
public BitStream(byte[] buffer, Encoding encoding, bool MSB = false)
{
this.stream = new MemoryStream();
MemoryStream m = new MemoryStream(buffer);
m.CopyTo(this.stream);
this.MSB = MSB;
offset = 0;
bit = 0;
this.encoding = encoding;
AutoIncreaseStream = false;
}
/// <summary>
/// Creates a <see cref="BitStream"/> using a byte[]
/// </summary>
/// <param name="buffer">byte[] to use</param>
/// <param name="MSB">true if Most Significant Bit will be used, if false LSB will be used</param>
public static BitStream Create(byte[] buffer, bool MSB = false)
{
return new BitStream(buffer, MSB);
}
/// <summary>
/// Creates a <see cref="BitStream"/> using a byte[]
/// </summary>
/// <param name="buffer">byte[] to use</param>
/// <param name="encoding">Encoding to use with chars/param>
/// <param name="MSB">true if Most Significant Bit will be used, if false LSB will be used</param>
public static BitStream Create(byte[] buffer, Encoding encoding, bool MSB = false)
{
return new BitStream(buffer, encoding, MSB);
}
/// <summary>
/// Creates a <see cref="BitStream"/> using a file path, throws IOException if file doesn't exists or path is not a file
/// </summary>
/// <param name="path">File path</param>
/// <param name="encoding">Encoding of the file, if null default <see cref="Encoding"/> will be used</param>
/// <returns></returns>
public static BitStream CreateFromFile(string path, Encoding encoding = null)
{
if (!File.Exists(path))
{
throw new IOException("File doesn't exists!");
}
if(File.GetAttributes(path) == FileAttributes.Directory)
{
throw new IOException("Path is a directory!");
}
if (encoding == null)
{
encoding = Encoding.UTF8;
}
return new BitStream(File.ReadAllBytes(path), encoding);
}
#endregion
#region Methods
/// <summary>
/// Seek to the specified offset and check if it is a valid position for reading in the stream
/// </summary>
/// <param name="offset">offset on the stream</param>
/// <param name="bit">bit position</param>
/// <returns>true if offset is valid to do reading, false otherwise</returns>
public bool this[long offset, int bit]
{
get
{
Seek(offset, bit);
return ValidPosition;
}
//set {
// Seek(offset, bit);
//}
private set { }
}
/// <summary>
/// Seek through the stream selecting the offset and bit using <see cref="SeekOrigin.Begin"/>
/// </summary>
/// <param name="offset">offset on the stream</param>
/// <param name="bit">bit position</param>
public void Seek(long offset, int bit)
{
if (offset > Length)
{
this.offset = Length;
}
else
{
if (offset >= 0)
{
this.offset = offset;
}
else
{
offset = 0;
}
}
if (bit >= 8)
{
int n = (int)(bit / 8);
this.offset += n;
this.bit = bit % 8;
}
else
{
this.bit = bit;
}
stream.Seek(offset, SeekOrigin.Begin);
}
/// <summary>
/// Advances the stream by one bit
/// </summary>
public void AdvanceBit()
{
bit = (bit + 1) % 8;
if (bit == 0)
{
offset++;
}
}
/// <summary>
/// Returns the stream by one bit
/// </summary>
public void ReturnBit()
{
bit = ((bit - 1) == -1 ? 7 : bit - 1);
if (bit == 7)
{
offset--;
}
if(offset < 0)
{
offset = 0;
}
}
/// <summary>
/// Get the edited stream
/// </summary>
/// <returns>Modified stream</returns>
public Stream GetStream()
{
return stream;
}
/// <summary>
/// Get the stream data as a byte[]
/// </summary>
/// <returns>Stream as byte[]</returns>
public byte[] GetStreamData()
{
stream.Seek(0, SeekOrigin.Begin);
MemoryStream s = new MemoryStream();
stream.CopyTo(s);
Seek(offset, bit);
return s.ToArray();
}
/// <summary>
/// Get the <see cref="Encoding"/> used for chars and strings
/// </summary>
/// <returns><see cref="Encoding"/> used</returns>
public Encoding GetEncoding()
{
return encoding;
}
/// <summary>
/// Set the <see cref="Encoding"/> that will be used for chars and strings
/// </summary>
/// <param name="encoding"><see cref="Encoding"/> to use</param>
public void SetEncoding(Encoding encoding)
{
this.encoding = encoding;
}
/// <summary>
/// Changes the length of the stream, if new length is less than current length stream data will be truncated
/// </summary>
/// <param name="length">New stream length</param>
/// <returns>return true if stream changed length, false if it wasn't possible</returns>
public bool ChangeLength(long length)
{
if (stream.CanSeek && stream.CanWrite)
{
stream.SetLength(length);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Cuts the <see cref="BitStream"/> from the specified offset and given length, will throw an exception when length + offset is higher than stream's length, offset and bit will be set to 0
/// </summary>
/// <param name="offset">Offset to start</param>
/// <param name="length">Length of the new <see cref="BitStream"/></param>
public void CutStream(long offset, long length)
{
byte[] data = GetStreamData();
byte[] buffer = new byte[length];
Array.Copy(data, offset, buffer, 0, length);
this.stream = new MemoryStream();
MemoryStream m = new MemoryStream(buffer);
this.stream = new MemoryStream();
m.CopyTo(this.stream);
this.offset = 0;
bit = 0;
}
/// <summary>
/// Copies the current <see cref="BitStream"/> buffer to another <see cref="Stream"/>
/// </summary>
/// <param name="stream"><see cref="Stream"/> to copy buffer</param>
public void CopyStreamTo(Stream stream)
{
Seek(0, 0);
stream.SetLength(this.stream.Length);
this.stream.CopyTo(stream);
}
/// <summary>
/// Copies the current <see cref="BitStream"/> buffer to another <see cref="BitStream"/>
/// </summary>
/// <param name="stream"><see cref="BitStream"/> to copy buffer</param>
public void CopyStreamTo(BitStream stream)
{
Seek(0, 0);
stream.ChangeLength(this.stream.Length);
this.stream.CopyTo(stream.stream);
stream.Seek(0, 0);
}
/// <summary>
/// Saves current <see cref="BitStream"/> buffer into a file
/// </summary>
/// <param name="filename">File to write data, if it exists it will be overwritten</param>
public void SaveStreamAsFile(string filename)
{
File.WriteAllBytes(filename, GetStreamData());
}
/// <summary>
/// Returns the current content of the stream as a <see cref="MemoryStream"/>
/// </summary>
/// <returns><see cref="MemoryStream"/> containing current <see cref="BitStream"/> data</returns>
public MemoryStream CloneAsMemoryStream()
{
return new MemoryStream(GetStreamData());
}
/// <summary>
/// Returns the current content of the stream as a <see cref="BufferedStream"/>
/// </summary>
/// <returns><see cref="BufferedStream"/> containing current <see cref="BitStream"/> data</returns>
public BufferedStream CloneAsBufferedStream()
{
BufferedStream bs = new BufferedStream(stream);
StreamWriter sw = new StreamWriter(bs);
sw.Write(GetStreamData());
bs.Seek(0, SeekOrigin.Begin);
return bs;
}
/// <summary>
/// Checks if the <see cref="BitStream"/> will be in a valid position on its last bit read/write
/// </summary>
/// <param name="bits">Number of bits it will advance</param>
/// <returns>true if <see cref="BitStream"/> will be inside the stream length</returns>
private bool ValidPositionWhen(int bits)
{
long o = offset;
int b = bit;
b = (b + 1) % 8;
if (b == 0)
{
o++;
}
return o < Length;
}
#endregion
#region BitRead/Write
/// <summary>
/// Read current position bit and advances the position within the stream by one bit
/// </summary>
/// <returns>Returns the current position bit as 0 or 1</returns>
public Bit ReadBit()
{
if (!ValidPosition)
{
throw new IOException("Cannot read in an offset bigger than the length of the stream");
}
stream.Seek(offset, SeekOrigin.Begin);
byte value;
if (!MSB)
{
value = (byte)((stream.ReadByte() >> (bit)) & 1);
}
else
{
value = (byte)((stream.ReadByte() >> (7 - bit)) & 1);
}
AdvanceBit();
stream.Seek(offset, SeekOrigin.Begin);
return value;
}
/// <summary>
/// Read from current position the specified number of bits
/// </summary>
/// <param name="length">Bits to read</param>
/// <returns><see cref="Bit"/>[] containing read bits</returns>
public Bit[] ReadBits(int length)
{
Bit[] bits = new Bit[length];
for(int i=0;i< length; i++)
{
bits[i] = ReadBit();
}
return bits;
}
/// <summary>
/// Writes a bit in the current position
/// </summary>
/// <param name="data">Bit to write, it data is not 0 or 1 data = data & 1</param>
public void WriteBit(Bit data)
{
stream.Seek(offset, SeekOrigin.Begin);
byte value = (byte)stream.ReadByte();
stream.Seek(offset, SeekOrigin.Begin);
if (!MSB)
{
value &= (byte)~(1 << bit);
value |= (byte)(data << bit);
}
else
{
value &= (byte)~(1 << (7 - bit));
value |= (byte)(data << (7 - bit));
}
if (ValidPosition)
{
stream.WriteByte(value);
}
else
{
if (AutoIncreaseStream)
{
if (ChangeLength(Length + (offset - Length) + 1))
{
stream.WriteByte(value);
}
else
{
throw new IOException("Cannot write in an offset bigger than the length of the stream");
}
}
else
{
throw new IOException("Cannot write in an offset bigger than the length of the stream");
}
}
AdvanceBit();
stream.Seek(offset, SeekOrigin.Begin);
}
/// <summary>
/// Write a sequence of bits into the stream
/// </summary>
/// <param name="bits"><see cref="Bit"/>[] to write</param>
public void WriteBits(ICollection<Bit> bits)
{
foreach(Bit b in bits)
{
WriteBit(b);
}
}
/// <summary>
/// Write a sequence of bits into the stream
/// </summary>
/// <param name="bits"><see cref="Bit"/>[] to write</param>
/// <param name="length">Number of bits to write</param>
public void WriteBits(ICollection<Bit> bits, int length)
{
Bit[] b = new Bit[bits.Count];
bits.CopyTo(b, 0);
for (int i=0;i< length;i++)
{
WriteBit(b[i]);
}
}
/// <summary>
/// Write a sequence of bits into the stream
/// </summary>
/// <param name="bits"><see cref="Bit"/>[] to write</param>
/// <param name="offset">Offset to begin bit writing</param>
/// <param name="length">Number of bits to write</param>
public void WriteBits(Bit[] bits, int offset, int length)
{
for (int i = offset; i < length; i++)
{
WriteBit(bits[i]);
}
}
#endregion
#region Read
/// <summary>
/// Read from the current position bit the specified number of bits or bytes and creates a byte[]
/// </summary>
/// <param name="length">Number of bits or bytes</param>
/// <param name="isBytes">if true will consider length as byte length, if false it will count the specified length of bits</param>
/// <returns>byte[] containing bytes created from current position</returns>
public byte[] ReadBytes(long length, bool isBytes = false)
{
if (isBytes)
{
length *= 8;
}
List<byte> data = new List<byte>();
for (long i = 0; i < length;)
{
byte value = 0;
for (int p = 0; p < 8 && i < length; i++, p++)
{
if (!MSB)
{
value |= (byte)(ReadBit() << p);
}
else
{
value |= (byte)(ReadBit() << (7 - p));
}
}
data.Add(value);
}
return data.ToArray();
}
/// <summary>
/// Read a byte based on the current stream and bit position
/// </summary>
public byte ReadByte()
{
return ReadBytes(8)[0];
}
/// <summary>
/// Read a byte made of specified number of bits (1-8)
/// </summary>
public byte ReadByte(int bits)
{
if (bits < 0)
{
bits = 0;
}
if(bits > 8)
{
bits = 8;
}
return ReadBytes(bits)[0];
}
/// <summary>
/// Read a signed byte based on the current stream and bit position
/// </summary>
public sbyte ReadSByte()
{
return (sbyte)ReadBytes(8)[0];
}
/// <summary>
/// Read a sbyte made of specified number of bits (1-8)
/// </summary>
public sbyte ReadSByte(int bits)
{
if (bits < 0)
{
bits = 0;
}
if (bits > 8)
{
bits = 8;
}
return (sbyte)ReadBytes(bits)[0];
}
/// <summary>
/// Read a byte based on the current stream and bit position and check if it is 0
/// </summary>
public bool ReadBool()
{
return ReadBytes(8)[0] == 0 ? false : true;
}
/// <summary>
/// Read a char based on the current stream and bit position and the <see cref="BitStream"/> encoding
/// </summary>
public char ReadChar()
{
return encoding.GetChars(ReadBytes(encoding.GetMaxByteCount(1) * 8))[0];
}
/// <summary>
/// Read a string based on the current stream and bit position and the <see cref="BitStream"/> encoding
/// </summary>
/// <param name="length">Length of the string to read</param>
public string ReadString(int length)
{
int bitsPerChar = encoding.GetByteCount(" ") * 8;
return encoding.GetString(ReadBytes(bitsPerChar*length));
}
/// <summary>
/// Read a short based on the current stream and bit position
/// </summary>
public short ReadInt16()
{
short value = BitConverter.ToInt16(ReadBytes(16), 0);
return value;
}
/// <summary>
/// Read a 24bit value based on the current stream and bit position
/// </summary>
public Int24 ReadInt24()
{
byte[] bytes = ReadBytes(24);
Array.Resize(ref bytes, 4);
Int24 value = BitConverter.ToInt32(bytes, 0);
return value;
}
/// <summary>
/// Read an int based on the current stream and bit position
/// </summary>
public int ReadInt32()
{
int value = BitConverter.ToInt32(ReadBytes(32), 0);
return value;
}
/// <summary>
/// Read a 48bit value based on the current stream and bit position
/// </summary>
public Int48 ReadInt48()
{
byte[] bytes = ReadBytes(48);
Array.Resize(ref bytes, 8);
Int48 value = BitConverter.ToInt64(bytes, 0);
return value;
}
/// <summary>
/// Read a long based on the current stream and bit position
/// </summary>
public long ReadInt64()
{
long value = BitConverter.ToInt64(ReadBytes(64), 0);
return value;
}
/// <summary>
/// Read a ushort based on the current stream and bit position
/// </summary>
public ushort ReadUInt16()
{
ushort value = BitConverter.ToUInt16(ReadBytes(16), 0);
return value;
}
/// <summary>
/// Read an unsigned 24bit value based on the current stream and bit position
/// </summary>
public UInt24 ReadUInt24()
{
byte[] bytes = ReadBytes(24);
Array.Resize(ref bytes, 4);
UInt24 value = BitConverter.ToUInt32(bytes, 0);
return value;
}
/// <summary>
/// Read an uint based on the current stream and bit position
/// </summary>
public uint ReadUInt32()
{
uint value = BitConverter.ToUInt32(ReadBytes(32), 0);
return value;
}
/// <summary>
/// Read an unsigned 48bit value based on the current stream and bit position
/// </summary>
public UInt48 ReadUInt48()
{
byte[] bytes = ReadBytes(48);
Array.Resize(ref bytes, 8);
UInt48 value = BitConverter.ToUInt64(bytes, 0);
return value;
}
/// <summary>
/// Read an ulong based on the current stream and bit position
/// </summary>
public ulong ReadUInt64()
{
ulong value = BitConverter.ToUInt64(ReadBytes(64), 0);
return value;
}
#endregion
#region Write
/// <summary>
/// Writes as bits a byte[] by a specified number of bits or bytes
/// </summary>
/// <param name="data">byte[] to write</param>
/// <param name="length">Number of bits or bytes to use from the array</param>
/// <param name="isBytes">if true will consider length as byte length, if false it will count the specified length of bits</param>
public void WriteBytes(byte[] data, long length, bool isBytes = false)
{
if (isBytes)
{
length *= 8;
}
int position = 0;
for (long i = 0; i < length;)
{
byte value = 0;
for (int p = 0; p < 8 && i < length; i++, p++)
{
if (!MSB)
{
value = (byte)((data[position] >> p) & 1);
}
else
{
value = (byte)((data[position] >> (7 - p)) & 1);
}
WriteBit(value);
}
position++;
}
}
/// <summary>
/// Write a byte value based on the current stream and bit position
/// </summary>
public void WriteByte(byte value)
{
WriteBytes(new byte[] { value }, 8);
}
/// <summary>
/// Write a byte value based on the current stream and bit position
/// </summary>
public void WriteByte(byte value, int bits)
{
if (bits < 0)
{
bits = 0;
}
if (bits > 8)
{
bits = 8;
}
WriteBytes(new byte[] { value }, bits);
}
/// <summary>
/// Write a byte value based on the current stream and bit position
/// </summary>
public void WriteSByte(sbyte value)
{
WriteBytes(new byte[] { (byte)value }, 8);
}
/// <summary>
/// Write a byte value based on the current stream and bit position
/// </summary>
public void WriteSByte(sbyte value, int bits)
{
if (bits < 0)
{
bits = 0;
}
if (bits > 8)
{
bits = 8;
}
WriteBytes(new byte[] { (byte)value }, bits);
}
/// <summary>
/// Write a bool value as 0:false, 1:true as byte based on the current stream and bit position
/// </summary>
public void WriteBool(bool value)
{
WriteBytes(new byte[] { value ? (byte)1 : (byte)0 }, 8);
}
/// <summary>
/// Write a char value based on the <see cref="BitStream"/> encoding
/// </summary>
public void WriteChar(char value)
{
byte[] bytes = encoding.GetBytes(new char[] { value }, 0, 1);
WriteBytes(bytes, bytes.Length*8);
}
/// <summary>
/// Write a string based on the <see cref="BitStream"/> encoding
/// </summary>
public void WriteString(string value)
{
byte[] bytes = encoding.GetBytes(value);
WriteBytes(bytes, bytes.Length * 8);
}
/// <summary>
/// Write a short value based on the current stream and bit position
/// </summary>
public void WriteInt16(short value)
{
WriteBytes(BitConverter.GetBytes(value), 16);
}
/// <summary>
/// Write a 24bit value based on the current stream and bit position
/// </summary>
public void WriteInt24(Int24 value)
{
WriteBytes(BitConverter.GetBytes(value), 24);
}
/// <summary>
/// Write an int value based on the current stream and bit position
/// </summary>
public void WriteInt32(int value)
{
WriteBytes(BitConverter.GetBytes(value), 32);
}
/// <summary>
/// Write a 48bit value based on the current stream and bit position
/// </summary>
public void WriteInt48(Int48 value)
{
WriteBytes(BitConverter.GetBytes(value), 48);
}
/// <summary>
/// Write a long value based on the current stream and bit position
/// </summary>
public void WriteInt64(long value)
{
WriteBytes(BitConverter.GetBytes(value), 64);
}
/// <summary>
/// Write an ushort value based on the current stream and bit position
/// </summary>
public void WriteUInt16(ushort value)
{
WriteBytes(BitConverter.GetBytes(value), 16);
}
/// <summary>
/// Write an unsigned 24bit value based on the current stream and bit position
/// </summary>
public void WriteUInt24(UInt24 value)
{
WriteBytes(BitConverter.GetBytes(value), 24);
}
/// <summary>
/// Write an uint value based on the current stream and bit position
/// </summary>
public void WriteUInt32(uint value)
{
WriteBytes(BitConverter.GetBytes(value), 32);
}
/// <summary>
/// Write an unsigned 48bit value based on the current stream and bit position
/// </summary>
public void WriteUInt48(UInt48 value)
{
WriteBytes(BitConverter.GetBytes(value), 48);
}
/// <summary>
/// Write an ulong value based on the current stream and bit position
/// </summary>
public void WriteUInt64(ulong value)
{
WriteBytes(BitConverter.GetBytes(value), 64);
}
#endregion
#region Shifts
/// <summary>
/// Do a bitwise shift on the current position of the stream on bit 0
/// </summary>
/// <param name="bits">bits to shift</param>
/// <param name="leftShift">true to left shift, false to right shift</param>
public void bitwiseShift(int bits, bool leftShift)
{
if (!ValidPositionWhen(8))
{
throw new IOException("Cannot read in an offset bigger than the length of the stream");
}
Seek(offset, 0);
if (bits != 0 && bits <= 7)
{
byte value = (byte)stream.ReadByte();
if (leftShift)
{
value = (byte)(value << bits);
}
else
{
value = (byte)(value >> bits);
}
Seek(offset, 0);
stream.WriteByte(value);
}
bit = 0;
offset++;
}
/// <summary>
/// Do a bitwise shift on the current position of the stream on current bit
/// </summary>
/// <param name="bits">bits to shift</param>
/// <param name="leftShift">true to left shift, false to right shift</param>
public void bitwiseShiftOnBit(int bits, bool leftShift)
{
if (!ValidPositionWhen(8))
{
throw new IOException("Cannot read in an offset bigger than the length of the stream");
}
Seek(offset, bit);
if (bits != 0 && bits <= 7)
{
byte value = ReadByte();
if (leftShift)
{
value = (byte)(value << bits);
}
else
{
value = (byte)(value >> bits);
}
offset--;
Seek(offset, bit);
WriteByte(value);
}
offset++;
}
/// <summary>