99 */
1010
1111using System ;
12- using System . Net ;
12+ using System . Buffers . Binary ;
1313
1414namespace HdrHistogram . Utilities
1515{
@@ -108,8 +108,8 @@ public byte Get()
108108 /// <returns>The value of the <see cref="short"/> at the current position.</returns>
109109 public short GetShort ( )
110110 {
111- var shortValue = IPAddress . HostToNetworkOrder ( BitConverter . ToInt16 ( _internalBuffer , Position ) ) ;
112- Position += ( sizeof ( short ) ) ;
111+ var shortValue = BinaryPrimitives . ReadInt16BigEndian ( _internalBuffer . AsSpan ( Position ) ) ;
112+ Position += sizeof ( short ) ;
113113 return shortValue ;
114114 }
115115
@@ -119,7 +119,7 @@ public short GetShort()
119119 /// <returns>The value of the <see cref="int"/> at the current position.</returns>
120120 public int GetInt ( )
121121 {
122- var intValue = IPAddress . HostToNetworkOrder ( BitConverter . ToInt32 ( _internalBuffer , Position ) ) ;
122+ var intValue = BinaryPrimitives . ReadInt32BigEndian ( _internalBuffer . AsSpan ( Position ) ) ;
123123 Position += sizeof ( int ) ;
124124 return intValue ;
125125 }
@@ -130,7 +130,7 @@ public int GetInt()
130130 /// <returns>The value of the <see cref="long"/> at the current position.</returns>
131131 public long GetLong ( )
132132 {
133- var longValue = IPAddress . HostToNetworkOrder ( BitConverter . ToInt64 ( _internalBuffer , Position ) ) ;
133+ var longValue = BinaryPrimitives . ReadInt64BigEndian ( _internalBuffer . AsSpan ( Position ) ) ;
134134 Position += sizeof ( long ) ;
135135 return longValue ;
136136 }
@@ -141,88 +141,9 @@ public long GetLong()
141141 /// <returns>The value of the <see cref="double"/> at the current position.</returns>
142142 public double GetDouble ( )
143143 {
144- var doubleValue = Int64BitsToDouble ( ToInt64 ( _internalBuffer , Position ) ) ;
144+ var longBits = BinaryPrimitives . ReadInt64BigEndian ( _internalBuffer . AsSpan ( Position ) ) ;
145145 Position += sizeof ( double ) ;
146- return doubleValue ;
147- }
148-
149- /// <summary>
150- /// Converts the specified 64-bit signed integer to a double-precision
151- /// floating point number. Note: the endianness of this converter does not
152- /// affect the returned value.
153- /// </summary>
154- /// <param name="value">The number to convert. </param>
155- /// <returns>A double-precision floating point number whose value is equivalent to value.</returns>
156- private static double Int64BitsToDouble ( long value )
157- {
158- return BitConverter . Int64BitsToDouble ( value ) ;
159- }
160-
161- /// <summary>
162- /// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
163- /// </summary>
164- /// <param name="value">An array of bytes.</param>
165- /// <param name="startIndex">The starting position within value.</param>
166- /// <returns>A 64-bit signed integer formed by eight bytes beginning at startIndex.</returns>
167- private static long ToInt64 ( byte [ ] value , int startIndex )
168- {
169- return CheckedFromBytes ( value , startIndex , 8 ) ;
170- }
171-
172- /// <summary>
173- /// Checks the arguments for validity before calling FromBytes
174- /// (which can therefore assume the arguments are valid).
175- /// </summary>
176- /// <param name="value">The bytes to convert after checking</param>
177- /// <param name="startIndex">The index of the first byte to convert</param>
178- /// <param name="bytesToConvert">The number of bytes to convert</param>
179- /// <returns></returns>
180- private static long CheckedFromBytes ( byte [ ] value , int startIndex , int bytesToConvert )
181- {
182- CheckByteArgument ( value , startIndex , bytesToConvert ) ;
183- return FromBytes ( value , startIndex , bytesToConvert ) ;
184- }
185-
186- /// <summary>
187- /// Checks the given argument for validity.
188- /// </summary>
189- /// <param name="value">The byte array passed in</param>
190- /// <param name="startIndex">The start index passed in</param>
191- /// <param name="bytesRequired">The number of bytes required</param>
192- /// <exception cref="ArgumentNullException">value is a null reference</exception>
193- /// <exception cref="ArgumentOutOfRangeException">
194- /// startIndex is less than zero or greater than the length of value minus bytesRequired.
195- /// </exception>
196- private static void CheckByteArgument ( byte [ ] value , int startIndex , int bytesRequired )
197- {
198- #pragma warning disable CA1510
199- if ( value == null )
200- {
201- throw new ArgumentNullException ( nameof ( value ) ) ;
202- }
203- #pragma warning restore CA1510
204- if ( startIndex < 0 || startIndex > value . Length - bytesRequired )
205- {
206- throw new ArgumentOutOfRangeException ( nameof ( startIndex ) ) ;
207- }
208- }
209-
210- /// <summary>
211- /// Returns a value built from the specified number of bytes from the given buffer,
212- /// starting at index.
213- /// </summary>
214- /// <param name="buffer">The data in byte array format</param>
215- /// <param name="startIndex">The first index to use</param>
216- /// <param name="bytesToConvert">The number of bytes to use</param>
217- /// <returns>The value built from the given bytes</returns>
218- private static long FromBytes ( byte [ ] buffer , int startIndex , int bytesToConvert )
219- {
220- long ret = 0 ;
221- for ( int i = 0 ; i < bytesToConvert ; i ++ )
222- {
223- ret = unchecked ( ( ret << 8 ) | buffer [ startIndex + i ] ) ;
224- }
225- return ret ;
146+ return BitConverter . Int64BitsToDouble ( longBits ) ;
226147 }
227148
228149 /// <summary>
@@ -240,9 +161,8 @@ public void Put(byte value)
240161 /// <param name="value">The value to set the current position to.</param>
241162 public void PutInt ( int value )
242163 {
243- var intAsBytes = BitConverter . GetBytes ( IPAddress . NetworkToHostOrder ( value ) ) ;
244- Array . Copy ( intAsBytes , 0 , _internalBuffer , Position , intAsBytes . Length ) ;
245- Position += intAsBytes . Length ;
164+ BinaryPrimitives . WriteInt32BigEndian ( _internalBuffer . AsSpan ( Position ) , value ) ;
165+ Position += sizeof ( int ) ;
246166 }
247167
248168 /// <summary>
@@ -255,8 +175,7 @@ public void PutInt(int value)
255175 /// </remarks>
256176 public void PutInt ( int index , int value )
257177 {
258- var intAsBytes = BitConverter . GetBytes ( IPAddress . NetworkToHostOrder ( value ) ) ;
259- Array . Copy ( intAsBytes , 0 , _internalBuffer , index , intAsBytes . Length ) ;
178+ BinaryPrimitives . WriteInt32BigEndian ( _internalBuffer . AsSpan ( index ) , value ) ;
260179 // We don't increment the Position as this is an explicit write.
261180 }
262181
@@ -266,9 +185,8 @@ public void PutInt(int index, int value)
266185 /// <param name="value">The value to set the current position to.</param>
267186 public void PutLong ( long value )
268187 {
269- var longAsBytes = BitConverter . GetBytes ( IPAddress . NetworkToHostOrder ( value ) ) ;
270- Array . Copy ( longAsBytes , 0 , _internalBuffer , Position , longAsBytes . Length ) ;
271- Position += longAsBytes . Length ;
188+ BinaryPrimitives . WriteInt64BigEndian ( _internalBuffer . AsSpan ( Position ) , value ) ;
189+ Position += sizeof ( long ) ;
272190 }
273191
274192 /// <summary>
@@ -277,11 +195,8 @@ public void PutLong(long value)
277195 /// <param name="value">The value to set the current position to.</param>
278196 public void PutDouble ( double value )
279197 {
280- //PutDouble(ix(CheckIndex(i, (1 << 3))), x);
281- var doubleAsBytes = BitConverter . GetBytes ( value ) ;
282- Array . Reverse ( doubleAsBytes ) ;
283- Array . Copy ( doubleAsBytes , 0 , _internalBuffer , Position , doubleAsBytes . Length ) ;
284- Position += doubleAsBytes . Length ;
198+ BinaryPrimitives . WriteInt64BigEndian ( _internalBuffer . AsSpan ( Position ) , BitConverter . DoubleToInt64Bits ( value ) ) ;
199+ Position += sizeof ( double ) ;
285200 }
286201
287202 /// <summary>
0 commit comments