Include Non-NuGet Resources

This commit is contained in:
Gary Sharp
2013-02-01 12:52:29 +11:00
parent 0a93429800
commit 442cb1bdb7
135 changed files with 224150 additions and 0 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,530 @@
//#define Trace
// BZip2OutputStream.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-August-02 16:44:11>
//
// ------------------------------------------------------------------
//
// This module defines the BZip2OutputStream class, which is a
// compressing stream that handles BZIP2. This code may have been
// derived in part from Apache commons source code. The license below
// applies to the original Apache code.
//
// ------------------------------------------------------------------
// flymake: csc.exe /t:module BZip2InputStream.cs BZip2Compressor.cs Rand.cs BCRC32.cs @@FILE@@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// Design Notes:
//
// This class follows the classic Decorator pattern: it is a Stream that
// wraps itself around a Stream, and in doing so provides bzip2
// compression as callers Write into it.
//
// BZip2 is a straightforward data format: there are 4 magic bytes at
// the top of the file, followed by 1 or more compressed blocks. There
// is a small "magic byte" trailer after all compressed blocks. This
// class emits the magic bytes for the header and trailer, and relies on
// a BZip2Compressor to generate each of the compressed data blocks.
//
// BZip2 does byte-shredding - it uses partial fractions of bytes to
// represent independent pieces of information. This class relies on the
// BitWriter to adapt the bit-oriented BZip2 output to the byte-oriented
// model of the .NET Stream class.
//
// ----
//
// Regarding the Apache code base: Most of the code in this particular
// class is related to stream operations, and is my own code. It largely
// does not rely on any code obtained from Apache commons. If you
// compare this code with the Apache commons BZip2OutputStream, you will
// see very little code that is common, except for the
// nearly-boilerplate structure that is common to all subtypes of
// System.IO.Stream. There may be some small remnants of code in this
// module derived from the Apache stuff, which is why I left the license
// in here. Most of the Apache commons compressor magic has been ported
// into the BZip2Compressor class.
//
using System;
using System.IO;
namespace Ionic.BZip2
{
/// <summary>
/// A write-only decorator stream that compresses data as it is
/// written using the BZip2 algorithm.
/// </summary>
public class BZip2OutputStream : System.IO.Stream
{
int totalBytesWrittenIn;
bool leaveOpen;
BZip2Compressor compressor;
uint combinedCRC;
Stream output;
BitWriter bw;
int blockSize100k; // 0...9
private TraceBits desiredTrace = TraceBits.Crc | TraceBits.Write;
/// <summary>
/// Constructs a new <c>BZip2OutputStream</c>, that sends its
/// compressed output to the given output stream.
/// </summary>
///
/// <param name='output'>
/// The destination stream, to which compressed output will be sent.
/// </param>
///
/// <example>
///
/// This example reads a file, then compresses it with bzip2 file,
/// and writes the compressed data into a newly created file.
///
/// <code>
/// var fname = "logfile.log";
/// using (var fs = File.OpenRead(fname))
/// {
/// var outFname = fname + ".bz2";
/// using (var output = File.Create(outFname))
/// {
/// using (var compressor = new Ionic.BZip2.BZip2OutputStream(output))
/// {
/// byte[] buffer = new byte[2048];
/// int n;
/// while ((n = fs.Read(buffer, 0, buffer.Length)) > 0)
/// {
/// compressor.Write(buffer, 0, n);
/// }
/// }
/// }
/// }
/// </code>
/// </example>
public BZip2OutputStream(Stream output)
: this(output, BZip2.MaxBlockSize, false)
{
}
/// <summary>
/// Constructs a new <c>BZip2OutputStream</c> with specified blocksize.
/// </summary>
/// <param name = "output">the destination stream.</param>
/// <param name = "blockSize">
/// The blockSize in units of 100000 bytes.
/// The valid range is 1..9.
/// </param>
public BZip2OutputStream(Stream output, int blockSize)
: this(output, blockSize, false)
{
}
/// <summary>
/// Constructs a new <c>BZip2OutputStream</c>.
/// </summary>
/// <param name = "output">the destination stream.</param>
/// <param name = "leaveOpen">
/// whether to leave the captive stream open upon closing this stream.
/// </param>
public BZip2OutputStream(Stream output, bool leaveOpen)
: this(output, BZip2.MaxBlockSize, leaveOpen)
{
}
/// <summary>
/// Constructs a new <c>BZip2OutputStream</c> with specified blocksize,
/// and explicitly specifies whether to leave the wrapped stream open.
/// </summary>
///
/// <param name = "output">the destination stream.</param>
/// <param name = "blockSize">
/// The blockSize in units of 100000 bytes.
/// The valid range is 1..9.
/// </param>
/// <param name = "leaveOpen">
/// whether to leave the captive stream open upon closing this stream.
/// </param>
public BZip2OutputStream(Stream output, int blockSize, bool leaveOpen)
{
if (blockSize < BZip2.MinBlockSize ||
blockSize > BZip2.MaxBlockSize)
{
var msg = String.Format("blockSize={0} is out of range; must be between {1} and {2}",
blockSize,
BZip2.MinBlockSize, BZip2.MaxBlockSize);
throw new ArgumentException(msg, "blockSize");
}
this.output = output;
if (!this.output.CanWrite)
throw new ArgumentException("The stream is not writable.", "output");
this.bw = new BitWriter(this.output);
this.blockSize100k = blockSize;
this.compressor = new BZip2Compressor(this.bw, blockSize);
this.leaveOpen = leaveOpen;
this.combinedCRC = 0;
EmitHeader();
}
/// <summary>
/// Close the stream.
/// </summary>
/// <remarks>
/// <para>
/// This may or may not close the underlying stream. Check the
/// constructors that accept a bool value.
/// </para>
/// </remarks>
public override void Close()
{
if (output != null)
{
Stream o = this.output;
Finish();
if (!leaveOpen)
o.Close();
}
}
/// <summary>
/// Flush the stream.
/// </summary>
public override void Flush()
{
if (this.output != null)
{
this.bw.Flush();
this.output.Flush();
}
}
private void EmitHeader()
{
var magic = new byte[] {
(byte) 'B',
(byte) 'Z',
(byte) 'h',
(byte) ('0' + this.blockSize100k)
};
// not necessary to shred the initial magic bytes
this.output.Write(magic, 0, magic.Length);
}
private void EmitTrailer()
{
// A magic 48-bit number, 0x177245385090, to indicate the end
// of the last block. (sqrt(pi), if you want to know)
TraceOutput(TraceBits.Write, "total written out: {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut);
// must shred
this.bw.WriteByte(0x17);
this.bw.WriteByte(0x72);
this.bw.WriteByte(0x45);
this.bw.WriteByte(0x38);
this.bw.WriteByte(0x50);
this.bw.WriteByte(0x90);
this.bw.WriteInt(this.combinedCRC);
this.bw.FinishAndPad();
TraceOutput(TraceBits.Write, "final total: {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut);
}
void Finish()
{
// Console.WriteLine("BZip2:Finish");
try
{
var totalBefore = this.bw.TotalBytesWrittenOut;
this.compressor.CompressAndWrite();
TraceOutput(TraceBits.Write,"out block length (bytes): {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut - totalBefore);
TraceOutput(TraceBits.Crc, " combined CRC (before): {0:X8}",
this.combinedCRC);
this.combinedCRC = (this.combinedCRC << 1) | (this.combinedCRC >> 31);
this.combinedCRC ^= (uint) compressor.Crc32;
TraceOutput(TraceBits.Crc, " block CRC : {0:X8}",
this.compressor.Crc32);
TraceOutput(TraceBits.Crc, " combined CRC (final) : {0:X8}",
this.combinedCRC);
EmitTrailer();
}
finally
{
this.output = null;
this.compressor = null;
this.bw = null;
}
}
/// <summary>
/// The blocksize parameter specified at construction time.
/// </summary>
public int BlockSize
{
get { return this.blockSize100k; }
}
/// <summary>
/// Write data to the stream.
/// </summary>
/// <remarks>
///
/// <para>
/// Use the <c>BZip2OutputStream</c> to compress data while writing:
/// create a <c>BZip2OutputStream</c> with a writable output stream.
/// Then call <c>Write()</c> on that <c>BZip2OutputStream</c>, providing
/// uncompressed data as input. The data sent to the output stream will
/// be the compressed form of the input data.
/// </para>
///
/// <para>
/// A <c>BZip2OutputStream</c> can be used only for <c>Write()</c> not for <c>Read()</c>.
/// </para>
///
/// </remarks>
///
/// <param name="buffer">The buffer holding data to write to the stream.</param>
/// <param name="offset">the offset within that data array to find the first byte to write.</param>
/// <param name="count">the number of bytes to write.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (offset < 0)
throw new IndexOutOfRangeException(String.Format("offset ({0}) must be > 0", offset));
if (count < 0)
throw new IndexOutOfRangeException(String.Format("count ({0}) must be > 0", count));
if (offset + count > buffer.Length)
throw new IndexOutOfRangeException(String.Format("offset({0}) count({1}) bLength({2})",
offset, count, buffer.Length));
if (this.output == null)
throw new IOException("the stream is not open");
if (count == 0) return; // nothing to do
int bytesWritten = 0;
int bytesRemaining = count;
do
{
int n = compressor.Fill(buffer, offset, bytesRemaining);
if (n != bytesRemaining)
{
// The compressor data block is full. Compress and
// write out the compressed data, then reset the
// compressor and continue.
var totalBefore = this.bw.TotalBytesWrittenOut;
this.compressor.CompressAndWrite();
TraceOutput(TraceBits.Write,"out block length (bytes): {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut - totalBefore);
// and now any remaining bits
TraceOutput(TraceBits.Write,
" remaining: {0} 0x{1:X}",
this.bw.NumRemainingBits,
this.bw.RemainingBits);
TraceOutput(TraceBits.Crc, " combined CRC (before): {0:X8}",
this.combinedCRC);
this.combinedCRC = (this.combinedCRC << 1) | (this.combinedCRC >> 31);
this.combinedCRC ^= (uint) compressor.Crc32;
TraceOutput(TraceBits.Crc, " block CRC : {0:X8}",
compressor.Crc32);
TraceOutput(TraceBits.Crc, " combined CRC (after) : {0:X8}",
this.combinedCRC);
offset += n;
}
bytesRemaining -= n;
bytesWritten += n;
} while (bytesRemaining > 0);
totalBytesWrittenIn += bytesWritten;
}
/// <summary>
/// Indicates whether the stream can be read.
/// </summary>
/// <remarks>
/// The return value is always false.
/// </remarks>
public override bool CanRead
{
get { return false; }
}
/// <summary>
/// Indicates whether the stream supports Seek operations.
/// </summary>
/// <remarks>
/// Always returns false.
/// </remarks>
public override bool CanSeek
{
get { return false; }
}
/// <summary>
/// Indicates whether the stream can be written.
/// </summary>
/// <remarks>
/// The return value should always be true, unless and until the
/// object is disposed and closed.
/// </remarks>
public override bool CanWrite
{
get
{
if (this.output == null) throw new ObjectDisposedException("BZip2Stream");
return this.output.CanWrite;
}
}
/// <summary>
/// Reading this property always throws a <see cref="NotImplementedException"/>.
/// </summary>
public override long Length
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// The position of the stream pointer.
/// </summary>
///
/// <remarks>
/// Setting this property always throws a <see
/// cref="NotImplementedException"/>. Reading will return the
/// total number of uncompressed bytes written through.
/// </remarks>
public override long Position
{
get
{
return this.totalBytesWrittenIn;
}
set { throw new NotImplementedException(); }
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="offset">this is irrelevant, since it will always throw!</param>
/// <param name="origin">this is irrelevant, since it will always throw!</param>
/// <returns>irrelevant!</returns>
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotImplementedException();
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="value">this is irrelevant, since it will always throw!</param>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name='buffer'>this parameter is never used</param>
/// <param name='offset'>this parameter is never used</param>
/// <param name='count'>this parameter is never used</param>
/// <returns>never returns anything; always throws</returns>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
// used only when Trace is defined
[Flags]
enum TraceBits : uint
{
None = 0,
Crc = 1,
Write = 2,
All = 0xffffffff,
}
[System.Diagnostics.ConditionalAttribute("Trace")]
private void TraceOutput(TraceBits bits, string format, params object[] varParams)
{
if ((bits & this.desiredTrace) != 0)
{
//lock(outputLock)
{
int tid = System.Threading.Thread.CurrentThread.GetHashCode();
#if !SILVERLIGHT && !NETCF
Console.ForegroundColor = (ConsoleColor) (tid % 8 + 10);
#endif
Console.Write("{0:000} PBOS ", tid);
Console.WriteLine(format, varParams);
#if !SILVERLIGHT && !NETCF
Console.ResetColor();
#endif
}
}
}
}
}
@@ -0,0 +1,248 @@
// BitWriter.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-July-25 18:57:31>
//
// ------------------------------------------------------------------
//
// This module defines the BitWriter class, which writes bits at a time
// to an output stream. It's used by the BZip2Compressor class, and by
// the BZip2OutputStream class and its parallel variant,
// ParallelBZip2OutputStream.
//
// ------------------------------------------------------------------
//
// Design notes:
//
// BZip2 employs byte-shredding in its data format - rather than
// aligning all data items in a compressed .bz2 file on byte barriers,
// the BZip2 format uses portions of bytes to represent independent
// pieces of information. This "shredding" starts with the first
// "randomised" bit - just 12 bytes or so into a bz2 file or stream. But
// the approach is used extensively in bzip2 files - sometimes 5 bits
// are used, sometimes 24 or 3 bits, sometimes just 1 bit, and so on.
// It's not possible to send this information directly to a stream in
// this form; Streams in .NET accept byte-oriented input. Therefore,
// when actually writing a bz2 file, the output data must be organized
// into a byte-aligned format before being written to the output stream.
//
// This BitWriter class provides the byte-shredding necessary for BZip2
// output. Think of this class as an Adapter that enables Bit-oriented
// output to a standard byte-oriented .NET stream. This class writes
// data out to the captive output stream only after the data bits have
// been accumulated and aligned. For example, suppose that during
// operation, the BZip2 compressor emits 5 bits, then 24 bits, then 32
// bits. When the first 5 bits are sent to the BitWriter, nothing is
// written to the output stream; instead these 5 bits are simply stored
// in the internal accumulator. When the next 24 bits are written, the
// first 3 bits are gathered with the accumulated bits. The resulting
// 5+3 constitutes an entire byte; the BitWriter then actually writes
// that byte to the output stream. This leaves 21 bits. BitWriter writes
// 2 more whole bytes (16 more bits), in 8-bit chunks, leaving 5 in the
// accumulator. BitWriter then follows the same procedure with the 32
// new bits. And so on.
//
// A quick tour of the implementation:
//
// The accumulator is a uint - so it can accumulate at most 4 bytes of
// information. In practice because of the design of this class, it
// never accumulates more than 3 bytes.
//
// The Flush() method emits all whole bytes available. After calling
// Flush(), there may be between 0-7 bits yet to be emitted into the
// output stream.
//
// FinishAndPad() emits all data, including the last partial byte and
// any necessary padding. In effect, it establishes a byte-alignment
// barrier. To support bzip2, FinishAndPad() should be called only once
// for a bz2 file, after the last bit of data has been written through
// this adapter. Other binary file formats may use byte-alignment at
// various points within the file, and FinishAndPad() would support that
// scenario.
//
// The internal fn Reset() is used to reset the state of the adapter;
// this class is used by BZip2Compressor, instances of which get re-used
// by multiple distinct threads, for different blocks of data.
//
using System;
using System.IO;
namespace Ionic.BZip2
{
internal class BitWriter
{
uint accumulator;
int nAccumulatedBits;
Stream output;
int totalBytesWrittenOut;
public BitWriter(Stream s)
{
this.output = s;
}
/// <summary>
/// Delivers the remaining bits, left-aligned, in a byte.
/// </summary>
/// <remarks>
/// <para>
/// This is valid only if NumRemainingBits is less than 8;
/// in other words it is valid only after a call to Flush().
/// </para>
/// </remarks>
public byte RemainingBits
{
get
{
return (byte) (this.accumulator >> (32 - this.nAccumulatedBits) & 0xff);
}
}
public int NumRemainingBits
{
get
{
return this.nAccumulatedBits;
}
}
public int TotalBytesWrittenOut
{
get
{
return this.totalBytesWrittenOut;
}
}
/// <summary>
/// Reset the BitWriter.
/// </summary>
/// <remarks>
/// <para>
/// This is useful when the BitWriter writes into a MemoryStream, and
/// is used by a BZip2Compressor, which itself is re-used for multiple
/// distinct data blocks.
/// </para>
/// </remarks>
public void Reset()
{
this.accumulator = 0;
this.nAccumulatedBits = 0;
this.totalBytesWrittenOut = 0;
this.output.Seek(0, SeekOrigin.Begin);
this.output.SetLength(0);
}
/// <summary>
/// Write some number of bits from the given value, into the output.
/// </summary>
/// <remarks>
/// <para>
/// The nbits value should be a max of 25, for safety. For performance
/// reasons, this method does not check!
/// </para>
/// </remarks>
public void WriteBits(int nbits, uint value)
{
int nAccumulated = this.nAccumulatedBits;
uint u = this.accumulator;
while (nAccumulated >= 8)
{
this.output.WriteByte ((byte)(u >> 24 & 0xff));
this.totalBytesWrittenOut++;
u <<= 8;
nAccumulated -= 8;
}
this.accumulator = u | (value << (32 - nAccumulated - nbits));
this.nAccumulatedBits = nAccumulated + nbits;
// Console.WriteLine("WriteBits({0}, 0x{1:X2}) => {2:X8} n({3})",
// nbits, value, accumulator, nAccumulatedBits);
// Console.ReadLine();
// At this point the accumulator may contain up to 31 bits waiting for
// output.
}
/// <summary>
/// Write a full 8-bit byte into the output.
/// </summary>
public void WriteByte(byte b)
{
WriteBits(8, b);
}
/// <summary>
/// Write four 8-bit bytes into the output.
/// </summary>
public void WriteInt(uint u)
{
WriteBits(8, (u >> 24) & 0xff);
WriteBits(8, (u >> 16) & 0xff);
WriteBits(8, (u >> 8) & 0xff);
WriteBits(8, u & 0xff);
}
/// <summary>
/// Write all available byte-aligned bytes.
/// </summary>
/// <remarks>
/// <para>
/// This method writes no new output, but flushes any accumulated
/// bits. At completion, the accumulator may contain up to 7
/// bits.
/// </para>
/// <para>
/// This is necessary when re-assembling output from N independent
/// compressors, one for each of N blocks. The output of any
/// particular compressor will in general have some fragment of a byte
/// remaining. This fragment needs to be accumulated into the
/// parent BZip2OutputStream.
/// </para>
/// </remarks>
public void Flush()
{
WriteBits(0,0);
}
/// <summary>
/// Writes all available bytes, and emits padding for the final byte as
/// necessary. This must be the last method invoked on an instance of
/// BitWriter.
/// </summary>
public void FinishAndPad()
{
Flush();
if (this.NumRemainingBits > 0)
{
byte b = (byte)((this.accumulator >> 24) & 0xff);
this.output.WriteByte(b);
this.totalBytesWrittenOut++;
}
}
}
}
@@ -0,0 +1,999 @@
//#define Trace
// ParallelBZip2OutputStream.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-August-02 16:44:24>
//
// ------------------------------------------------------------------
//
// This module defines the ParallelBZip2OutputStream class, which is a
// BZip2 compressing stream. This code was derived in part from Apache
// commons source code. The license below applies to the original Apache
// code.
//
// ------------------------------------------------------------------
// flymake: csc.exe /t:module BZip2InputStream.cs BZip2Compressor.cs Rand.cs BCRC32.cs @@FILE@@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// Design Notes:
//
// This class follows the classic Decorator pattern: it is a Stream that
// wraps itself around a Stream, and in doing so provides bzip2
// compression as callers Write into it. It is exactly the same in
// outward function as the BZip2OutputStream, except that this class can
// perform compression using multiple independent threads. Because of
// that, and because of the CPU-intensive nature of BZip2 compression,
// this class can perform significantly better (in terms of wall-click
// time) than the single-threaded variant, at the expense of memory and
// CPU utilization.
//
// BZip2 is a straightforward data format: there are 4 magic bytes at
// the top of the file, followed by 1 or more compressed blocks. There
// is a small "magic byte" trailer after all compressed blocks.
//
// In concept parallelizing BZip2 is simple: do the CPU-intensive
// compression for each block in a separate thread, then emit the
// compressed output, in order, to the output stream. Each block can be
// compressed independently, so a block is the natural candidate for the
// parcel of work that can be passed to an independent worker thread.
//
// The design approach used here is simple: within the Write() method of
// the stream, fill a block. When the block is full, pass it to a
// background worker thread for compression. When the compressor thread
// completes its work, the main thread (the application thread that
// calls Write()) can send the compressed data to the output stream,
// being careful to respect the order of the compressed blocks.
//
// The challenge of ordering the compressed data is a solved and
// well-understood problem - it is the same approach here as DotNetZip
// uses in the ParallelDeflateOutputStream. It is a map/reduce approach
// in design intent.
//
// One new twist for BZip2 is that the compressor output is not
// byte-aligned. In other words the final output of a compressed block
// will in general be a number of bits that is not a multiple of
// 8. Therefore, combining the ordered results of the N compressor
// threads requires additional byte-shredding by the parent
// stream. Hence this stream uses a BitWriter to adapt bit-oriented
// BZip2 output to the byte-oriented .NET Stream.
//
// The approach used here creates N instances of the BZip2Compressor
// type, where N is governed by the number of cores (cpus) and limited
// by the MaxWorkers property exposed by this class. Each
// BZip2Compressor instance gets its own MemoryStream, to which it
// writes its data, via a BitWriter.
//
// along with the bit accumulator described above. The MemoryStream
// would gather the byte-aligned compressed output of the compressor.
// When reducing the output of the various workers, this class must
// again do the byte-shredding thing. The data from the compressors is
// therefore shredded twice: once when being placed into the
// MemoryStream, and again when emitted into the final output stream
// that this class decorates. This is an unfortunate and seemingly
// unavoidable inefficiency. Two rounds of byte-shredding will use more
// CPU than we'd like, but I haven't imagined a way to avoid it.
//
// The BZip2Compressor is designed to write directly into the parent
// stream's accumulator (BitWriter) when possible, and write into a
// distinct BitWriter when necessary. The former can be used in a
// single-thread scenario, while the latter is required in a
// multi-thread scenario.
//
// ----
//
// Regarding the Apache code base: Most of the code in this particular
// class is related to stream operations and thread synchronization, and
// is my own code. It largely does not rely on any code obtained from
// Apache commons. If you compare this code with the Apache commons
// BZip2OutputStream, you will see very little code that is common,
// except for the nearly-boilerplate structure that is common to all
// subtypes of System.IO.Stream. There may be some small remnants of
// code in this module derived from the Apache stuff, which is why I
// left the license in here. Most of the Apache commons compressor magic
// has been ported into the BZip2Compressor class.
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
namespace Ionic.BZip2
{
internal class WorkItem
{
public int index;
public BZip2Compressor Compressor { get; private set; }
public MemoryStream ms;
public int ordinal;
public BitWriter bw;
public WorkItem(int ix, int blockSize)
{
// compressed data gets written to a MemoryStream
this.ms = new MemoryStream();
this.bw = new BitWriter(ms);
this.Compressor = new BZip2Compressor(bw, blockSize);
this.index = ix;
}
}
/// <summary>
/// A write-only decorator stream that compresses data as it is
/// written using the BZip2 algorithm. This stream compresses by
/// block using multiple threads.
/// </summary>
/// <para>
/// This class performs BZIP2 compression through writing. For
/// more information on the BZIP2 algorithm, see
/// <see href="http://en.wikipedia.org/wiki/BZIP2"/>.
/// </para>
///
/// <para>
/// This class is similar to <see cref="Ionic.BZip2.BZip2OutputStream"/>,
/// except that this implementation uses an approach that employs multiple
/// worker threads to perform the compression. On a multi-cpu or multi-core
/// computer, the performance of this class can be significantly higher than
/// the single-threaded BZip2OutputStream, particularly for larger streams.
/// How large? Anything over 10mb is a good candidate for parallel
/// compression.
/// </para>
///
/// <para>
/// The tradeoff is that this class uses more memory and more CPU than the
/// vanilla <c>BZip2OutputStream</c>. Also, for small files, the
/// <c>ParallelBZip2OutputStream</c> can be much slower than the vanilla
/// <c>BZip2OutputStream</c>, because of the overhead associated to using the
/// thread pool.
/// </para>
///
/// <seealso cref="Ionic.BZip2.BZip2OutputStream" />
public class ParallelBZip2OutputStream : System.IO.Stream
{
private static readonly int BufferPairsPerCore = 4;
private int _maxWorkers;
private bool firstWriteDone;
private int lastFilled;
private int lastWritten;
private int latestCompressed;
private int currentlyFilling;
private volatile Exception pendingException;
private bool handlingException;
private bool emitting;
private System.Collections.Generic.Queue<int> toWrite;
private System.Collections.Generic.Queue<int> toFill;
private System.Collections.Generic.List<WorkItem> pool;
private object latestLock = new object();
private object eLock = new object(); // for exceptions
private object outputLock = new object(); // for multi-thread output
private AutoResetEvent newlyCompressedBlob;
long totalBytesWrittenIn;
long totalBytesWrittenOut;
bool leaveOpen;
uint combinedCRC;
Stream output;
BitWriter bw;
int blockSize100k; // 0...9
private TraceBits desiredTrace = TraceBits.Crc | TraceBits.Write;
/// <summary>
/// Constructs a new <c>ParallelBZip2OutputStream</c>, that sends its
/// compressed output to the given output stream.
/// </summary>
///
/// <param name='output'>
/// The destination stream, to which compressed output will be sent.
/// </param>
///
/// <example>
///
/// This example reads a file, then compresses it with bzip2 file,
/// and writes the compressed data into a newly created file.
///
/// <code>
/// var fname = "logfile.log";
/// using (var fs = File.OpenRead(fname))
/// {
/// var outFname = fname + ".bz2";
/// using (var output = File.Create(outFname))
/// {
/// using (var compressor = new Ionic.BZip2.ParallelBZip2OutputStream(output))
/// {
/// byte[] buffer = new byte[2048];
/// int n;
/// while ((n = fs.Read(buffer, 0, buffer.Length)) > 0)
/// {
/// compressor.Write(buffer, 0, n);
/// }
/// }
/// }
/// }
/// </code>
/// </example>
public ParallelBZip2OutputStream(Stream output)
: this(output, BZip2.MaxBlockSize, false)
{
}
/// <summary>
/// Constructs a new <c>ParallelBZip2OutputStream</c> with specified blocksize.
/// </summary>
/// <param name = "output">the destination stream.</param>
/// <param name = "blockSize">
/// The blockSize in units of 100000 bytes.
/// The valid range is 1..9.
/// </param>
public ParallelBZip2OutputStream(Stream output, int blockSize)
: this(output, blockSize, false)
{
}
/// <summary>
/// Constructs a new <c>ParallelBZip2OutputStream</c>.
/// </summary>
/// <param name = "output">the destination stream.</param>
/// <param name = "leaveOpen">
/// whether to leave the captive stream open upon closing this stream.
/// </param>
public ParallelBZip2OutputStream(Stream output, bool leaveOpen)
: this(output, BZip2.MaxBlockSize, leaveOpen)
{
}
/// <summary>
/// Constructs a new <c>ParallelBZip2OutputStream</c> with specified blocksize,
/// and explicitly specifies whether to leave the wrapped stream open.
/// </summary>
///
/// <param name = "output">the destination stream.</param>
/// <param name = "blockSize">
/// The blockSize in units of 100000 bytes.
/// The valid range is 1..9.
/// </param>
/// <param name = "leaveOpen">
/// whether to leave the captive stream open upon closing this stream.
/// </param>
public ParallelBZip2OutputStream(Stream output, int blockSize, bool leaveOpen)
{
if (blockSize < BZip2.MinBlockSize || blockSize > BZip2.MaxBlockSize)
{
var msg = String.Format("blockSize={0} is out of range; must be between {1} and {2}",
blockSize,
BZip2.MinBlockSize, BZip2.MaxBlockSize);
throw new ArgumentException(msg, "blockSize");
}
this.output = output;
if (!this.output.CanWrite)
throw new ArgumentException("The stream is not writable.", "output");
this.bw = new BitWriter(this.output);
this.blockSize100k = blockSize;
this.leaveOpen = leaveOpen;
this.combinedCRC = 0;
this.MaxWorkers = 16; // default
EmitHeader();
}
private void InitializePoolOfWorkItems()
{
this.toWrite = new Queue<int>();
this.toFill = new Queue<int>();
this.pool = new System.Collections.Generic.List<WorkItem>();
int nWorkers = BufferPairsPerCore * Environment.ProcessorCount;
nWorkers = Math.Min(nWorkers, this.MaxWorkers);
for(int i=0; i < nWorkers; i++)
{
this.pool.Add(new WorkItem(i, this.blockSize100k));
this.toFill.Enqueue(i);
}
this.newlyCompressedBlob = new AutoResetEvent(false);
this.currentlyFilling = -1;
this.lastFilled = -1;
this.lastWritten = -1;
this.latestCompressed = -1;
}
/// <summary>
/// The maximum number of concurrent compression worker threads to use.
/// </summary>
///
/// <remarks>
/// <para>
/// This property sets an upper limit on the number of concurrent worker
/// threads to employ for compression. The implementation of this stream
/// employs multiple threads from the .NET thread pool, via <see
/// cref="System.Threading.ThreadPool.QueueUserWorkItem(WaitCallback)">
/// ThreadPool.QueueUserWorkItem()</see>, to compress the incoming data by
/// block. As each block of data is compressed, this stream re-orders the
/// compressed blocks and writes them to the output stream.
/// </para>
///
/// <para>
/// A higher number of workers enables a higher degree of
/// parallelism, which tends to increase the speed of compression on
/// multi-cpu computers. On the other hand, a higher number of buffer
/// pairs also implies a larger memory consumption, more active worker
/// threads, and a higher cpu utilization for any compression. This
/// property enables the application to limit its memory consumption and
/// CPU utilization behavior depending on requirements.
/// </para>
///
/// <para>
/// By default, DotNetZip allocates 4 workers per CPU core, subject to the
/// upper limit specified in this property. For example, suppose the
/// application sets this property to 16. Then, on a machine with 2
/// cores, DotNetZip will use 8 workers; that number does not exceed the
/// upper limit specified by this property, so the actual number of
/// workers used will be 4 * 2 = 8. On a machine with 4 cores, DotNetZip
/// will use 16 workers; again, the limit does not apply. On a machine
/// with 8 cores, DotNetZip will use 16 workers, because of the limit.
/// </para>
///
/// <para>
/// For each compression "worker thread" that occurs in parallel, there is
/// up to 2mb of memory allocated, for buffering and processing. The
/// actual number depends on the <see cref="BlockSize"/> property.
/// </para>
///
/// <para>
/// CPU utilization will also go up with additional workers, because a
/// larger number of buffer pairs allows a larger number of background
/// threads to compress in parallel. If you find that parallel
/// compression is consuming too much memory or CPU, you can adjust this
/// value downward.
/// </para>
///
/// <para>
/// The default value is 16. Different values may deliver better or
/// worse results, depending on your priorities and the dynamic
/// performance characteristics of your storage and compute resources.
/// </para>
///
/// <para>
/// The application can set this value at any time, but it is effective
/// only before the first call to Write(), which is when the buffers are
/// allocated.
/// </para>
/// </remarks>
public int MaxWorkers
{
get
{
return _maxWorkers;
}
set
{
if (value < 4)
throw new ArgumentException("MaxWorkers",
"Value must be 4 or greater.");
_maxWorkers = value;
}
}
/// <summary>
/// Close the stream.
/// </summary>
/// <remarks>
/// <para>
/// This may or may not close the underlying stream. Check the
/// constructors that accept a bool value.
/// </para>
/// </remarks>
public override void Close()
{
if (this.pendingException != null)
{
this.handlingException = true;
var pe = this.pendingException;
this.pendingException = null;
throw pe;
}
if (this.handlingException)
return;
if (output == null)
return;
Stream o = this.output;
try
{
FlushOutput(true);
}
finally
{
this.output = null;
this.bw = null;
}
if (!leaveOpen)
o.Close();
}
private void FlushOutput(bool lastInput)
{
if (this.emitting) return;
// compress and write whatever is ready
if (this.currentlyFilling >= 0)
{
WorkItem workitem = this.pool[this.currentlyFilling];
CompressOne(workitem);
this.currentlyFilling = -1; // get a new buffer next Write()
}
if (lastInput)
{
EmitPendingBuffers(true, false);
EmitTrailer();
}
else
{
EmitPendingBuffers(false, false);
}
}
/// <summary>
/// Flush the stream.
/// </summary>
public override void Flush()
{
if (this.output != null)
{
FlushOutput(false);
this.bw.Flush();
this.output.Flush();
}
}
private void EmitHeader()
{
var magic = new byte[] {
(byte) 'B',
(byte) 'Z',
(byte) 'h',
(byte) ('0' + this.blockSize100k)
};
// not necessary to shred the initial magic bytes
this.output.Write(magic, 0, magic.Length);
}
private void EmitTrailer()
{
// A magic 48-bit number, 0x177245385090, to indicate the end
// of the last block. (sqrt(pi), if you want to know)
TraceOutput(TraceBits.Write, "total written out: {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut);
// must shred
this.bw.WriteByte(0x17);
this.bw.WriteByte(0x72);
this.bw.WriteByte(0x45);
this.bw.WriteByte(0x38);
this.bw.WriteByte(0x50);
this.bw.WriteByte(0x90);
this.bw.WriteInt(this.combinedCRC);
this.bw.FinishAndPad();
TraceOutput(TraceBits.Write, "final total : {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut);
}
/// <summary>
/// The blocksize parameter specified at construction time.
/// </summary>
public int BlockSize
{
get { return this.blockSize100k; }
}
/// <summary>
/// Write data to the stream.
/// </summary>
/// <remarks>
///
/// <para>
/// Use the <c>ParallelBZip2OutputStream</c> to compress data while
/// writing: create a <c>ParallelBZip2OutputStream</c> with a writable
/// output stream. Then call <c>Write()</c> on that
/// <c>ParallelBZip2OutputStream</c>, providing uncompressed data as
/// input. The data sent to the output stream will be the compressed
/// form of the input data.
/// </para>
///
/// <para>
/// A <c>ParallelBZip2OutputStream</c> can be used only for
/// <c>Write()</c> not for <c>Read()</c>.
/// </para>
///
/// </remarks>
///
/// <param name="buffer">The buffer holding data to write to the stream.</param>
/// <param name="offset">the offset within that data array to find the first byte to write.</param>
/// <param name="count">the number of bytes to write.</param>
public override void Write(byte[] buffer, int offset, int count)
{
bool mustWait = false;
// This method does this:
// 0. handles any pending exceptions
// 1. write any buffers that are ready to be written
// 2. fills a compressor buffer; when full, flip state to 'Filled',
// 3. if more data to be written, goto step 1
if (this.output == null)
throw new IOException("the stream is not open");
// dispense any exceptions that occurred on the BG threads
if (this.pendingException != null)
{
this.handlingException = true;
var pe = this.pendingException;
this.pendingException = null;
throw pe;
}
if (offset < 0)
throw new IndexOutOfRangeException(String.Format("offset ({0}) must be > 0", offset));
if (count < 0)
throw new IndexOutOfRangeException(String.Format("count ({0}) must be > 0", count));
if (offset + count > buffer.Length)
throw new IndexOutOfRangeException(String.Format("offset({0}) count({1}) bLength({2})",
offset, count, buffer.Length));
if (count == 0) return; // nothing to do
if (!this.firstWriteDone)
{
// Want to do this on first Write, first session, and not in the
// constructor. Must allow the MaxWorkers to change after
// construction, but before first Write().
InitializePoolOfWorkItems();
this.firstWriteDone = true;
}
int bytesWritten = 0;
int bytesRemaining = count;
do
{
// may need to make buffers available
EmitPendingBuffers(false, mustWait);
mustWait = false;
// get a compressor to fill
int ix = -1;
if (this.currentlyFilling >= 0)
{
ix = this.currentlyFilling;
}
else
{
if (this.toFill.Count == 0)
{
// No compressors available to fill, so... need to emit
// compressed buffers.
mustWait = true;
continue;
}
ix = this.toFill.Dequeue();
++this.lastFilled;
}
WorkItem workitem = this.pool[ix];
workitem.ordinal = this.lastFilled;
int n = workitem.Compressor.Fill(buffer, offset, bytesRemaining);
if (n != bytesRemaining)
{
if (!ThreadPool.QueueUserWorkItem( CompressOne, workitem ))
throw new Exception("Cannot enqueue workitem");
this.currentlyFilling = -1; // will get a new buffer next time
offset += n;
}
else
this.currentlyFilling = ix;
bytesRemaining -= n;
bytesWritten += n;
}
while (bytesRemaining > 0);
totalBytesWrittenIn += bytesWritten;
return;
}
private void EmitPendingBuffers(bool doAll, bool mustWait)
{
// When combining parallel compression with a ZipSegmentedStream, it's
// possible for the ZSS to throw from within this method. In that
// case, Close/Dispose will be called on this stream, if this stream
// is employed within a using or try/finally pair as required. But
// this stream is unaware of the pending exception, so the Close()
// method invokes this method AGAIN. This can lead to a deadlock.
// Therefore, failfast if re-entering.
if (emitting) return;
emitting = true;
if (doAll || mustWait)
this.newlyCompressedBlob.WaitOne();
do
{
int firstSkip = -1;
int millisecondsToWait = doAll ? 200 : (mustWait ? -1 : 0);
int nextToWrite = -1;
do
{
if (Monitor.TryEnter(this.toWrite, millisecondsToWait))
{
nextToWrite = -1;
try
{
if (this.toWrite.Count > 0)
nextToWrite = this.toWrite.Dequeue();
}
finally
{
Monitor.Exit(this.toWrite);
}
if (nextToWrite >= 0)
{
WorkItem workitem = this.pool[nextToWrite];
if (workitem.ordinal != this.lastWritten + 1)
{
// out of order. requeue and try again.
lock(this.toWrite)
{
this.toWrite.Enqueue(nextToWrite);
}
if (firstSkip == nextToWrite)
{
// We went around the list once.
// None of the items in the list is the one we want.
// Now wait for a compressor to signal again.
this.newlyCompressedBlob.WaitOne();
firstSkip = -1;
}
else if (firstSkip == -1)
firstSkip = nextToWrite;
continue;
}
firstSkip = -1;
TraceOutput(TraceBits.Write,
"Writing block {0}", workitem.ordinal);
// write the data to the output
var bw2 = workitem.bw;
bw2.Flush(); // not bw2.FinishAndPad()!
var ms = workitem.ms;
ms.Seek(0,SeekOrigin.Begin);
// cannot dump bytes!!
// ms.WriteTo(this.output);
//
// must do byte shredding:
int n;
int y = -1;
long totOut = 0;
var buffer = new byte[1024];
while ((n = ms.Read(buffer,0,buffer.Length)) > 0)
{
#if Trace
if (y == -1) // diagnostics only
{
var sb1 = new System.Text.StringBuilder();
sb1.Append("first 16 whole bytes in block: ");
for (int z=0; z < 16; z++)
sb1.Append(String.Format(" {0:X2}", buffer[z]));
TraceOutput(TraceBits.Write, sb1.ToString());
}
#endif
y = n;
for (int k=0; k < n; k++)
{
this.bw.WriteByte(buffer[k]);
}
totOut += n;
}
#if Trace
TraceOutput(TraceBits.Write,"out block length (bytes): {0} (0x{0:X})", totOut);
var sb = new System.Text.StringBuilder();
sb.Append("final 16 whole bytes in block: ");
for (int z=0; z < 16; z++)
sb.Append(String.Format(" {0:X2}", buffer[y-1-12+z]));
TraceOutput(TraceBits.Write, sb.ToString());
#endif
// and now any remaining bits
TraceOutput(TraceBits.Write,
" remaining bits: {0} 0x{1:X}",
bw2.NumRemainingBits,
bw2.RemainingBits);
if (bw2.NumRemainingBits > 0)
{
this.bw.WriteBits(bw2.NumRemainingBits, bw2.RemainingBits);
}
TraceOutput(TraceBits.Crc," combined CRC (before): {0:X8}",
this.combinedCRC);
this.combinedCRC = (this.combinedCRC << 1) | (this.combinedCRC >> 31);
this.combinedCRC ^= (uint) workitem.Compressor.Crc32;
TraceOutput(TraceBits.Crc,
" block CRC : {0:X8}",
workitem.Compressor.Crc32);
TraceOutput(TraceBits.Crc,
" combined CRC (after) : {0:X8}",
this.combinedCRC);
TraceOutput(TraceBits.Write,
"total written out: {0} (0x{0:X})",
this.bw.TotalBytesWrittenOut);
TraceOutput(TraceBits.Write | TraceBits.Crc, "");
this.totalBytesWrittenOut += totOut;
bw2.Reset();
this.lastWritten = workitem.ordinal;
workitem.ordinal = -1;
this.toFill.Enqueue(workitem.index);
// don't wait next time through
if (millisecondsToWait == -1) millisecondsToWait = 0;
}
}
else
nextToWrite = -1;
} while (nextToWrite >= 0);
} while (doAll && (this.lastWritten != this.latestCompressed));
if (doAll)
{
TraceOutput(TraceBits.Crc,
" combined CRC (final) : {0:X8}", this.combinedCRC);
}
emitting = false;
}
private void CompressOne(Object wi)
{
// compress and one buffer
WorkItem workitem = (WorkItem) wi;
try
{
// compress and write to the compressor's MemoryStream
workitem.Compressor.CompressAndWrite();
lock(this.latestLock)
{
if (workitem.ordinal > this.latestCompressed)
this.latestCompressed = workitem.ordinal;
}
lock (this.toWrite)
{
this.toWrite.Enqueue(workitem.index);
}
this.newlyCompressedBlob.Set();
}
catch (System.Exception exc1)
{
lock(this.eLock)
{
// expose the exception to the main thread
if (this.pendingException!=null)
this.pendingException = exc1;
}
}
}
/// <summary>
/// Indicates whether the stream can be read.
/// </summary>
/// <remarks>
/// The return value is always false.
/// </remarks>
public override bool CanRead
{
get { return false; }
}
/// <summary>
/// Indicates whether the stream supports Seek operations.
/// </summary>
/// <remarks>
/// Always returns false.
/// </remarks>
public override bool CanSeek
{
get { return false; }
}
/// <summary>
/// Indicates whether the stream can be written.
/// </summary>
/// <remarks>
/// The return value depends on whether the captive stream supports writing.
/// </remarks>
public override bool CanWrite
{
get
{
if (this.output == null) throw new ObjectDisposedException("BZip2Stream");
return this.output.CanWrite;
}
}
/// <summary>
/// Reading this property always throws a <see cref="NotImplementedException"/>.
/// </summary>
public override long Length
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// The position of the stream pointer.
/// </summary>
///
/// <remarks>
/// Setting this property always throws a <see
/// cref="NotImplementedException"/>. Reading will return the
/// total number of uncompressed bytes written through.
/// </remarks>
public override long Position
{
get
{
return this.totalBytesWrittenIn;
}
set { throw new NotImplementedException(); }
}
/// <summary>
/// The total number of bytes written out by the stream.
/// </summary>
/// <remarks>
/// This value is meaningful only after a call to Close().
/// </remarks>
public Int64 BytesWrittenOut { get { return totalBytesWrittenOut; } }
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="offset">this is irrelevant, since it will always throw!</param>
/// <param name="origin">this is irrelevant, since it will always throw!</param>
/// <returns>irrelevant!</returns>
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotImplementedException();
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="value">this is irrelevant, since it will always throw!</param>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name='buffer'>this parameter is never used</param>
/// <param name='offset'>this parameter is never used</param>
/// <param name='count'>this parameter is never used</param>
/// <returns>never returns anything; always throws</returns>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
// used only when Trace is defined
[Flags]
enum TraceBits : uint
{
None = 0,
Crc = 1,
Write = 2,
All = 0xffffffff,
}
[System.Diagnostics.ConditionalAttribute("Trace")]
private void TraceOutput(TraceBits bits, string format, params object[] varParams)
{
if ((bits & this.desiredTrace) != 0)
{
lock(outputLock)
{
int tid = Thread.CurrentThread.GetHashCode();
#if !SILVERLIGHT
Console.ForegroundColor = (ConsoleColor) (tid % 8 + 10);
#endif
Console.Write("{0:000} PBOS ", tid);
Console.WriteLine(format, varParams);
#if !SILVERLIGHT
Console.ResetColor();
#endif
}
}
}
}
}
@@ -0,0 +1,99 @@
// Rand.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-July-31 15:09:16>
//
// ------------------------------------------------------------------
//
// This module defines a helper class for the BZip2 classes. This code
// is derived from the original BZip2 source code.
//
// ------------------------------------------------------------------
namespace Ionic.BZip2
{
internal static class Rand
{
private static int[] RNUMS =
{
619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
936, 638
};
/// <summary>
/// Returns the "random" number at a specific index.
/// </summary>
/// <param name='i'>the index</param>
/// <returns>the random number</returns>
internal static int Rnums(int i)
{
return RNUMS[i];
}
}
}