Skip to content

Commit a6a03d8

Browse files
committed
Use try/finally for secondary compressor disposal
1 parent c9a4edc commit a6a03d8

1 file changed

Lines changed: 87 additions & 76 deletions

File tree

src/VCDiff/Decoders/VcDecoderEx.cs

Lines changed: 87 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -191,54 +191,60 @@ public VCDiffResult Decode(out long bytesWritten)
191191
return result;
192192

193193
var secondaryCompressor = SecondaryCompressorId != 0 ? CreateCompressor(SecondaryCompressorId): null;
194-
while (delta.CanRead)
194+
try
195195
{
196-
//delta is streamed in order aka not random access
197-
using var w = new WindowDecoder<TDeltaBuffer>(source.Length, delta, secondaryCompressor, maxTargetFileSize);
198-
199-
if (!w.Decode(this.IsSDCHFormat, this.SecondaryCompressorId))
196+
while (delta.CanRead)
200197
{
201-
return (VCDiffResult)w.Result;
202-
}
198+
//delta is streamed in order aka not random access
199+
using var w = new WindowDecoder<TDeltaBuffer>(source.Length, delta, secondaryCompressor, maxTargetFileSize);
203200

204-
using var body = new BodyDecoder<TDeltaBuffer, TSourceBuffer, TDeltaBuffer>(w, source, delta, outputStream, disableChecksums: disableChecksums);
205-
if (this.IsSDCHFormat && w.AddRunLength == 0 && w.AddressesForCopyLength == 0 && w.InstructionAndSizesLength > 0)
206-
{
207-
//interleaved
208-
//decodedinterleave actually has an internal loop for waiting and streaming the incoming rest of the interleaved window
209-
result = body.DecodeInterleave();
201+
if (!w.Decode(this.IsSDCHFormat, this.SecondaryCompressorId))
202+
{
203+
return (VCDiffResult)w.Result;
204+
}
210205

211-
if (result != VCDiffResult.SUCCESS && result != VCDiffResult.EOD)
212-
return result;
206+
using var body = new BodyDecoder<TDeltaBuffer, TSourceBuffer, TDeltaBuffer>(w, source, delta, outputStream, disableChecksums: disableChecksums);
207+
if (this.IsSDCHFormat && w.AddRunLength == 0 && w.AddressesForCopyLength == 0 && w.InstructionAndSizesLength > 0)
208+
{
209+
//interleaved
210+
//decodedinterleave actually has an internal loop for waiting and streaming the incoming rest of the interleaved window
211+
result = body.DecodeInterleave();
213212

214-
bytesWritten += body.TotalBytesDecoded;
215-
}
216-
//technically add could be 0 if it is all copy instructions
217-
//so do an or check on those two
218-
else if (!this.IsSDCHFormat ||
219-
(this.IsSDCHFormat && (w.AddRunLength > 0 || w.AddressesForCopyLength > 0) &&
220-
w.InstructionAndSizesLength > 0))
221-
{
222-
//not interleaved
223-
//expects the full window to be available
224-
//in the stream
225-
result = body.Decode();
226-
if (result != VCDiffResult.SUCCESS)
227-
return result;
228-
229-
bytesWritten += body.TotalBytesDecoded;
213+
if (result != VCDiffResult.SUCCESS && result != VCDiffResult.EOD)
214+
return result;
215+
216+
bytesWritten += body.TotalBytesDecoded;
217+
}
218+
//technically add could be 0 if it is all copy instructions
219+
//so do an or check on those two
220+
else if (!this.IsSDCHFormat ||
221+
(this.IsSDCHFormat && (w.AddRunLength > 0 || w.AddressesForCopyLength > 0) &&
222+
w.InstructionAndSizesLength > 0))
223+
{
224+
//not interleaved
225+
//expects the full window to be available
226+
//in the stream
227+
result = body.Decode();
228+
if (result != VCDiffResult.SUCCESS)
229+
return result;
230+
231+
bytesWritten += body.TotalBytesDecoded;
232+
}
233+
else
234+
{
235+
//invalid file
236+
return VCDiffResult.ERROR;
237+
}
230238
}
231-
else
239+
}
240+
finally
241+
{
242+
if (secondaryCompressor is IDisposable secondaryCompressorDisposable)
232243
{
233-
//invalid file
234-
return VCDiffResult.ERROR;
244+
secondaryCompressorDisposable.Dispose();
235245
}
236246
}
237247

238-
if (secondaryCompressor is IDisposable secondaryCompressorDisposable)
239-
{
240-
secondaryCompressorDisposable.Dispose();
241-
}
242248

243249
return result;
244250
}
@@ -255,55 +261,60 @@ public VCDiffResult Decode(out long bytesWritten)
255261
return decodeAsync;
256262

257263
var secondaryCompressor = SecondaryCompressorId != 0 ? CreateCompressor(SecondaryCompressorId) : null;
258-
while (delta.CanRead)
264+
try
259265
{
260-
//delta is streamed in order aka not random access
261-
using var w = new WindowDecoder<TDeltaBuffer>(source.Length, delta, secondaryCompressor, maxTargetFileSize);
262-
263-
if (w.Decode(this.IsSDCHFormat, this.SecondaryCompressorId))
266+
while (delta.CanRead)
264267
{
265-
using var body = new BodyDecoder<TDeltaBuffer, TSourceBuffer, TDeltaBuffer>(w, source, delta, outputStream, disableChecksums: disableChecksums);
266-
if (this.IsSDCHFormat && w.AddRunLength == 0 && w.AddressesForCopyLength == 0 && w.InstructionAndSizesLength > 0)
267-
{
268-
//interleaved
269-
//decodedinterleave actually has an internal loop for waiting and streaming the incoming rest of the interleaved window
270-
result = await body.DecodeInterleaveAsync();
271-
272-
if (result != VCDiffResult.SUCCESS && result != VCDiffResult.EOD)
273-
return (result, bytesWritten);
268+
//delta is streamed in order aka not random access
269+
using var w = new WindowDecoder<TDeltaBuffer>(source.Length, delta, secondaryCompressor, maxTargetFileSize);
274270

275-
bytesWritten += body.TotalBytesDecoded;
276-
}
277-
//technically add could be 0 if it is all copy instructions
278-
//so do an or check on those two
279-
else if (!this.IsSDCHFormat || (this.IsSDCHFormat && (w.AddRunLength > 0 || w.AddressesForCopyLength > 0) &&
280-
w.InstructionAndSizesLength > 0))
271+
if (w.Decode(this.IsSDCHFormat, this.SecondaryCompressorId))
281272
{
282-
//not interleaved
283-
//expects the full window to be available
284-
//in the stream
285-
result = await body.DecodeAsync();
286-
287-
if (result != VCDiffResult.SUCCESS)
288-
return (result, bytesWritten);
289-
290-
bytesWritten += body.TotalBytesDecoded;
273+
using var body = new BodyDecoder<TDeltaBuffer, TSourceBuffer, TDeltaBuffer>(w, source, delta, outputStream, disableChecksums: disableChecksums);
274+
if (this.IsSDCHFormat && w.AddRunLength == 0 && w.AddressesForCopyLength == 0 && w.InstructionAndSizesLength > 0)
275+
{
276+
//interleaved
277+
//decodedinterleave actually has an internal loop for waiting and streaming the incoming rest of the interleaved window
278+
result = await body.DecodeInterleaveAsync();
279+
280+
if (result != VCDiffResult.SUCCESS && result != VCDiffResult.EOD)
281+
return (result, bytesWritten);
282+
283+
bytesWritten += body.TotalBytesDecoded;
284+
}
285+
//technically add could be 0 if it is all copy instructions
286+
//so do an or check on those two
287+
else if (!this.IsSDCHFormat || (this.IsSDCHFormat && (w.AddRunLength > 0 || w.AddressesForCopyLength > 0) &&
288+
w.InstructionAndSizesLength > 0))
289+
{
290+
//not interleaved
291+
//expects the full window to be available
292+
//in the stream
293+
result = await body.DecodeAsync();
294+
295+
if (result != VCDiffResult.SUCCESS)
296+
return (result, bytesWritten);
297+
298+
bytesWritten += body.TotalBytesDecoded;
299+
}
300+
else
301+
{
302+
//invalid file
303+
return (VCDiffResult.ERROR, bytesWritten);
304+
}
291305
}
292306
else
293307
{
294-
//invalid file
295-
return (VCDiffResult.ERROR, bytesWritten);
308+
return ((VCDiffResult)w.Result, bytesWritten);
296309
}
297310
}
298-
else
299-
{
300-
return ((VCDiffResult)w.Result, bytesWritten);
301-
}
302311
}
303-
304-
if (secondaryCompressor is IDisposable secondaryCompressorDisposable)
312+
finally
305313
{
306-
secondaryCompressorDisposable.Dispose();
314+
if (secondaryCompressor is IDisposable secondaryCompressorDisposable)
315+
{
316+
secondaryCompressorDisposable.Dispose();
317+
}
307318
}
308319

309320
return (result, bytesWritten);

0 commit comments

Comments
 (0)