@@ -67,15 +67,6 @@ internal static ByteString FromBytes(byte[] bytes)
67
67
{
68
68
return new ByteString ( bytes ) ;
69
69
}
70
-
71
- /// <summary>
72
- /// Provides direct, unrestricted access to the bytes contained in this instance.
73
- /// You must not modify or resize the byte array returned by this method.
74
- /// </summary>
75
- internal static byte [ ] GetBuffer ( ByteString bytes )
76
- {
77
- return bytes . bytes ;
78
- }
79
70
}
80
71
81
72
/// <summary>
@@ -119,6 +110,14 @@ public bool IsEmpty
119
110
get { return Length == 0 ; }
120
111
}
121
112
113
+ #if NETSTANDARD2_0
114
+ /// <summary>
115
+ /// Provides read-only access to the data of this <see cref="ByteString"/>.
116
+ /// No data is copied so this is the most efficient way of accessing.
117
+ /// </summary>
118
+ public ReadOnlySpan < byte > Span => new ReadOnlySpan < byte > ( bytes ) ;
119
+ #endif
120
+
122
121
/// <summary>
123
122
/// Converts this <see cref="ByteString"/> into a byte array.
124
123
/// </summary>
@@ -161,7 +160,7 @@ public static ByteString FromStream(Stream stream)
161
160
int capacity = stream . CanSeek ? checked ( ( int ) ( stream . Length - stream . Position ) ) : 0 ;
162
161
var memoryStream = new MemoryStream ( capacity ) ;
163
162
stream . CopyTo ( memoryStream ) ;
164
- #if NETSTANDARD1_0
163
+ #if NETSTANDARD1_0 || NETSTANDARD2_0
165
164
byte [ ] bytes = memoryStream . ToArray ( ) ;
166
165
#else
167
166
// Avoid an extra copy if we can.
@@ -187,7 +186,7 @@ public static ByteString FromStream(Stream stream)
187
186
// We have to specify the buffer size here, as there's no overload accepting the cancellation token
188
187
// alone. But it's documented to use 81920 by default if not specified.
189
188
await stream . CopyToAsync ( memoryStream , 81920 , cancellationToken ) ;
190
- #if NETSTANDARD1_0
189
+ #if NETSTANDARD1_0 || NETSTANDARD2_0
191
190
byte [ ] bytes = memoryStream . ToArray ( ) ;
192
191
#else
193
192
// Avoid an extra copy if we can.
@@ -219,6 +218,18 @@ public static ByteString CopyFrom(byte[] bytes, int offset, int count)
219
218
return new ByteString ( portion ) ;
220
219
}
221
220
221
+ #if NETSTANDARD2_0
222
+ /// <summary>
223
+ /// Constructs a <see cref="ByteString" /> from a read only span. The contents
224
+ /// are copied, so further modifications to the span will not
225
+ /// be reflected in the returned <see cref="ByteString" />.
226
+ /// </summary>
227
+ public static ByteString CopyFrom ( ReadOnlySpan < byte > bytes )
228
+ {
229
+ return new ByteString ( bytes . ToArray ( ) ) ;
230
+ }
231
+ #endif
232
+
222
233
/// <summary>
223
234
/// Creates a new <see cref="ByteString" /> by encoding the specified text with
224
235
/// the given encoding.
0 commit comments