3
3
4
4
using System . Diagnostics . CodeAnalysis ;
5
5
using System . IO ;
6
+ using System . Runtime . InteropServices ;
6
7
using System . Runtime . Serialization ;
7
8
8
9
namespace System . Xml
@@ -1250,10 +1251,7 @@ private unsafe int ReadArray(bool[] array, int offset, int count)
1250
1251
{
1251
1252
CheckArray ( array , offset , count ) ;
1252
1253
int actual = Math . Min ( count , _arrayCount ) ;
1253
- fixed ( bool * items = & array [ offset ] )
1254
- {
1255
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1256
- }
1254
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1257
1255
SkipArrayElements ( actual ) ;
1258
1256
return actual ;
1259
1257
}
@@ -1276,10 +1274,7 @@ private unsafe int ReadArray(short[] array, int offset, int count)
1276
1274
{
1277
1275
CheckArray ( array , offset , count ) ;
1278
1276
int actual = Math . Min ( count , _arrayCount ) ;
1279
- fixed ( short * items = & array [ offset ] )
1280
- {
1281
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1282
- }
1277
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1283
1278
SkipArrayElements ( actual ) ;
1284
1279
return actual ;
1285
1280
}
@@ -1302,10 +1297,7 @@ private unsafe int ReadArray(int[] array, int offset, int count)
1302
1297
{
1303
1298
CheckArray ( array , offset , count ) ;
1304
1299
int actual = Math . Min ( count , _arrayCount ) ;
1305
- fixed ( int * items = & array [ offset ] )
1306
- {
1307
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1308
- }
1300
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1309
1301
SkipArrayElements ( actual ) ;
1310
1302
return actual ;
1311
1303
}
@@ -1328,10 +1320,7 @@ private unsafe int ReadArray(long[] array, int offset, int count)
1328
1320
{
1329
1321
CheckArray ( array , offset , count ) ;
1330
1322
int actual = Math . Min ( count , _arrayCount ) ;
1331
- fixed ( long * items = & array [ offset ] )
1332
- {
1333
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1334
- }
1323
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1335
1324
SkipArrayElements ( actual ) ;
1336
1325
return actual ;
1337
1326
}
@@ -1354,10 +1343,7 @@ private unsafe int ReadArray(float[] array, int offset, int count)
1354
1343
{
1355
1344
CheckArray ( array , offset , count ) ;
1356
1345
int actual = Math . Min ( count , _arrayCount ) ;
1357
- fixed ( float * items = & array [ offset ] )
1358
- {
1359
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1360
- }
1346
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1361
1347
SkipArrayElements ( actual ) ;
1362
1348
return actual ;
1363
1349
}
@@ -1380,10 +1366,7 @@ private unsafe int ReadArray(double[] array, int offset, int count)
1380
1366
{
1381
1367
CheckArray ( array , offset , count ) ;
1382
1368
int actual = Math . Min ( count , _arrayCount ) ;
1383
- fixed ( double * items = & array [ offset ] )
1384
- {
1385
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1386
- }
1369
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1387
1370
SkipArrayElements ( actual ) ;
1388
1371
return actual ;
1389
1372
}
@@ -1406,10 +1389,7 @@ private unsafe int ReadArray(decimal[] array, int offset, int count)
1406
1389
{
1407
1390
CheckArray ( array , offset , count ) ;
1408
1391
int actual = Math . Min ( count , _arrayCount ) ;
1409
- fixed ( decimal * items = & array [ offset ] )
1410
- {
1411
- BufferReader . UnsafeReadArray ( ( byte * ) items , ( byte * ) & items [ actual ] ) ;
1412
- }
1392
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1413
1393
SkipArrayElements ( actual ) ;
1414
1394
return actual ;
1415
1395
}
@@ -1433,9 +1413,11 @@ private int ReadArray(DateTime[] array, int offset, int count)
1433
1413
{
1434
1414
CheckArray ( array , offset , count ) ;
1435
1415
int actual = Math . Min ( count , _arrayCount ) ;
1436
- for ( int i = 0 ; i < actual ; i ++ )
1416
+ // Try to read in whole array, but don't fail if not possible
1417
+ BufferReader . GetBuffer ( actual * ValueHandleLength . DateTime , out _ , out _ ) ;
1418
+ foreach ( ref DateTime item in array . AsSpan ( offset , actual ) )
1437
1419
{
1438
- array [ offset + i ] = BufferReader . ReadDateTime ( ) ;
1420
+ item = BufferReader . ReadDateTime ( ) ;
1439
1421
}
1440
1422
SkipArrayElements ( actual ) ;
1441
1423
return actual ;
@@ -1460,9 +1442,18 @@ private int ReadArray(Guid[] array, int offset, int count)
1460
1442
{
1461
1443
CheckArray ( array , offset , count ) ;
1462
1444
int actual = Math . Min ( count , _arrayCount ) ;
1463
- for ( int i = 0 ; i < actual ; i ++ )
1445
+ if ( BitConverter . IsLittleEndian )
1464
1446
{
1465
- array [ offset + i ] = BufferReader . ReadGuid ( ) ;
1447
+ BufferReader . ReadRawArrayBytes ( array . AsSpan ( offset , actual ) ) ;
1448
+ }
1449
+ else
1450
+ {
1451
+ // Try to read in whole array, but don't fail if not possible
1452
+ BufferReader . GetBuffer ( actual * ValueHandleLength . Guid , out _ , out _ ) ;
1453
+ foreach ( ref Guid item in array . AsSpan ( offset , actual ) )
1454
+ {
1455
+ item = BufferReader . ReadGuid ( ) ;
1456
+ }
1466
1457
}
1467
1458
SkipArrayElements ( actual ) ;
1468
1459
return actual ;
@@ -1487,9 +1478,11 @@ private int ReadArray(TimeSpan[] array, int offset, int count)
1487
1478
{
1488
1479
CheckArray ( array , offset , count ) ;
1489
1480
int actual = Math . Min ( count , _arrayCount ) ;
1490
- for ( int i = 0 ; i < actual ; i ++ )
1481
+ // Try to read in whole array, but don't fail if not possible
1482
+ BufferReader . GetBuffer ( actual * ValueHandleLength . TimeSpan , out _ , out _ ) ;
1483
+ foreach ( ref TimeSpan item in array . AsSpan ( offset , actual ) )
1491
1484
{
1492
- array [ offset + i ] = BufferReader . ReadTimeSpan ( ) ;
1485
+ item = BufferReader . ReadTimeSpan ( ) ;
1493
1486
}
1494
1487
SkipArrayElements ( actual ) ;
1495
1488
return actual ;
0 commit comments