@@ -296,7 +296,7 @@ public override int Read(byte[] buf, int offset, int size)
296296
297297 try
298298 {
299- byte [ ] tbuf = _in . ReadBytes ( size ) ;
299+ byte [ ] tbuf = ReadBytesExactly ( size ) ;
300300 Buffer . BlockCopy ( tbuf , 0 , buf , offset , tbuf . Length ) ;
301301 result = tbuf . Length ;
302302 }
@@ -314,7 +314,7 @@ public override int Read(sbyte[] buf, int offset, int size)
314314
315315 try
316316 {
317- byte [ ] tbuf = _in . ReadBytes ( size ) ;
317+ byte [ ] tbuf = ReadBytesExactly ( size ) ;
318318 for ( int i = 0 ; i < tbuf . Length ; ++ i )
319319 {
320320 buf [ i + offset ] = ( sbyte ) tbuf [ i ] ;
@@ -326,7 +326,7 @@ public override int Read(sbyte[] buf, int offset, int size)
326326 result = 0 ;
327327 }
328328
329- return 0 ;
329+ return result ;
330330 }
331331
332332 public override int Read ( bool [ ] buf , int offset , int size )
@@ -335,7 +335,7 @@ public override int Read(bool[] buf, int offset, int size)
335335
336336 try
337337 {
338- byte [ ] tbuf = _in . ReadBytes ( boolByteStride * size ) ;
338+ byte [ ] tbuf = ReadBytesExactly ( boolByteStride * size ) ;
339339 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += boolByteStride )
340340 {
341341 buf [ nRead + offset ] = BitConverter . ToBoolean ( tbuf , b ) ;
@@ -358,7 +358,7 @@ public override int Read(char[] buf, int offset, int size)
358358
359359 try
360360 {
361- byte [ ] tbuf = _in . ReadBytes ( charByteStride * size ) ;
361+ byte [ ] tbuf = ReadBytesExactly ( charByteStride * size ) ;
362362 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += charByteStride )
363363 {
364364 buf [ nRead + offset ] = ( char ) ( ( tbuf [ b ] << 8 ) | tbuf [ b + 1 ] ) ;
@@ -378,7 +378,7 @@ public override int Read(short[] buf, int offset, int size)
378378
379379 try
380380 {
381- byte [ ] tbuf = _in . ReadBytes ( shortByteStride * size ) ;
381+ byte [ ] tbuf = ReadBytesExactly ( shortByteStride * size ) ;
382382#if NETSTANDARD2_0
383383 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += shortByteStride )
384384 {
@@ -405,7 +405,7 @@ public override int Read(int[] buf, int offset, int size)
405405
406406 try
407407 {
408- byte [ ] tbuf = _in . ReadBytes ( intByteStride * size ) ;
408+ byte [ ] tbuf = ReadBytesExactly ( intByteStride * size ) ;
409409#if NETSTANDARD2_0
410410 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += intByteStride )
411411 {
@@ -432,7 +432,7 @@ public override int Read(long[] buf, int offset, int size)
432432
433433 try
434434 {
435- byte [ ] tbuf = _in . ReadBytes ( longByteStride * size ) ;
435+ byte [ ] tbuf = ReadBytesExactly ( longByteStride * size ) ;
436436#if NETSTANDARD2_0
437437 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += longByteStride )
438438 {
@@ -460,7 +460,7 @@ public override int Read(float[] buf, int offset, int size)
460460
461461 try
462462 {
463- byte [ ] tbuf = _in . ReadBytes ( floatByteStride * size ) ;
463+ byte [ ] tbuf = ReadBytesExactly ( floatByteStride * size ) ;
464464#if NETSTANDARD2_0
465465 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += floatByteStride )
466466 {
@@ -488,7 +488,7 @@ public override int Read(double[] buf, int offset, int size)
488488
489489 try
490490 {
491- byte [ ] tbuf = _in . ReadBytes ( doubleByteStride * size ) ;
491+ byte [ ] tbuf = ReadBytesExactly ( doubleByteStride * size ) ;
492492#if NETSTANDARD2_0
493493 for ( int b = 0 ; b < tbuf . Length ; ++ nRead , b += doubleByteStride )
494494 {
@@ -1049,5 +1049,33 @@ public override void Close()
10491049 protected byte [ ] _garbageBuf ;
10501050 protected int primitiveArrayCount ;
10511051 #endregion
1052+
1053+ #region Helper Methods
1054+ /// <summary>
1055+ /// Reads exactly the specified number of bytes from the underlying stream.
1056+ /// Throws EndOfStreamException if fewer bytes are available.
1057+ /// </summary>
1058+ /// <param name="count">The number of bytes to read.</param>
1059+ /// <returns>A byte array containing exactly count bytes.</returns>
1060+ private byte [ ] ReadBytesExactly ( int count )
1061+ {
1062+ byte [ ] buffer = new byte [ count ] ;
1063+ #if NETSTANDARD2_0
1064+ int totalRead = 0 ;
1065+ while ( totalRead < count )
1066+ {
1067+ int bytesRead = _s . Read ( buffer , totalRead , count - totalRead ) ;
1068+ if ( bytesRead = = 0 )
1069+ {
1070+ throw new EndOfStreamException ( $"Unable to read { count } bytes from stream . Only { totalRead } bytes were available . ") ;
1071+ }
1072+ totalRead += bytesRead ;
1073+ }
1074+ #else
1075+ _s . ReadExactly ( buffer ) ;
1076+ #endif
1077+ return buffer;
1078+ }
1079+ #endregion
10521080 }
10531081}
0 commit comments