@@ -276,16 +276,16 @@ public char ReadChar(EncodingType encodingType, long offset)
276
276
BaseStream . Position = offset ;
277
277
return ReadChar ( encodingType ) ;
278
278
}
279
- public char [ ] ReadChars ( int count )
279
+ public char [ ] ReadChars ( int count , bool trimNullTerminators )
280
280
{
281
- return ReadChars ( count , Encoding ) ;
281
+ return ReadChars ( count , trimNullTerminators , Encoding ) ;
282
282
}
283
- public char [ ] ReadChars ( int count , long offset )
283
+ public char [ ] ReadChars ( int count , bool trimNullTerminators , long offset )
284
284
{
285
285
BaseStream . Position = offset ;
286
- return ReadChars ( count ) ;
286
+ return ReadChars ( count , trimNullTerminators ) ;
287
287
}
288
- public char [ ] ReadChars ( int count , EncodingType encodingType )
288
+ public char [ ] ReadChars ( int count , bool trimNullTerminators , EncodingType encodingType )
289
289
{
290
290
if ( Utils . ValidateReadArraySize ( count , out char [ ] array ) )
291
291
{
@@ -294,12 +294,21 @@ public char[] ReadChars(int count, EncodingType encodingType)
294
294
Encoding encoding = Utils . EncodingFromEnum ( encodingType ) ;
295
295
int encodingSize = Utils . EncodingSize ( encoding ) ;
296
296
ReadBytesIntoBuffer ( count * encodingSize ) ;
297
- return encoding . GetChars ( _buffer , 0 , encodingSize * count ) ;
297
+ array = encoding . GetChars ( _buffer , 0 , encodingSize * count ) ;
298
+ if ( trimNullTerminators )
299
+ {
300
+ int i = Array . IndexOf ( array , '\0 ' ) ;
301
+ if ( i != - 1 )
302
+ {
303
+ Array . Resize ( ref array , i ) ;
304
+ }
305
+ }
306
+ return array ;
298
307
}
299
- public char [ ] ReadChars ( int count , EncodingType encodingType , long offset )
308
+ public char [ ] ReadChars ( int count , bool trimNullTerminators , EncodingType encodingType , long offset )
300
309
{
301
310
BaseStream . Position = offset ;
302
- return ReadChars ( count , encodingType ) ;
311
+ return ReadChars ( count , trimNullTerminators , encodingType ) ;
303
312
}
304
313
public string ReadStringNullTerminated ( )
305
314
{
@@ -329,23 +338,23 @@ public string ReadStringNullTerminated(EncodingType encodingType, long offset)
329
338
BaseStream . Position = offset ;
330
339
return ReadStringNullTerminated ( encodingType ) ;
331
340
}
332
- public string ReadString ( int charCount )
341
+ public string ReadString ( int charCount , bool trimNullTerminators )
333
342
{
334
- return ReadString ( charCount , Encoding ) ;
343
+ return ReadString ( charCount , trimNullTerminators , Encoding ) ;
335
344
}
336
- public string ReadString ( int charCount , long offset )
345
+ public string ReadString ( int charCount , bool trimNullTerminators , long offset )
337
346
{
338
347
BaseStream . Position = offset ;
339
- return ReadString ( charCount ) ;
348
+ return ReadString ( charCount , trimNullTerminators ) ;
340
349
}
341
- public string ReadString ( int charCount , EncodingType encodingType )
350
+ public string ReadString ( int charCount , bool trimNullTerminators , EncodingType encodingType )
342
351
{
343
- return new string ( ReadChars ( charCount , encodingType ) ) ;
352
+ return new string ( ReadChars ( charCount , trimNullTerminators , encodingType ) ) ;
344
353
}
345
- public string ReadString ( int charCount , EncodingType encodingType , long offset )
354
+ public string ReadString ( int charCount , bool trimNullTerminators , EncodingType encodingType , long offset )
346
355
{
347
356
BaseStream . Position = offset ;
348
- return ReadString ( charCount , encodingType ) ;
357
+ return ReadString ( charCount , trimNullTerminators , encodingType ) ;
349
358
}
350
359
public string [ ] ReadStringsNullTerminated ( int count )
351
360
{
@@ -373,31 +382,31 @@ public string[] ReadStringsNullTerminated(int count, EncodingType encodingType,
373
382
BaseStream . Position = offset ;
374
383
return ReadStringsNullTerminated ( count , encodingType ) ;
375
384
}
376
- public string [ ] ReadStrings ( int count , int charCount )
385
+ public string [ ] ReadStrings ( int count , int charCount , bool trimNullTerminators )
377
386
{
378
- return ReadStrings ( count , charCount , Encoding ) ;
387
+ return ReadStrings ( count , charCount , trimNullTerminators , Encoding ) ;
379
388
}
380
- public string [ ] ReadStrings ( int count , int charCount , long offset )
389
+ public string [ ] ReadStrings ( int count , int charCount , bool trimNullTerminators , long offset )
381
390
{
382
391
BaseStream . Position = offset ;
383
- return ReadStrings ( count , charCount ) ;
392
+ return ReadStrings ( count , charCount , trimNullTerminators ) ;
384
393
}
385
- public string [ ] ReadStrings ( int count , int charCount , EncodingType encodingType )
394
+ public string [ ] ReadStrings ( int count , int charCount , bool trimNullTerminators , EncodingType encodingType )
386
395
{
387
396
if ( ! Utils . ValidateReadArraySize ( count , out string [ ] array ) )
388
397
{
389
398
array = new string [ count ] ;
390
399
for ( int i = 0 ; i < count ; i ++ )
391
400
{
392
- array [ i ] = ReadString ( charCount , encodingType ) ;
401
+ array [ i ] = ReadString ( charCount , trimNullTerminators , encodingType ) ;
393
402
}
394
403
}
395
404
return array ;
396
405
}
397
- public string [ ] ReadStrings ( int count , int charCount , EncodingType encodingType , long offset )
406
+ public string [ ] ReadStrings ( int count , int charCount , bool trimNullTerminators , EncodingType encodingType , long offset )
398
407
{
399
408
BaseStream . Position = offset ;
400
- return ReadStrings ( count , charCount , encodingType ) ;
409
+ return ReadStrings ( count , charCount , trimNullTerminators , encodingType ) ;
401
410
}
402
411
public short ReadInt16 ( )
403
412
{
@@ -739,7 +748,8 @@ public void ReadIntoObject(object obj)
739
748
case TypeCode . Char :
740
749
{
741
750
EncodingType encodingType = Utils . AttributeValueOrDefault < BinaryEncodingAttribute , EncodingType > ( propertyInfo , Encoding ) ;
742
- value = ReadChars ( arrayLength , encodingType ) ;
751
+ bool trimNullTerminators = Utils . AttributeValueOrDefault < BinaryStringTrimNullTerminatorsAttribute , bool > ( propertyInfo , false ) ;
752
+ value = ReadChars ( arrayLength , trimNullTerminators , encodingType ) ;
743
753
break ;
744
754
}
745
755
case TypeCode . Int16 : value = ReadInt16s ( arrayLength ) ; break ;
@@ -762,7 +772,8 @@ public void ReadIntoObject(object obj)
762
772
}
763
773
else
764
774
{
765
- value = ReadStrings ( arrayLength , stringLength , encodingType ) ;
775
+ bool trimNullTerminators = Utils . AttributeValueOrDefault < BinaryStringTrimNullTerminatorsAttribute , bool > ( propertyInfo , false ) ;
776
+ value = ReadStrings ( arrayLength , stringLength , trimNullTerminators , encodingType ) ;
766
777
}
767
778
break ;
768
779
}
@@ -834,7 +845,8 @@ public void ReadIntoObject(object obj)
834
845
}
835
846
else
836
847
{
837
- value = ReadString ( stringLength , encodingType ) ;
848
+ bool trimNullTerminators = Utils . AttributeValueOrDefault < BinaryStringTrimNullTerminatorsAttribute , bool > ( propertyInfo , false ) ;
849
+ value = ReadString ( stringLength , trimNullTerminators , encodingType ) ;
838
850
}
839
851
break ;
840
852
}
0 commit comments