@@ -26,9 +26,6 @@ public BlobStream(PageBlobClient pageBlob)
2626 _pageBlob . CreateIfNotExists ( 0 ) ;
2727 }
2828
29- public long WriteCount { get ; private set ; }
30- public long ReadCount { get ; private set ; }
31-
3229 private long BlobLength => _pageBlob . GetProperties ( ) . Value . ContentLength ;
3330
3431 public override bool CanRead => true ;
@@ -112,7 +109,6 @@ public override int Read(byte[] buffer, int offset, int count)
112109 }
113110
114111 Position += bytesRead ;
115- ReadCount ++ ;
116112 return bytesRead ;
117113 }
118114
@@ -129,17 +125,30 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
129125 }
130126
131127 Position += bytesRead ;
132- ReadCount ++ ;
133128 return bytesRead ;
134129 }
135130
136- public override void Write ( byte [ ] buffer , int offset , int count )
131+ private void EnsureCapacity ( long position )
137132 {
138- if ( BlobLength < Position + count )
133+ if ( BlobLength < position )
139134 {
140- var newSize = NextPageAddress ( Position + count ) ;
135+ var newSize = NextPageAddress ( position ) ;
141136 _pageBlob . Resize ( newSize ) ;
142137 }
138+ }
139+
140+ private async Task EnsureCapacityAsync ( long position )
141+ {
142+ if ( BlobLength < position )
143+ {
144+ var newSize = NextPageAddress ( position ) ;
145+ await _pageBlob . ResizeAsync ( newSize ) ;
146+ }
147+ }
148+
149+ public override void Write ( byte [ ] buffer , int offset , int count )
150+ {
151+ EnsureCapacity ( Position + count ) ;
143152
144153 var pageStartAddress = PreviousPageAddress ( Position ) ;
145154 var pageBytes = NextPageAddress ( Position + count ) - pageStartAddress ;
@@ -153,19 +162,19 @@ public override void Write(byte[] buffer, int offset, int count)
153162 using ( var stream = _pageBlob . OpenRead ( false , pageStartAddress ) )
154163 {
155164 _ = stream . Read ( bufferToMerge , 0 , localCount ) ;
156- ReadCount ++ ;
157165 }
158166 }
159167
160168 Buffer . BlockCopy ( buffer , offset , bufferToMerge , offsetInFirstPage , count ) ;
169+
170+ EnsureCapacity ( pageStartAddress + bufferToMerge . Length ) ;
161171
162172 using ( var stream = _pageBlob . OpenWrite ( false , pageStartAddress ) )
163173 {
164174 stream . Write ( bufferToMerge , 0 , bufferToMerge . Length ) ;
165175 stream . Flush ( ) ;
166- WriteCount ++ ;
167176 }
168-
177+
169178 Position += count ;
170179 if ( Position > Length )
171180 {
@@ -175,12 +184,8 @@ public override void Write(byte[] buffer, int offset, int count)
175184
176185 public override async Task WriteAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
177186 {
178- if ( BlobLength < Position + count )
179- {
180- var newSize = NextPageAddress ( Position + count ) ;
181- await _pageBlob . ResizeAsync ( newSize , cancellationToken : cancellationToken ) ;
182- }
183-
187+ //await EnsureCapacityAsync(Position + count);
188+
184189 var pageStartAddress = PreviousPageAddress ( Position ) ;
185190 var pageBytes = NextPageAddress ( Position + count ) - pageStartAddress ;
186191 var offsetInFirstPage = ( int ) ( Position % PageSizeInBytes ) ;
@@ -192,18 +197,18 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc
192197 var localCount = ( int ) ( pageBytes - PageSizeInBytes ) ;
193198 using ( var stream = await _pageBlob . OpenReadAsync ( false , pageStartAddress , cancellationToken : cancellationToken ) )
194199 {
195- _ = stream . Read ( bufferToMerge , 0 , localCount ) ;
196- ReadCount ++ ;
200+ _ = await stream . ReadAsync ( bufferToMerge , 0 , localCount , cancellationToken ) ;
197201 }
198202 }
199203
200204 Buffer . BlockCopy ( buffer , offset , bufferToMerge , offsetInFirstPage , count ) ;
201-
205+
206+ await EnsureCapacityAsync ( pageStartAddress + bufferToMerge . Length ) ;
207+
202208 using ( var stream = await _pageBlob . OpenWriteAsync ( false , pageStartAddress , cancellationToken : cancellationToken ) )
203209 {
204210 await stream . WriteAsync ( bufferToMerge , 0 , bufferToMerge . Length , cancellationToken ) ;
205211 await stream . FlushAsync ( cancellationToken ) ;
206- WriteCount ++ ;
207212 }
208213
209214 Position += count ;
0 commit comments