11using System ;
22using System . IO ;
33using System . Text ;
4- using System . Runtime . InteropServices ;
54using System . Security . Cryptography ;
65using SecureFolderFS . Sdk . Streams ;
76using System . Runtime . CompilerServices ;
@@ -11,56 +10,32 @@ namespace SecureFolderFS.Core.Helpers
1110 internal static class StreamHelpers
1211 {
1312 [ MethodImpl ( MethodImplOptions . Synchronized ) ]
14- public static int ReadToIntPtrBuffer ( IBaseFileStream baseFileStream , IntPtr nativeBuffer , uint bufferLength , long offset )
13+ public unsafe static int ReadToIntPtrBuffer ( IBaseFileStream baseFileStream , IntPtr nativeBuffer , uint bufferLength , long offset )
1514 {
1615 if ( offset >= baseFileStream . Length )
1716 {
1817 return Constants . IO . FILE_EOF ;
1918 }
2019 else
2120 {
22- var readBuffer = new byte [ Constants . IO . READ_BUFFER_SIZE ] ;
23- var position = 0 ;
2421 baseFileStream . Position = offset ;
2522
26- do
27- {
28- var remaining = ( int ) bufferLength - position ;
29- var read = baseFileStream . Read ( readBuffer , 0 , Math . Min ( readBuffer . Length , remaining ) ) ;
23+ var nativeBufferSpan = new Span < byte > ( nativeBuffer . ToPointer ( ) , ( int ) bufferLength ) ;
24+ baseFileStream . Read ( nativeBufferSpan ) ;
3025
31- if ( read == Constants . IO . FILE_EOF )
32- {
33- return position ; // Reached End-of-File EOF
34- }
35- else
36- {
37- Marshal . Copy ( readBuffer , 0 , nativeBuffer + position , read ) ;
38- position += read ;
39- }
40- } while ( position < bufferLength ) ;
41-
42- return position ;
26+ return ( int ) bufferLength ;
4327 }
4428 }
4529
4630 [ MethodImpl ( MethodImplOptions . Synchronized ) ]
4731 public unsafe static int WriteFromIntPtrBuffer ( IBaseFileStream baseFileStream , IntPtr nativeBuffer , uint bufferLength , long offset )
4832 {
49- var position = 0 ;
50-
5133 baseFileStream . Position = offset ;
52- do
53- {
54- var remaining = bufferLength - position ;
55- var writeLength = ( int ) Math . Min ( remaining , Constants . IO . WRITE_BUFFER_SIZE ) ;
56- var writeBuffer = new Span < byte > ( ( nativeBuffer + position ) . ToPointer ( ) , writeLength ) ;
57-
58- baseFileStream . Write ( writeBuffer . Slice ( 0 , writeLength ) ) ;
5934
60- position += writeLength ;
61- } while ( position < bufferLength ) ;
35+ var nativeBufferSpan = new ReadOnlySpan < byte > ( nativeBuffer . ToPointer ( ) , ( int ) bufferLength ) ;
36+ baseFileStream . Write ( nativeBufferSpan ) ;
6237
63- return position ;
38+ return ( int ) bufferLength ;
6439 }
6540
6641 public static void WriteToStream ( Stream sourceStream , Stream destinationStream )
0 commit comments