55
66namespace PolylineAlgorithm . Abstraction ;
77
8+ using Microsoft . Extensions . Logging ;
89using PolylineAlgorithm . Abstraction . Internal ;
910using PolylineAlgorithm . Abstraction . Properties ;
1011using System ;
@@ -48,12 +49,17 @@ public PolylineEncoder(PolylineEncodingOptions options) {
4849 /// </exception>
4950 public TPolyline Encode ( IEnumerable < TCoordinate > coordinates ) {
5051 if ( coordinates is null ) {
52+ Options
53+ . UseLoggerFor < PolylineEncoder < TCoordinate , TPolyline > > ( ) . LogNullArgumentError ( nameof ( coordinates ) ) ;
5154 throw new ArgumentNullException ( nameof ( coordinates ) ) ;
5255 }
5356
5457 int count = GetCount ( coordinates ) ;
5558
5659 if ( count == 0 ) {
60+ Options
61+ . UseLoggerFor < PolylineEncoder < TCoordinate , TPolyline > > ( ) . LogEmptyArgumentError ( nameof ( coordinates ) ) ;
62+
5763 throw new ArgumentException ( ExceptionMessageResource . ArgumentCannotBeEmptyEnumerationMessage , nameof ( coordinates ) ) ;
5864 }
5965
@@ -72,12 +78,16 @@ public TPolyline Encode(IEnumerable<TCoordinate> coordinates) {
7278 . Next ( PolylineEncoding . Normalize ( GetLatitude ( enumerator . Current ) , PolylineEncoding . ValueType . Latitude ) , PolylineEncoding . Normalize ( GetLongitude ( enumerator . Current ) , PolylineEncoding . ValueType . Longitude ) ) ;
7379
7480 if ( GetRemainingBufferSize ( position , buffer . Length ) < GetRequiredLength ( variance ) ) {
81+ Options
82+ . UseLoggerFor < PolylineEncoder < TCoordinate , TPolyline > > ( ) . LogInternalBufferOverflowError ( position , buffer . Length , GetRequiredLength ( variance ) ) ;
7583 throw new InternalBufferOverflowException ( ) ;
7684 }
7785
7886 if ( ! PolylineEncoding . TryWriteValue ( variance . Latitude , ref buffer , ref position )
7987 || ! PolylineEncoding . TryWriteValue ( variance . Longitude , ref buffer , ref position )
8088 ) {
89+ Options
90+ . UseLoggerFor < PolylineEncoder < TCoordinate , TPolyline > > ( ) . LogCannotWriteValueToBufferError ( position ) ;
8191 throw new InvalidOperationException ( ) ;
8292 }
8393
@@ -101,13 +111,16 @@ static int GetRequiredLength(CoordinateVariance variance) =>
101111
102112 int GetBufferLength ( int count ) {
103113 int maxBufferLength = Options . BufferSize / sizeof ( char ) ;
104- int ? requestedBufferLength = count * Defaults . Polyline . MaxEncodedCoordinateLength ;
114+ int requestedBufferLength = count * Defaults . Polyline . MaxEncodedCoordinateLength ;
115+
116+ if ( requestedBufferLength > maxBufferLength ) {
117+ Options
118+ . UseLoggerFor < PolylineEncoder < TCoordinate , TPolyline > > ( ) . LogRequestedBufferSizeExceedsMaxBufferLength ( requestedBufferLength , maxBufferLength ) ;
105119
106- if ( requestedBufferLength is null || requestedBufferLength > maxBufferLength ) {
107120 return maxBufferLength ;
108121 }
109122
110- return requestedBufferLength . Value ;
123+ return requestedBufferLength ;
111124 }
112125 }
113126
0 commit comments