@@ -213,7 +213,7 @@ public virtual Blob CreateBlob(Stream stream, string hintpath, long numberOfByte
213213 return CreateBlob ( stream , hintpath , ( long ? ) numberOfBytesToConsume ) ;
214214 }
215215
216- private Blob CreateBlob ( Stream stream , string hintpath , long ? numberOfBytesToConsume )
216+ private unsafe Blob CreateBlob ( Stream stream , string hintpath , long ? numberOfBytesToConsume )
217217 {
218218 Ensure . ArgumentNotNull ( stream , "stream" ) ;
219219
@@ -228,9 +228,51 @@ private Blob CreateBlob(Stream stream, string hintpath, long? numberOfBytesToCon
228228 throw new ArgumentException ( "The stream cannot be read from." , "stream" ) ;
229229 }
230230
231- var proc = new Processor ( stream , numberOfBytesToConsume ) ;
232- ObjectId id = Proxy . git_blob_create_fromchunks ( repo . Handle , hintpath , proc . Provider ) ;
231+ IntPtr writestream_ptr = Proxy . git_blob_create_fromstream ( repo . Handle , hintpath ) ;
232+ GitWriteStream writestream = ( GitWriteStream ) Marshal . PtrToStructure ( writestream_ptr , typeof ( GitWriteStream ) ) ;
233233
234+ try
235+ {
236+ var buffer = new byte [ 4 * 1024 ] ;
237+ long totalRead = 0 ;
238+ int read = 0 ;
239+
240+ while ( true )
241+ {
242+ int toRead = numberOfBytesToConsume . HasValue ?
243+ ( int ) Math . Min ( numberOfBytesToConsume . Value - totalRead , ( long ) buffer . Length ) :
244+ buffer . Length ;
245+
246+ if ( toRead > 0 )
247+ {
248+ read = ( toRead > 0 ) ? stream . Read ( buffer , 0 , toRead ) : 0 ;
249+ }
250+
251+ if ( read == 0 )
252+ {
253+ break ;
254+ }
255+
256+ fixed ( byte * buffer_ptr = buffer )
257+ {
258+ writestream . write ( writestream_ptr , ( IntPtr ) buffer_ptr , ( UIntPtr ) read ) ;
259+ }
260+
261+ totalRead += read ;
262+ }
263+
264+ if ( numberOfBytesToConsume . HasValue && totalRead < numberOfBytesToConsume . Value )
265+ {
266+ throw new EndOfStreamException ( "The stream ended unexpectedly" ) ;
267+ }
268+ }
269+ catch ( Exception e )
270+ {
271+ writestream . free ( writestream_ptr ) ;
272+ throw e ;
273+ }
274+
275+ ObjectId id = Proxy . git_blob_create_fromstream_commit ( writestream_ptr ) ;
234276 return repo . Lookup < Blob > ( id ) ;
235277 }
236278
0 commit comments