@@ -9,35 +9,35 @@ public class StreamHelperReadException : Exception
99 public StreamHelperReadException ( string message ) : base ( message )
1010 {
1111 }
12- }
12+ }
1313
1414 public static class StreamHelpers
1515 {
1616 public static int ReadInt ( Stream stream )
1717 {
18- byte [ ] buffer = new byte [ 8 ] ;
18+ Span < byte > buffer = stackalloc byte [ 4 ] ;
1919
20- if ( stream . Read ( buffer , 0 , 4 ) != 4 )
20+ if ( stream . Read ( buffer ) != sizeof ( int ) )
2121 throw new StreamHelperReadException ( "Failed to read integer from stream: end of stream" ) ;
2222
23- return BitConverter . ToInt32 ( buffer , 0 ) ;
23+ return BitConverter . ToInt32 ( buffer ) ;
2424 }
2525
2626 public static long ReadLong ( Stream stream )
2727 {
28- byte [ ] buffer = new byte [ 8 ] ;
28+ Span < byte > buffer = stackalloc byte [ 8 ] ;
2929
30- if ( stream . Read ( buffer , 0 , 8 ) != 8 )
30+ if ( stream . Read ( buffer ) != sizeof ( int ) )
3131 throw new StreamHelperReadException ( "Failed to read integer from stream: end of stream" ) ;
3232
33- return BitConverter . ToInt64 ( buffer , 0 ) ;
33+ return BitConverter . ToInt64 ( buffer ) ;
3434 }
3535
3636 public static string ReadASCIIString ( Stream stream )
3737 {
3838 int length = ReadInt ( stream ) ;
39- byte [ ] stringBuffer = new byte [ length ] ;
40- if ( stream . Read ( stringBuffer , 0 , length ) != length )
39+ Span < byte > stringBuffer = stackalloc byte [ length ] ;
40+ if ( stream . Read ( stringBuffer ) != length )
4141 throw new StreamHelperReadException ( "Failed to read string from stream: end of stream" ) ;
4242
4343 if ( length == - 1 )
@@ -49,11 +49,13 @@ public static string ReadASCIIString(Stream stream)
4949
5050 public static byte [ ] ASCIIStringToBytes ( string str )
5151 {
52- byte [ ] buffer = new byte [ sizeof ( int ) + str . Length ] ;
53- Array . Copy ( BitConverter . GetBytes ( str . Length ) , buffer , sizeof ( int ) ) ;
54- byte [ ] stringBytes = Encoding . ASCII . GetBytes ( str ) ;
55- Array . Copy ( stringBytes , 0 , buffer , sizeof ( int ) , stringBytes . Length ) ;
56- return buffer ;
52+ int length = sizeof ( int ) + str . Length ;
53+ Span < byte > span = stackalloc byte [ length ] ;
54+
55+ BitConverter . TryWriteBytes ( span , str . Length ) ;
56+ Encoding . ASCII . GetBytes ( str , span . Slice ( sizeof ( int ) ) ) ;
57+
58+ return span . ToArray ( ) ;
5759 }
5860
5961 public static string ReadUnicodeString ( Stream stream )
@@ -63,22 +65,23 @@ public static string ReadUnicodeString(Stream stream)
6365 if ( length == - 1 )
6466 return null ;
6567
66- byte [ ] stringBuffer = new byte [ length ] ;
67- if ( stream . Read ( stringBuffer , 0 , length ) != length )
68+ Span < byte > stringBuffer = stackalloc byte [ length ] ;
69+ if ( stream . Read ( stringBuffer ) != length )
6870 throw new StreamHelperReadException ( "Failed to read Unicode string from stream: end of stream" ) ;
6971
7072 string result = Encoding . UTF8 . GetString ( stringBuffer ) ;
7173 return result ;
7274 }
7375
7476 public static byte [ ] UnicodeStringToBytes ( string str )
75- {
76- byte [ ] buffer = new byte [ sizeof ( int ) + str . Length ] ;
77-
78- Array . Copy ( BitConverter . GetBytes ( str . Length ) , buffer , sizeof ( int ) ) ;
79- byte [ ] stringBytes = Encoding . UTF8 . GetBytes ( str ) ;
80- Array . Copy ( stringBytes , 0 , buffer , sizeof ( int ) , stringBytes . Length ) ;
81- return buffer ;
77+ {
78+ int length = sizeof ( int ) + str . Length ;
79+ Span < byte > span = stackalloc byte [ length ] ;
80+
81+ BitConverter . TryWriteBytes ( span , str . Length ) ;
82+ Encoding . Unicode . GetBytes ( str , span . Slice ( sizeof ( int ) ) ) ;
83+
84+ return span . ToArray ( ) ;
8285 }
8386
8487 public static bool ReadBool ( Stream stream )
@@ -98,15 +101,13 @@ public static void WriteUShort(Stream stream, ushort shortNum)
98101
99102 public static void WriteUnicodeString ( Stream stream , string str )
100103 {
101- byte [ ] bytes ;
102-
103104 if ( string . IsNullOrEmpty ( str ) )
104105 {
105106 stream . Write ( BitConverter . GetBytes ( - 1 ) ) ;
106107 }
107108 else
108109 {
109- bytes = Encoding . UTF8 . GetBytes ( str ) ;
110+ byte [ ] bytes = Encoding . UTF8 . GetBytes ( str ) ;
110111 stream . Write ( BitConverter . GetBytes ( bytes . Length ) ) ;
111112 stream . Write ( bytes ) ;
112113 }
0 commit comments