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).
예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        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;
        }
예제 #4
0
 public void WriteBlock(FragmentBlock block)
 {
     WriteBlock(block.Content, 0, block.Length, block.Compressed, block.UncompressedLength);
 }
예제 #5
0
 public void WriteBlock(FragmentBlock block)
 {
     WriteBlock(block.Content, 0, block.Length, block.Compressed, block.UncompressedLength);
 }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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;
        }
예제 #8
0
        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;
        }