11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4- using System . IO ;
54using System . Text ;
65
76namespace FastSerialization
@@ -13,12 +12,31 @@ public class SegmentedMemoryStreamReader
1312 /// <summary>
1413 /// Create a IStreamReader (reads binary data) from a given byte buffer
1514 /// </summary>
16- public SegmentedMemoryStreamReader ( SegmentedList < byte > data ) : this ( data , 0 , ( int ) data . Count ) { }
15+ public SegmentedMemoryStreamReader ( SegmentedList < byte > data , SerializationConfiguration config = null ) : this ( data , 0 , data . Count , config ) { }
1716 /// <summary>
1817 /// Create a IStreamReader (reads binary data) from a given subregion of a byte buffer
1918 /// </summary>
20- public SegmentedMemoryStreamReader ( SegmentedList < byte > data , int start , int length )
19+ public SegmentedMemoryStreamReader ( SegmentedList < byte > data , long start , long length , SerializationConfiguration config = null )
2120 {
21+ SerializationConfiguration = config ?? new SerializationConfiguration ( ) ;
22+
23+ if ( SerializationConfiguration . StreamLabelWidth == StreamLabelWidth . FourBytes )
24+ {
25+ readLabel = ( ) =>
26+ {
27+ return ( StreamLabel ) ( uint ) ReadInt32 ( ) ;
28+ } ;
29+ sizeOfSerializedStreamLabel = 4 ;
30+ }
31+ else
32+ {
33+ readLabel = ( ) =>
34+ {
35+ return ( StreamLabel ) ( ulong ) ReadInt64 ( ) ;
36+ } ;
37+ sizeOfSerializedStreamLabel = 8 ;
38+ }
39+
2240 bytes = new SegmentedList < byte > ( 65_536 , length ) ;
2341 bytes . AppendFrom ( data , start , length ) ;
2442 position = start ;
@@ -130,18 +148,24 @@ public string ReadString()
130148 /// <summary>
131149 /// Implementation of IStreamReader
132150 /// </summary>
133- public StreamLabel ReadLabel ( )
134- {
135- return ( StreamLabel ) ( uint ) ReadInt32 ( ) ;
136- }
151+ public StreamLabel ReadLabel ( ) => readLabel ( ) ;
152+
137153 /// <summary>
138154 /// Implementation of IStreamReader
139155 /// </summary>
140156 public virtual void Goto ( StreamLabel label )
141157 {
142158 Debug . Assert ( label != StreamLabel . Invalid ) ;
143- Debug . Assert ( ( long ) label <= int . MaxValue ) ;
144- position = ( int ) label ;
159+
160+ if ( SerializationConfiguration . StreamLabelWidth == StreamLabelWidth . FourBytes )
161+ {
162+ Debug . Assert ( ( long ) label <= int . MaxValue ) ;
163+ position = ( uint ) label ;
164+ }
165+ else
166+ {
167+ position = ( long ) label ;
168+ }
145169 }
146170 /// <summary>
147171 /// Implementation of IStreamReader
@@ -150,16 +174,22 @@ public virtual StreamLabel Current
150174 {
151175 get
152176 {
153- return ( StreamLabel ) ( uint ) position ;
177+ if ( SerializationConfiguration . StreamLabelWidth == StreamLabelWidth . FourBytes )
178+ {
179+ return ( StreamLabel ) ( uint ) position ;
180+ }
181+ else
182+ {
183+ return ( StreamLabel ) position ;
184+ }
154185 }
155186 }
156187 /// <summary>
157188 /// Implementation of IStreamReader
158189 /// </summary>
159190 public virtual void GotoSuffixLabel ( )
160191 {
161- const int serializedStreamLabelSize = 4 ;
162- Goto ( ( StreamLabel ) ( Length - serializedStreamLabelSize ) ) ;
192+ Goto ( ( StreamLabel ) ( Length - sizeOfSerializedStreamLabel ) ) ;
163193 Goto ( ReadLabel ( ) ) ;
164194 }
165195 /// <summary>
@@ -174,6 +204,11 @@ public void Dispose()
174204 /// Dispose pattern
175205 /// </summary>
176206 protected virtual void Dispose ( bool disposing ) { }
207+
208+ /// <summary>
209+ /// Returns the SerializationConfiguration for this stream reader.
210+ /// </summary>
211+ internal SerializationConfiguration SerializationConfiguration { get ; private set ; }
177212 #endregion
178213
179214 #region private
@@ -182,9 +217,11 @@ protected virtual void Dispose(bool disposing) { }
182217 throw new Exception ( "Streamreader read past end of buffer" ) ;
183218 }
184219 internal /*protected*/ SegmentedList < byte > bytes ;
185- internal /*protected*/ int position ;
186- internal /*protected*/ int endPosition ;
220+ internal /*protected*/ long position ;
221+ internal /*protected*/ long endPosition ;
187222 private StringBuilder sb ;
223+ private Func < StreamLabel > readLabel ;
224+ private readonly int sizeOfSerializedStreamLabel ;
188225 #endregion
189226 }
190227}
0 commit comments