Example #1
0
        /// <summary>
        /// Skips n decompressed bytes in the stream.
        /// </summary>
        /// <param name="n">The number of bytes to skip.</param>
        public long Skip(long n)
        {
            int len = 512;

            if (n < len)
            {
                len = (int)n;
            }
            byte[] tmp = new byte[len];
            return((long)ZLibUtil.ReadInput(this, tmp, 0, tmp.Length));
        }
Example #2
0
        /// <summary>
        /// Reads a number of decompressed bytes into the specified byte array.
        /// </summary>
        /// <param name="buffer">The array used to store decompressed bytes.</param>
        /// <param name="offset">The location in the array to begin reading.</param>
        /// <param name="count">The number of decompressed bytes to read.</param>
        /// <returns>The number of bytes that were decompressed into the byte array.</returns>
        /// <example> The following code demonstrates how to use the <c>ZInputStream</c> to decompresses data
        /// <code>
        /// [C#]
        /// private void decompressFile(string inFile, string outFile)
        ///	{
        ///	  /* Create a file to store decompressed data */
        ///		System.IO.FileStream decompressedFile = new System.IO.FileStream(@"c:\data\decompressed.dat", System.IO.FileMode.Create);
        ///		/* Open a file containing compressed data */
        ///		System.IO.FileStream compressedFile = new System.IO.FileStream(@"c:\data\compressed.dat", System.IO.FileMode.Open);
        ///		/* Create ZInputStream for decompression */
        ///		ZInputStream decompressionStream = new ZInputStream(compressedFile);
        ///
        ///		try
        ///		{
        ///				byte[] buffer = new byte[2000];
        ///				int len;
        ///				/* Read and decompress data */
        ///				while ((len = decompressionStream.Read(buffer, 0, 2000)) > 0)
        ///				{
        ///				  /* Store decompressed data */
        ///					decompressedFile.Write(buffer, 0, len);
        ///				}
        ///		}
        ///		finally
        ///		{
        ///			decompressionStream.Close();
        ///			decompressedFile.Close();
        ///			compressedFile.Close();
        ///		}
        ///	}
        /// </code>
        /// </example>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (count == 0)
            {
                return(0);
            }

            if (this.needCopyArrays && ZLibUtil.CopyLargeArrayToSmall.GetRemainingDataSize() > 0)
            {
                return(ZLibUtil.CopyLargeArrayToSmall.CopyData());
            }
            else
            {
                this.needCopyArrays = false;
            }

            bool call_finish = false;
            int  err;

            z.next_out       = buffer;
            z.next_out_index = offset;
            z.avail_out      = count;
            do
            {
                if ((z.avail_in == 0) && (!nomoreinput))
                {
                    // if buffer is empty and more input is available, refill it
                    z.next_in_index = 0;
                    z.avail_in      = ZLibUtil.ReadInput(_stream, buf, 0, ZLibUtil.zLibBufSize); //(ZLibUtil.zLibBufSize<z._avail_out ? ZLibUtil.zLibBufSize : z._avail_out));
                    if (z.avail_in == -1)
                    {
                        z.avail_in  = 0;
                        nomoreinput = true;
                    }
                }
                if ((z.avail_in == 0) && nomoreinput)
                {
                    call_finish = true;
                    break;
                }

                err = z.inflate(flush);
                if (nomoreinput && (err == (int)ZLibResultCode.Z_BUF_ERROR))
                {
                    return(-1);
                }
                if (err != (int)ZLibResultCode.Z_OK && err != (int)ZLibResultCode.Z_STREAM_END)
                {
                    throw new ZStreamException("inflating: " + z.msg);
                }
                if (nomoreinput && (z.avail_out == count))
                {
                    return(-1);
                }
            }while (z.avail_out == count && err == (int)ZLibResultCode.Z_OK);
            if (call_finish)
            {
                return(Finish(buffer, offset, count));
            }
            return(count - z.avail_out);
        }