1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Ionic.Crc;
namespace Ionic.Zlib
{
public class ParallelDeflateOutputStream : Stream
{
public CompressionStrategy Strategy { get; private set; }
public int MaxBufferPairs
{
get
{
return this._maxBufferPairs;
}
set
{
bool flag = value < 4;
if (flag)
{
throw new ArgumentException("MaxBufferPairs", "Value must be 4 or greater.");
}
this._maxBufferPairs = value;
}
}
public int BufferSize
{
get
{
return this._bufferSize;
}
set
{
bool flag = value < 1024;
if (flag)
{
throw new ArgumentOutOfRangeException("BufferSize", "BufferSize must be greater than 1024 bytes");
}
this._bufferSize = value;
}
}
public int Crc32
{
get
{
return this._Crc32;
}
}
public long BytesProcessed
{
get
{
return this._totalBytesProcessed;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return this._outStream.CanWrite;
}
}
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Position
{
get
{
return this._outStream.Position;
}
set
{
throw new NotSupportedException();
}
}
private static readonly int IO_BUFFER_SIZE_DEFAULT = 65536;
private static readonly int BufferPairsPerCore = 4;
private List<WorkItem> _pool;
private bool _leaveOpen;
private bool emitting;
private Stream _outStream;
private int _maxBufferPairs;
private int _bufferSize = ParallelDeflateOutputStream.IO_BUFFER_SIZE_DEFAULT;
private AutoResetEvent _newlyCompressedBlob;
private object _outputLock = new object();
private bool _isClosed;
private bool _firstWriteDone;
private int _currentlyFilling;
private int _lastFilled;
private int _lastWritten;
private int _latestCompressed;
private int _Crc32;
private CRC32 _runningCrc;
private object _latestLock = new object();
private Queue<int> _toWrite;
private Queue<int> _toFill;
private long _totalBytesProcessed;
private CompressionLevel _compressLevel;
private volatile Exception _pendingException;
private bool _handlingException;
private object _eLock = new object();
private ParallelDeflateOutputStream.TraceBits _DesiredTrace = ParallelDeflateOutputStream.TraceBits.EmitLock | ParallelDeflateOutputStream.TraceBits.EmitEnter | ParallelDeflateOutputStream.TraceBits.EmitBegin | ParallelDeflateOutputStream.TraceBits.EmitDone | ParallelDeflateOutputStream.TraceBits.EmitSkip | ParallelDeflateOutputStream.TraceBits.Session | ParallelDeflateOutputStream.TraceBits.Compress | ParallelDeflateOutputStream.TraceBits.WriteEnter | ParallelDeflateOutputStream.TraceBits.WriteTake;
[Flags]
private enum TraceBits : uint
{
None = 0u,
NotUsed1 = 1u,
EmitLock = 2u,
EmitEnter = 4u,
EmitBegin = 8u,
EmitDone = 16u,
EmitSkip = 32u,
EmitAll = 58u,
Flush = 64u,
Lifecycle = 128u,
Session = 256u,
Synch = 512u,
Instance = 1024u,
Compress = 2048u,
Write = 4096u,
WriteEnter = 8192u,
WriteTake = 16384u,
All = 4294967295u
}
public ParallelDeflateOutputStream(Stream stream) : this(stream, CompressionLevel.Default, CompressionStrategy.Default, false)
{
}
public ParallelDeflateOutputStream(Stream stream, CompressionLevel level) : this(stream, level, CompressionStrategy.Default, false)
{
}
public ParallelDeflateOutputStream(Stream stream, bool leaveOpen) : this(stream, CompressionLevel.Default, CompressionStrategy.Default, leaveOpen)
{
}
public ParallelDeflateOutputStream(Stream stream, CompressionLevel level, bool leaveOpen) : this(stream, CompressionLevel.Default, CompressionStrategy.Default, leaveOpen)
{
}
public ParallelDeflateOutputStream(Stream stream, CompressionLevel level, CompressionStrategy strategy, bool leaveOpen)
{
this._outStream = stream;
this._compressLevel = level;
this.Strategy = strategy;
this._leaveOpen = leaveOpen;
this.MaxBufferPairs = 16;
}
private void _InitializePoolOfWorkItems()
{
this._toWrite = new Queue<int>();
this._toFill = new Queue<int>();
this._pool = new List<WorkItem>();
int num = ParallelDeflateOutputStream.BufferPairsPerCore * Environment.ProcessorCount;
num = Math.Min(num, this._maxBufferPairs);
for (int i = 0; i < num; i++)
{
this._pool.Add(new WorkItem(this._bufferSize, this._compressLevel, this.Strategy, i));
this._toFill.Enqueue(i);
}
this._newlyCompressedBlob = new AutoResetEvent(false);
this._runningCrc = new CRC32();
this._currentlyFilling = -1;
this._lastFilled = -1;
this._lastWritten = -1;
this._latestCompressed = -1;
}
public override void Write(byte[] buffer, int offset, int count)
{
bool mustWait = false;
bool isClosed = this._isClosed;
if (isClosed)
{
throw new InvalidOperationException();
}
bool flag = this._pendingException != null;
if (flag)
{
this._handlingException = true;
Exception pendingException = this._pendingException;
this._pendingException = null;
throw pendingException;
}
bool flag2 = count == 0;
if (!flag2)
{
bool flag3 = !this._firstWriteDone;
if (flag3)
{
this._InitializePoolOfWorkItems();
this._firstWriteDone = true;
}
for (;;)
{
this.EmitPendingBuffers(false, mustWait);
mustWait = false;
bool flag4 = this._currentlyFilling >= 0;
int num;
if (flag4)
{
num = this._currentlyFilling;
goto IL_D2;
}
bool flag5 = this._toFill.Count == 0;
if (!flag5)
{
num = this._toFill.Dequeue();
this._lastFilled++;
goto IL_D2;
}
mustWait = true;
IL_1A9:
if (count <= 0)
{
return;
}
continue;
IL_D2:
WorkItem workItem = this._pool[num];
int num2 = (workItem.buffer.Length - workItem.inputBytesAvailable > count) ? count : (workItem.buffer.Length - workItem.inputBytesAvailable);
workItem.ordinal = this._lastFilled;
Buffer.BlockCopy(buffer, offset, workItem.buffer, workItem.inputBytesAvailable, num2);
count -= num2;
offset += num2;
workItem.inputBytesAvailable += num2;
bool flag6 = workItem.inputBytesAvailable == workItem.buffer.Length;
if (flag6)
{
bool flag7 = !ThreadPool.QueueUserWorkItem(new WaitCallback(this._DeflateOne), workItem);
if (flag7)
{
break;
}
this._currentlyFilling = -1;
}
else
{
this._currentlyFilling = num;
}
bool flag8 = count > 0;
if (flag8)
{
}
goto IL_1A9;
}
throw new Exception("Cannot enqueue workitem");
}
}
private void _FlushFinish()
{
byte[] array = new byte[128];
ZlibCodec zlibCodec = new ZlibCodec();
int num = zlibCodec.InitializeDeflate(this._compressLevel, false);
zlibCodec.InputBuffer = null;
zlibCodec.NextIn = 0;
zlibCodec.AvailableBytesIn = 0;
zlibCodec.OutputBuffer = array;
zlibCodec.NextOut = 0;
zlibCodec.AvailableBytesOut = array.Length;
num = zlibCodec.Deflate(FlushType.Finish);
bool flag = num != 1 && num != 0;
if (flag)
{
throw new Exception("deflating: " + zlibCodec.Message);
}
bool flag2 = array.Length - zlibCodec.AvailableBytesOut > 0;
if (flag2)
{
this._outStream.Write(array, 0, array.Length - zlibCodec.AvailableBytesOut);
}
zlibCodec.EndDeflate();
this._Crc32 = this._runningCrc.Crc32Result;
}
private void _Flush(bool lastInput)
{
bool isClosed = this._isClosed;
if (isClosed)
{
throw new InvalidOperationException();
}
bool flag = this.emitting;
if (!flag)
{
bool flag2 = this._currentlyFilling >= 0;
if (flag2)
{
WorkItem wi = this._pool[this._currentlyFilling];
this._DeflateOne(wi);
this._currentlyFilling = -1;
}
if (lastInput)
{
this.EmitPendingBuffers(true, false);
this._FlushFinish();
}
else
{
this.EmitPendingBuffers(false, false);
}
}
}
public override void Flush()
{
bool flag = this._pendingException != null;
if (flag)
{
this._handlingException = true;
Exception pendingException = this._pendingException;
this._pendingException = null;
throw pendingException;
}
bool handlingException = this._handlingException;
if (!handlingException)
{
this._Flush(false);
}
}
public override void Close()
{
bool flag = this._pendingException != null;
if (flag)
{
this._handlingException = true;
Exception pendingException = this._pendingException;
this._pendingException = null;
throw pendingException;
}
bool handlingException = this._handlingException;
if (!handlingException)
{
bool isClosed = this._isClosed;
if (!isClosed)
{
this._Flush(true);
bool flag2 = !this._leaveOpen;
if (flag2)
{
this._outStream.Close();
}
this._isClosed = true;
}
}
}
public new void Dispose()
{
this.Close();
this._pool = null;
this.Dispose(true);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
public void Reset(Stream stream)
{
bool flag = !this._firstWriteDone;
if (!flag)
{
this._toWrite.Clear();
this._toFill.Clear();
foreach (WorkItem workItem in this._pool)
{
this._toFill.Enqueue(workItem.index);
workItem.ordinal = -1;
}
this._firstWriteDone = false;
this._totalBytesProcessed = 0L;
this._runningCrc = new CRC32();
this._isClosed = false;
this._currentlyFilling = -1;
this._lastFilled = -1;
this._lastWritten = -1;
this._latestCompressed = -1;
this._outStream = stream;
}
}
private void EmitPendingBuffers(bool doAll, bool mustWait)
{
bool flag = this.emitting;
if (!flag)
{
this.emitting = true;
bool flag2 = doAll || mustWait;
if (flag2)
{
this._newlyCompressedBlob.WaitOne();
}
do
{
int num = -1;
int num2 = doAll ? 200 : (mustWait ? -1 : 0);
int num3 = -1;
for (;;)
{
bool flag3 = Monitor.TryEnter(this._toWrite, num2);
if (flag3)
{
num3 = -1;
try
{
bool flag4 = this._toWrite.Count > 0;
if (flag4)
{
num3 = this._toWrite.Dequeue();
}
}
finally
{
Monitor.Exit(this._toWrite);
}
bool flag5 = num3 >= 0;
if (flag5)
{
WorkItem workItem = this._pool[num3];
bool flag6 = workItem.ordinal != this._lastWritten + 1;
if (flag6)
{
Queue<int> toWrite = this._toWrite;
lock (toWrite)
{
this._toWrite.Enqueue(num3);
}
bool flag7 = num == num3;
if (flag7)
{
this._newlyCompressedBlob.WaitOne();
num = -1;
}
else
{
bool flag8 = num == -1;
if (flag8)
{
num = num3;
}
}
}
else
{
num = -1;
this._outStream.Write(workItem.compressed, 0, workItem.compressedBytesAvailable);
this._runningCrc.Combine(workItem.crc, workItem.inputBytesAvailable);
this._totalBytesProcessed += (long)workItem.inputBytesAvailable;
workItem.inputBytesAvailable = 0;
this._lastWritten = workItem.ordinal;
this._toFill.Enqueue(workItem.index);
bool flag9 = num2 == -1;
if (flag9)
{
num2 = 0;
}
}
}
}
else
{
num3 = -1;
}
IL_1AE:
if (num3 < 0)
{
break;
}
continue;
goto IL_1AE;
}
}
while (doAll && this._lastWritten != this._latestCompressed);
this.emitting = false;
}
}
private void _DeflateOne(object wi)
{
WorkItem workItem = (WorkItem)wi;
try
{
int index = workItem.index;
CRC32 crc = new CRC32();
crc.SlurpBlock(workItem.buffer, 0, workItem.inputBytesAvailable);
this.DeflateOneSegment(workItem);
workItem.crc = crc.Crc32Result;
object latestLock = this._latestLock;
lock (latestLock)
{
bool flag = workItem.ordinal > this._latestCompressed;
if (flag)
{
this._latestCompressed = workItem.ordinal;
}
}
Queue<int> toWrite = this._toWrite;
lock (toWrite)
{
this._toWrite.Enqueue(workItem.index);
}
this._newlyCompressedBlob.Set();
}
catch (Exception pendingException)
{
object eLock = this._eLock;
lock (eLock)
{
bool flag2 = this._pendingException != null;
if (flag2)
{
this._pendingException = pendingException;
}
}
}
}
private bool DeflateOneSegment(WorkItem workitem)
{
ZlibCodec compressor = workitem.compressor;
compressor.ResetDeflate(true);
compressor.NextIn = 0;
compressor.AvailableBytesIn = workitem.inputBytesAvailable;
compressor.NextOut = 0;
compressor.AvailableBytesOut = workitem.compressed.Length;
do
{
compressor.Deflate(FlushType.None);
}
while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
int num = compressor.Deflate(FlushType.Sync);
workitem.compressedBytesAvailable = (int)compressor.TotalBytesOut;
return true;
}
[Conditional("Trace")]
private void TraceOutput(ParallelDeflateOutputStream.TraceBits bits, string format, params object[] varParams)
{
bool flag = (bits & this._DesiredTrace) > ParallelDeflateOutputStream.TraceBits.None;
if (flag)
{
object outputLock = this._outputLock;
lock (outputLock)
{
int hashCode = Thread.CurrentThread.GetHashCode();
}
}
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
}
}
|