@@ -25,48 +25,41 @@ public byte GetByte()
25
25
26
26
public void GetBytes ( scoped Span < byte > bytes )
27
27
{
28
- if ( _position + bytes . Length > _bytes . Length )
29
- throw new IOException ( "End of data reached." ) ;
30
-
31
- _bytes . Slice ( _position , bytes . Length ) . CopyTo ( bytes ) ;
32
- _position += bytes . Length ;
28
+ var buffer = Advance ( bytes . Length ) ;
29
+ buffer . CopyTo ( bytes ) ;
33
30
}
34
31
35
32
public byte [ ] GetBytes ( int count )
36
33
{
37
- if ( _position + count > _bytes . Length )
38
- throw new IOException ( "End of data reached." ) ;
39
-
34
+ var buffer = Advance ( count ) ;
40
35
var bytes = new byte [ count ] ;
41
36
42
- _bytes . Slice ( _position , count ) . CopyTo ( bytes ) ;
43
- _position += count ;
37
+ buffer . CopyTo ( bytes ) ;
44
38
return bytes ;
45
39
}
46
40
47
- public void Advance ( int n )
41
+ private ReadOnlySpan < byte > Advance ( int count )
48
42
{
49
- Debug . Assert ( n >= 0 , "n must be zero or greater" ) ;
43
+ Debug . Assert ( count >= 0 , "count must be zero or greater" ) ;
50
44
51
- if ( _position + n > _bytes . Length )
45
+ if ( _position + count > _bytes . Length )
52
46
throw new IOException ( "End of data reached." ) ;
53
47
54
- _position += n ;
48
+ var span = _bytes . Slice ( _position , count ) ;
49
+
50
+ _position += count ;
51
+
52
+ return span ;
55
53
}
56
54
57
- public bool TryAdvance ( int n )
55
+ public void Skip ( int count )
58
56
{
59
- Debug . Assert ( n >= 0 , "n must be zero or greater" ) ;
57
+ Debug . Assert ( count >= 0 , "count must be zero or greater" ) ;
60
58
61
- _position += n ;
62
-
63
- if ( _position > _bytes . Length )
64
- {
65
- _position = _bytes . Length ;
66
- return false ;
67
- }
59
+ if ( _position + count > _bytes . Length )
60
+ throw new IOException ( "End of data reached." ) ;
68
61
69
- return true ;
62
+ _position += count ;
70
63
}
71
64
72
65
public sbyte GetSByte ( )
@@ -76,8 +69,7 @@ public sbyte GetSByte()
76
69
77
70
public ushort GetUInt16 ( )
78
71
{
79
- var bytes = _bytes . Slice ( _position , 2 ) ;
80
- Advance ( 2 ) ;
72
+ var bytes = Advance ( 2 ) ;
81
73
82
74
return _isBigEndian
83
75
? BinaryPrimitives . ReadUInt16BigEndian ( bytes )
@@ -86,8 +78,7 @@ public ushort GetUInt16()
86
78
87
79
public short GetInt16 ( )
88
80
{
89
- var bytes = _bytes . Slice ( _position , 2 ) ;
90
- Advance ( 2 ) ;
81
+ var bytes = Advance ( 2 ) ;
91
82
92
83
return _isBigEndian
93
84
? BinaryPrimitives . ReadInt16BigEndian ( bytes )
@@ -96,8 +87,7 @@ public short GetInt16()
96
87
97
88
public uint GetUInt32 ( )
98
89
{
99
- var bytes = _bytes . Slice ( _position , 4 ) ;
100
- Advance ( 4 ) ;
90
+ var bytes = Advance ( 4 ) ;
101
91
102
92
return _isBigEndian
103
93
? BinaryPrimitives . ReadUInt32BigEndian ( bytes )
@@ -106,8 +96,7 @@ public uint GetUInt32()
106
96
107
97
public int GetInt32 ( )
108
98
{
109
- var bytes = _bytes . Slice ( _position , 4 ) ;
110
- Advance ( 4 ) ;
99
+ var bytes = Advance ( 4 ) ;
111
100
112
101
return _isBigEndian
113
102
? BinaryPrimitives . ReadInt32BigEndian ( bytes )
@@ -116,8 +105,7 @@ public int GetInt32()
116
105
117
106
public long GetInt64 ( )
118
107
{
119
- var bytes = _bytes . Slice ( _position , 8 ) ;
120
- Advance ( 8 ) ;
108
+ var bytes = Advance ( 8 ) ;
121
109
122
110
return _isBigEndian
123
111
? BinaryPrimitives . ReadInt64BigEndian ( bytes )
0 commit comments