@@ -34,21 +34,33 @@ public bool Equals(Asn1Object other)
34
34
public static Asn1Object FromByteArray (
35
35
byte [ ] data )
36
36
{
37
- try
38
- {
39
- MemoryStream input = new MemoryStream ( data , false ) ;
40
- Asn1InputStream asn1 = new Asn1InputStream ( input , data . Length ) ;
41
- Asn1Object result = asn1 . ReadObject ( ) ;
42
- if ( input . Position != input . Length )
43
- throw new IOException ( "extra data found after object" ) ;
44
- return result ;
45
- }
46
- catch ( InvalidCastException )
47
- {
48
- throw new IOException ( "cannot recognise object in byte array" ) ;
49
- }
37
+ return FromByteArray ( new ArraySegment < byte > ( data , 0 , data . Length ) ) ;
50
38
}
51
39
40
+ /// <summary>Create a base ASN.1 object from a byte array segment.</summary>
41
+ /// <param name="data">The byte array segment to parse.</param>
42
+ /// <returns>The base ASN.1 object represented by the byte array.</returns>
43
+ /// <exception cref="IOException">
44
+ /// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
45
+ /// </exception>
46
+ public static Asn1Object FromByteArray (
47
+ ArraySegment < byte > data )
48
+ {
49
+ try
50
+ {
51
+ MemoryStream input = new MemoryStream ( data . Array ?? new byte [ 0 ] , data . Offset , data . Count , false ) ;
52
+ Asn1InputStream asn1 = new Asn1InputStream ( input , data . Count ) ;
53
+ Asn1Object result = asn1 . ReadObject ( ) ;
54
+ if ( input . Position != input . Length )
55
+ throw new IOException ( "extra data found after object" ) ;
56
+ return result ;
57
+ }
58
+ catch ( InvalidCastException )
59
+ {
60
+ throw new IOException ( "cannot recognise object in byte array" ) ;
61
+ }
62
+ }
63
+
52
64
/// <summary>Read a base ASN.1 object from a stream.</summary>
53
65
/// <param name="inStr">The stream to parse.</param>
54
66
/// <returns>The base ASN.1 object represented by the byte array.</returns>
0 commit comments