Example #1
0
        // Write data to this stream.
        public override void Write(byte[] buffer, int offset, int count)
        {
            int temp;

            if (stream == null)
            {
                throw new ObjectDisposedException
                          (S._("Exception_Disposed"));
            }
            if (mode != CompressionMode.Compress)
            {
                throw new NotSupportedException(S._("IO_NotSupp_Write"));
            }
            DeflateStream.ValidateBuffer(buffer, offset, count);
            if (!headerDone)
            {
                // Write the gzip header to the output stream.
                stream.WriteByte(0x1F);                                 // Magic number.
                stream.WriteByte(0x8B);
                stream.WriteByte(0x08);                                 // Compression type.
                stream.WriteByte(0x00);                                 // Flags.
                int secs = (int)((DateTime.UtcNow.Ticks /
                                  TimeSpan.TicksPerSecond) -
                                 62135596800L);
                stream.WriteByte((byte)(secs & 0xFF));
                stream.WriteByte((byte)((secs >> 8) & 0xFF));
                stream.WriteByte((byte)((secs >> 16) & 0xFF));
                stream.WriteByte((byte)((secs >> 24) & 0xFF));
                stream.WriteByte(0x00);                                 // Extra flags.
                stream.WriteByte(0xFF);                                 // OS type (unknown).
                headerDone = true;
            }
            crc32.Update(buffer, offset, count);
            deflater.SetInput(buffer, offset, count);
            while (!deflater.IsNeedingInput)
            {
                temp = deflater.Deflate(buf, 0, buf.Length);
                if (temp <= 0)
                {
                    if (!deflater.IsNeedingInput)
                    {
                        throw new IOException(S._("IO_Compress_Input"));
                    }
                    break;
                }
                stream.Write(buf, 0, temp);
            }
        }
Example #2
0
        // Read data from this stream.
        public override int Read(byte[] buffer, int offset, int count)
        {
            int temp;

            if (stream == null)
            {
                throw new ObjectDisposedException
                          (S._("Exception_Disposed"));
            }
            if (mode != CompressionMode.Decompress)
            {
                throw new NotSupportedException(S._("IO_NotSupp_Read"));
            }
            DeflateStream.ValidateBuffer(buffer, offset, count);
            if (!headerDone)
            {
                ReadHeader();
            }
            if (endOfStream)
            {
                return(0);
            }
            for (;;)
            {
                temp = inflater.Inflate(buffer, offset, count);
                if (temp > 0)
                {
                    crc32.Update(buffer, offset, temp);
                    if (inflater.IsFinished)
                    {
                        ReadFooter();
                    }
                    return(temp);
                }
                if (inflater.IsNeedingDictionary)
                {
                    throw new IOException
                              (S._("IO_Decompress_NeedDict"));
                }
                else if (inflater.IsFinished)
                {
                    ReadFooter();
                    return(0);
                }
                else if (inflater.IsNeedingInput)
                {
                    temp = stream.Read(buf, 0, buf.Length);
                    if (temp <= 0)
                    {
                        throw new IOException
                                  (S._("IO_Decompress_Truncated"));
                    }
                    inflater.SetInput(buf, 0, temp);
                }
                else
                {
                    throw new IOException
                              (S._("IO_Decompress_Invalid"));
                }
            }
        }