Example #1
0
        public static byte[] LowLevelCompress(byte[] bytes, Shell.EndianFormat byteOrder)
        {
            Contract.Requires <ArgumentNullException>(bytes != null);
            Contract.Ensures(Contract.Result <byte[]>() != null);

            byte[] result = new byte[sizeof(int)];
            // Setup the decompressed size header
            byte[] size_bytes = BitConverter.GetBytes(bytes.Length);
            if (!byteOrder.IsSameAsRuntime())
            {
                Bitwise.ByteSwap.SwapInt32(size_bytes, 0);
            }
            Array.Copy(size_bytes, result, size_bytes.Length);

            var zip = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
                ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION, false);

            {
                zip.SetInput(bytes);
                zip.Finish();
                byte[] temp            = new byte[bytes.Length];
                int    compressed_size = zip.Deflate(temp);

                Contract.Assert(compressed_size <= bytes.Length);
                Array.Resize(ref result, sizeof(int) + compressed_size);
                Array.Copy(temp, 0, result, sizeof(int), compressed_size);
            }
            return(result);
        }
        // #REVIEW: .NET 4.5: BinaryReader has 'bool leaveOpen' ctor
        #region Ctor
        /// <summary>Create a new binary reader which respects the endian format of the underlying stream's bytes</summary>
        /// <param name="input">Base stream to use as input</param>
        /// <param name="encoding">Character encoding to use. If null, <see cref="System.Text.UTF8Encoding"/> is assumed</param>
        /// <param name="byteOrder">Endian format for how we interpret the stream's bytes</param>
        /// <param name="streamOwner">Owner object of this stream, or null</param>
        /// <param name="name">Special name to associate with this stream</param>
        public EndianReader(Stream input, Encoding encoding,
                            Shell.EndianFormat byteOrder, object streamOwner = null, string name = null) : base(input, encoding)
        {
            Contract.Requires <ArgumentNullException>(input != null);
            Contract.Requires <ArgumentNullException>(encoding != null);

            BaseStreamOwner = true;
            BaseAddress     = Values.PtrHandle.Null32;

            ByteOrder       = byteOrder;
            Owner           = streamOwner;
            mStringEncoding = encoding;

            StreamName = name ?? "(unnamed)";

            // If the stream is a different endian than the runtime, data will
            // be byte swapped of course
            //this.mRequiresByteSwap = Shell.Platform.Environment.ProcessorType.ByteOrder != byteOrder;
            mRequiresByteSwap = !byteOrder.IsSameAsRuntime();
        }