11using System ;
2+ using System . Buffers ;
23using System . IO ;
34using System . Text ;
45
6+ #nullable enable
57
68namespace SatorImaging . TarArchiver
79{
810 public class TarStream : IDisposable
911 {
10- Stream m_outputStream ;
12+ readonly Stream m_outputStream ;
1113
1214 public TarStream ( Stream output )
1315 {
@@ -17,7 +19,7 @@ public TarStream(Stream output)
1719 public void Dispose ( )
1820 {
1921 /// Indicates if archive should be finalized (by 2 empty blocks) on close.
20- m_outputStream . Write ( new byte [ 1024 ] ) ;
22+ m_outputStream . Write ( stackalloc byte [ 1024 ] ) ;
2123 m_outputStream . Flush ( ) ;
2224 // parent stream keep open for further op
2325 //m_outputStream.Close();
@@ -37,11 +39,11 @@ public void Write(string filename, string source, DateTime modificationTime, lon
3739 public void Write ( string filename , byte [ ] source ) => Write ( filename , source , DateTime . Now ) ;
3840 public void Write ( string filename , byte [ ] source , DateTime modificationTime , long size = - 1 )
3941 {
40- using ( var stream = new MemoryStream ( ) )
42+ using ( var stream = new MemoryStream ( source ) )
4143 {
42- stream . Write ( source ) ;
43- stream . Flush ( ) ;
44- stream . Position = 0 ;
44+ // stream.Write(source);
45+ // stream.Flush();
46+ // stream.Position = 0;
4547 Write ( filename , stream , modificationTime , size ) ;
4648 }
4749 }
@@ -56,7 +58,7 @@ public void Write(string filename, Stream source, DateTime modificationTime, lon
5658
5759 long realSize = size < 0 ? source . Length : size ;
5860
59- TarHeader header = new TarHeader ( ) ;
61+ TarHeader header = new ( ) ;
6062
6163 header . LastModifiedTime = modificationTime ;
6264 header . Name = NormalizeFilename ( filename ) ;
@@ -68,17 +70,15 @@ public void Write(string filename, Stream source, DateTime modificationTime, lon
6870 }
6971
7072
71-
7273 #region //////// Utility ////////
7374
74-
7575 void PadTo512 ( long size )
7676 {
7777 int zeros = unchecked ( ( int ) ( ( ( size + 511L ) & ~ 511L ) - size ) ) ;
78- m_outputStream . Write ( new byte [ zeros ] ) ;
78+ m_outputStream . Write ( stackalloc byte [ zeros ] ) ;
7979 }
8080
81- string NormalizeFilename ( string filename )
81+ static string NormalizeFilename ( string filename )
8282 {
8383 filename = filename . Replace ( '\\ ' , '/' ) ;
8484
@@ -92,21 +92,19 @@ string NormalizeFilename(string filename)
9292 }
9393
9494
95- static byte [ ] GetTransferByteArray ( ) => new byte [ 81920 ] ; //ArrayPool<byte>.Shared.Rent(81920);
96-
9795 static bool ReadTransferBlock ( Stream source , byte [ ] array , out int count ) =>
9896 ( count = source . Read ( array , 0 , array . Length ) ) != 0 ;
9997
10098 static long TransferTo ( /*this*/ Stream source , Stream destination )
10199 {
102- var array = GetTransferByteArray ( ) ;
100+ var rental_array = ArrayPool < byte > . Shared . Rent ( 8192 ) ;
103101 try
104102 {
105103 long total = 0 ;
106- while ( ReadTransferBlock ( source , array , out var count ) )
104+ while ( ReadTransferBlock ( source , rental_array , out var count ) )
107105 {
108106 total += count ;
109- destination . Write ( array , 0 , count ) ;
107+ destination . Write ( rental_array , 0 , count ) ;
110108 }
111109 return total ;
112110 }
@@ -116,13 +114,10 @@ static long TransferTo(/*this*/ Stream source, Stream destination)
116114 }
117115 finally
118116 {
119- // ArrayPool<byte>.Shared.Return(array );
117+ ArrayPool < byte > . Shared . Return ( rental_array ) ;
120118 }
121119 }
122120
123-
124121 #endregion
125-
126-
127122 }
128123}
0 commit comments