Fragment streams consists of one or more of what I've come to call blocks which in turn consists of a header describing the block, it's compression mode (on/off), it's length (compressed and uncompressed).
        private void ReadBlock()
        {
            this.currentBlock = this.reader.ReadBlock();

            if (this.currentBlock.IsEmpty)
            {
                this.readBuffer = new MemoryStream();
                return;
            }

            if (this.currentBlock.Compressed)
            {
                byte[] buffer = new byte[this.currentBlock.UncompressedLength];

                var inflater = new Inflater();

                inflater.SetInput(this.currentBlock.Content);
                inflater.Inflate(buffer);

                this.readBuffer = new MemoryStream(buffer);
            }
            else
            {
                this.readBuffer = new MemoryStream(this.currentBlock.Content);
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this.streamMode != StreamMode.Read)
            {
                throw new InvalidOperationException("Cannot read from write stream");
            }

            if (this.isDone)
            {
                return(0);
            }

            if (this.currentBlock == null)
            {
                this.ReadBlock();
            }

            if (this.currentBlock.IsEmpty)
            {
                this.isDone       = true;
                this.currentBlock = null;
                return(0);
            }

            int read = this.readBuffer.Read(buffer, offset, count);

            if (read == 0)
            {
                if (this.currentBlock.Compressed && (this.currentBlockRead != this.currentBlock.UncompressedLength))
                {
                    throw new IOException("Uncompressed block size did not match actual content length");
                }

                this.currentBlock     = null;
                this.currentBlockRead = 0;

                return(this.Read(buffer, offset, count));
            }

            this.currentBlockRead += read;

            return(read);
        }
        public override void Close()
        {
            base.Close();

            if (this.isClosed)
            {
                return;
            }

            if (this.streamMode == StreamMode.Read)
            {
                if (this.currentBlock != null)
                {
                    this.currentBlock = null;
                }
            }
            else if (this.streamMode == StreamMode.Write)
            {
                this.writeStream.Close();
            }

            this.isClosed = true;
        }
Beispiel #4
0
 public void WriteBlock(FragmentBlock block)
 {
     WriteBlock(block.Content, 0, block.Length, block.Compressed, block.UncompressedLength);
 }
Beispiel #5
0
 public void WriteBlock(FragmentBlock block)
 {
     WriteBlock(block.Content, 0, block.Length, block.Compressed, block.UncompressedLength);
 }
        private void ReadBlock()
        {
            this.currentBlock = this.reader.ReadBlock();

            if (this.currentBlock.IsEmpty)
            {
                this.readBuffer = new MemoryStream();
                return;
            }

            if (this.currentBlock.Compressed)
            {
                byte[] buffer = new byte[this.currentBlock.UncompressedLength];

                var inflater = new Inflater();

                inflater.SetInput(this.currentBlock.Content);
                inflater.Inflate(buffer);

                this.readBuffer = new MemoryStream(buffer);
            }
            else
            {
                this.readBuffer = new MemoryStream(this.currentBlock.Content);
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this.streamMode != StreamMode.Read)
                throw new InvalidOperationException("Cannot read from write stream");

            if (this.isDone)
                return 0;

            if (this.currentBlock == null)
                this.ReadBlock();

            if (this.currentBlock.IsEmpty)
            {
                this.isDone = true;
                this.currentBlock = null;
                return 0;
            }

            int read = this.readBuffer.Read(buffer, offset, count);

            if (read == 0)
            {
                if (this.currentBlock.Compressed && (this.currentBlockRead != this.currentBlock.UncompressedLength))
                    throw new IOException("Uncompressed block size did not match actual content length");

                this.currentBlock = null;
                this.currentBlockRead = 0;

                return this.Read(buffer, offset, count);
            }

            this.currentBlockRead += read;

            return read;
        }
        public override void Close()
        {
            base.Close();

            if (this.isClosed)
                return;

            if (this.streamMode == StreamMode.Read)
            {
                if (this.currentBlock != null)
                    this.currentBlock = null;
            }
            else if (this.streamMode == StreamMode.Write)
            {
                this.writeStream.Close();
            }

            this.isClosed = true;
        }