@@ -7,69 +7,60 @@ namespace SteamKit2
7
7
{
8
8
internal static class StreamHelpers
9
9
{
10
- [ ThreadStatic ]
11
- static byte [ ] ? data ;
12
-
13
- [ MemberNotNull ( nameof ( data ) ) ]
14
- static void EnsureInitialized ( )
15
- {
16
- data ??= new byte [ 8 ] ;
17
- }
18
-
19
10
public static short ReadInt16 ( this Stream stream )
20
11
{
21
- EnsureInitialized ( ) ;
12
+ Span < byte > data = stackalloc byte [ 2 ] ;
22
13
23
- stream . Read ( data , 0 , 2 ) ;
24
- return BitConverter . ToInt16 ( data , 0 ) ;
14
+ stream . Read ( data ) ;
15
+ return BitConverter . ToInt16 ( data ) ;
25
16
}
26
17
27
18
public static ushort ReadUInt16 ( this Stream stream )
28
19
{
29
- EnsureInitialized ( ) ;
20
+ Span < byte > data = stackalloc byte [ 2 ] ;
30
21
31
- stream . Read ( data , 0 , 2 ) ;
32
- return BitConverter . ToUInt16 ( data , 0 ) ;
22
+ stream . Read ( data ) ;
23
+ return BitConverter . ToUInt16 ( data ) ;
33
24
}
34
25
35
26
public static int ReadInt32 ( this Stream stream )
36
27
{
37
- EnsureInitialized ( ) ;
28
+ Span < byte > data = stackalloc byte [ 4 ] ;
38
29
39
- stream . Read ( data , 0 , 4 ) ;
40
- return BitConverter . ToInt32 ( data , 0 ) ;
30
+ stream . Read ( data ) ;
31
+ return BitConverter . ToInt32 ( data ) ;
41
32
}
42
33
43
34
public static long ReadInt64 ( this Stream stream )
44
35
{
45
- EnsureInitialized ( ) ;
36
+ Span < byte > data = stackalloc byte [ 8 ] ;
46
37
47
- stream . Read ( data , 0 , 8 ) ;
48
- return BitConverter . ToInt64 ( data , 0 ) ;
38
+ stream . Read ( data ) ;
39
+ return BitConverter . ToInt64 ( data ) ;
49
40
}
50
41
51
42
public static uint ReadUInt32 ( this Stream stream )
52
43
{
53
- EnsureInitialized ( ) ;
44
+ Span < byte > data = stackalloc byte [ 4 ] ;
54
45
55
- stream . Read ( data , 0 , 4 ) ;
56
- return BitConverter . ToUInt32 ( data , 0 ) ;
46
+ stream . Read ( data ) ;
47
+ return BitConverter . ToUInt32 ( data ) ;
57
48
}
58
49
59
50
public static ulong ReadUInt64 ( this Stream stream )
60
51
{
61
- EnsureInitialized ( ) ;
52
+ Span < byte > data = stackalloc byte [ 8 ] ;
62
53
63
- stream . Read ( data , 0 , 8 ) ;
64
- return BitConverter . ToUInt64 ( data , 0 ) ;
54
+ stream . Read ( data ) ;
55
+ return BitConverter . ToUInt64 ( data ) ;
65
56
}
66
57
67
58
public static float ReadFloat ( this Stream stream )
68
59
{
69
- EnsureInitialized ( ) ;
60
+ Span < byte > data = stackalloc byte [ 4 ] ;
70
61
71
- stream . Read ( data , 0 , 4 ) ;
72
- return BitConverter . ToSingle ( data , 0 ) ;
62
+ stream . Read ( data ) ;
63
+ return BitConverter . ToSingle ( data ) ;
73
64
}
74
65
75
66
public static string ReadNullTermString ( this Stream stream , Encoding encoding )
0 commit comments