77namespace nanoFramework . MessagePack . Dto
88{
99 /// <summary>
10- /// Segment by bytes array
10+ /// Segment by byte array.
1111 /// </summary>
1212 public class ArraySegment : IEnumerable , IEnumerator
1313 {
@@ -16,11 +16,39 @@ public class ArraySegment : IEnumerable, IEnumerator
1616 private readonly long _length ;
1717
1818 /// <summary>
19- /// Segment constructor
19+ /// Gets the current position in the segment.
2020 /// </summary>
21- /// <param name="buffer">Source byte array</param>
22- /// <param name="offset">Offset in source byte array</param>
23- /// <param name="length">Length segment</param>
21+ public long Position { get ; private set ; } = - 1 ;
22+
23+ /// <summary>
24+ /// Gets the element corresponding to the current position in the segment.
25+ /// </summary>
26+ public object Current
27+ {
28+ get
29+ {
30+ if ( Position >= _length )
31+ {
32+ throw ExceptionUtility . NotEnoughBytes ( Position , _length ) ;
33+ }
34+
35+ try
36+ {
37+ return _buffer [ _offset + Position ] ;
38+ }
39+ catch
40+ {
41+ throw ExceptionUtility . NotEnoughBytes ( _offset + Position , _buffer . Length ) ;
42+ }
43+ }
44+ }
45+
46+ /// <summary>
47+ /// Initializes a new instance of the <see cref="ArraySegment" /> class.
48+ /// </summary>
49+ /// <param name="buffer">Source byte array.</param>
50+ /// <param name="offset">Offset in source byte array.</param>
51+ /// <param name="length">Length segment.</param>
2452 public ArraySegment ( byte [ ] buffer , long offset , long length )
2553 {
2654 _buffer = buffer ;
@@ -29,9 +57,27 @@ public ArraySegment(byte[] buffer, long offset, long length)
2957 }
3058
3159 /// <summary>
32- /// Rad one byte in segment
60+ /// Implicit conversion from byte array to <see cref="ArraySegment"/>.
3361 /// </summary>
34- /// <returns>Reading byte</returns>
62+ /// <param name="bytes" Source byte array.</param>
63+ public static implicit operator ArraySegment ( byte [ ] bytes )
64+ {
65+ return new ArraySegment ( bytes , 0 , bytes . Length ) ;
66+ }
67+
68+ /// <summary>
69+ /// Implicit conversion from <see cref="ArraySegment"/> to byte array.
70+ /// </summary>
71+ /// <param name="segment">Source <see cref="ArraySegment"/>.</param>
72+ public static explicit operator byte [ ] ( ArraySegment segment )
73+ {
74+ return segment . ToArray ( ) ;
75+ }
76+
77+ /// <summary>.
78+ /// Read one byte from the segment.
79+ /// </summary>
80+ /// <returns>The byte read.</returns>
3581 public byte ReadByte ( )
3682 {
3783 if ( ++ Position >= _length )
@@ -48,18 +94,18 @@ public byte ReadByte()
4894 }
4995
5096 /// <summary>
51- /// Get bytes enumerator
97+ /// Get bytes enumerator.
5298 /// </summary>
53- /// <returns></returns>
99+ /// <returns>Enumerator for bytes in segment. </returns>
54100 public IEnumerator GetEnumerator ( )
55101 {
56102 return this ;
57103 }
58104
59105 /// <summary>
60- /// Go to next byte in array
106+ /// Go to next byte in array.
61107 /// </summary>
62- /// <returns>True if the end of the segment is not reached otherwise false</returns>
108+ /// <returns><see langword="true"/> if the end of the segment is not reached otherwise <see langword=" false"/>. </returns>
63109 public bool MoveNext ( )
64110 {
65111 Position ++ ;
@@ -68,69 +114,24 @@ public bool MoveNext()
68114 }
69115
70116 /// <summary>
71- /// Resets the current position to the beginning of the segment
117+ /// Resets the current position to the beginning of the segment.
72118 /// </summary>
73119 public void Reset ( )
74120 {
75121 Position = - 1 ;
76122 }
77123
78- /// <summary>
79- /// Current position of the segment
80- /// </summary>
81- public long Position { get ; private set ; } = - 1 ;
82-
83- /// <summary>
84- /// The element corresponding to the current position in the segment
85- /// </summary>
86- public object Current
87- {
88- get
89- {
90- if ( Position >= _length )
91- {
92- throw ExceptionUtility . NotEnoughBytes ( Position , _length ) ;
93- }
94-
95- try
96- {
97- return _buffer [ _offset + Position ] ;
98- }
99- catch
100- {
101- throw ExceptionUtility . NotEnoughBytes ( _offset + Position , _buffer . Length ) ;
102- }
103- }
104- }
105-
106124 private byte [ ] ToArray ( )
107125 {
108126 var data = new byte [ _length ] ;
109- //for (int i = 0; i < data.Length; i++)
110- //{
111- // data[i] = _buffer[_offset + i];
112- //}
113- System . Array . Copy ( _buffer , ( int ) _offset , data , 0 , ( int ) _length ) ;
114- return data ;
115- }
116127
117- /// <summary>
118- /// Implicit conversion from byte array to <see cref="ArraySegment"/>.
119- /// </summary>
120- /// <param name="bytes"></param>
121- public static implicit operator ArraySegment ( byte [ ] bytes )
122- {
123- return new ArraySegment ( bytes , 0 , bytes . Length ) ;
124- }
128+ ////for (int i = 0; i < data.Length; i++)
129+ ////{
130+ //// data[i] = _buffer[_offset + i];
131+ ////}
125132
126- /// <summary>
127- /// Implicit conversion from <see cref="ArraySegment"/> to byte array.
128- /// </summary>
129- /// <param name="segment"></param>
130- public static explicit operator byte [ ] ( ArraySegment segment )
131- {
132- return segment . ToArray ( ) ;
133+ System . Array . Copy ( _buffer , ( int ) _offset , data , 0 , ( int ) _length ) ;
134+ return data ;
133135 }
134-
135136 }
136137}
0 commit comments