@@ -404,14 +404,16 @@ public bool TryReadByteArray(Span<byte> buff, int len)
404404 // Every time you call this method increment the offset and decrease len by the value of totalRead
405405 public bool TryReadByteArray( Span < byte > buff , int len , out int totalRead )
406406 {
407+ #if NETFRAMEWORK
407408 TdsParser. ReliabilitySection . Assert ( "unreliable call to ReadByteArray" ) ; // you need to setup for a thread abort somewhere before you call this method
409+ #endif
408410 totalRead = 0 ;
409411
410412#if DEBUG
411413 if ( _snapshot != null && _snapshot . DoPend ( ) )
412414 {
413415 _networkPacketTaskSource = new TaskCompletionSource< object > ( ) ;
414- Thread . MemoryBarrier ( ) ;
416+ Interlocked . MemoryBarrier ( ) ;
415417
416418 if ( s_forcePendingReadsToWaitForUser )
417419 {
@@ -463,15 +465,17 @@ public bool TryReadByteArray(Span<byte> buff, int len, out int totalRead)
463465 // before the byte is returned.
464466 internal bool TryReadByte( out byte value )
465467 {
468+ #if NETFRAMEWORK
466469 TdsParser. ReliabilitySection . Assert ( "unreliable call to ReadByte") ; // you need to setup for a thread abort somewhere before you call this method
470+ #endif
467471 Debug. Assert( _inBytesUsed >= 0 && _inBytesUsed <= _inBytesRead, "ERROR - TDSParser: _inBytesUsed < 0 or _inBytesUsed > _inBytesRead" ) ;
468472 value = 0 ;
469473
470474#if DEBUG
471475 if ( _snapshot != null && _snapshot . DoPend ( ) )
472476 {
473477 _networkPacketTaskSource = new TaskCompletionSource < object > ( ) ;
474- Thread . MemoryBarrier ( ) ;
478+ Interlocked . MemoryBarrier ( ) ;
475479
476480 if ( s_forcePendingReadsToWaitForUser )
477481 {
@@ -510,35 +514,28 @@ internal bool TryReadChar(out char value)
510514 {
511515 Debug . Assert ( _syncOverAsync || ! _asyncReadWithoutSnapshot , "This method is not safe to call when doing sync over async" ) ;
512516
513- byte [ ] buffer ;
514- int offset ;
517+ Span < byte > buffer = stackalloc byte [ 2 ] ;
515518 if ( ( ( _inBytesUsed + 2 ) > _inBytesRead ) || ( _inBytesPacket < 2 ) )
516519 {
517520 // If the char isn't fully in the buffer, or if it isn't fully in the packet,
518521 // then use ReadByteArray since the logic is there to take care of that.
519- if ( ! TryReadByteArray ( _bTmp , 2 ) )
522+ if ( ! TryReadByteArray ( buffer , 2 ) )
520523 {
521524 value = '\0 ' ;
522525 return false ;
523526 }
524-
525- buffer = _bTmp ;
526- offset = 0 ;
527527 }
528528 else
529529 {
530530 // The entire char is in the packet and in the buffer, so just return it
531531 // and take care of the counters.
532-
533- buffer = _inBuff ;
534- offset = _inBytesUsed ;
535-
532+ buffer = _inBuff . AsSpan ( _inBytesUsed , 2 ) ;
536533 _inBytesUsed += 2 ;
537534 _inBytesPacket -= 2 ;
538535 }
539536
540537 AssertValidState ( ) ;
541- value = ( char ) ( ( buffer [ offset + 1 ] << 8 ) + buffer [ offset ] ) ;
538+ value = ( char ) ( ( buffer [ 1 ] << 8 ) + buffer [ 0 ] ) ;
542539
543540 return true ;
544541 }
0 commit comments