예제 #1
0
파일: Bytes.cs 프로젝트: ulrakhma/Nemo
        /// <summary>
        /// Reads all bytes in the given zip stream and returns them.
        /// </summary>
        /// <param name="gzip">The zip stream that is processed.</param>
        /// <returns></returns>
        private static byte[] ReadAllBytes(GZipStream zip)
        {
            zip.ThrowIfNull("zip");

            int buffersize = 100;
            byte[] buffer = new byte[buffersize];
            int offset = 0, read = 0, size = 0;
            do
            {
                // If the buffer doesn't offer enough space left create a new array
                // with the double size. Copy the current buffer content to that array
                // and use that as new buffer.
                if (buffer.Length < size + buffersize)
                {
                    byte[] tmp = new byte[buffer.Length * 2];
                    Array.Copy(buffer, tmp, buffer.Length);
                    buffer = tmp;
                }

                // Read the net chunk of data.
                read = zip.Read(buffer, offset, buffersize);

                // Increment offset and read size.
                offset += buffersize;
                size += read;
            } while (read == buffersize); // Terminate if we read less then the buffer size.

            // Copy only that amount of data to the result that has actually been read!
            byte[] result = new byte[size];
            Array.Copy(buffer, result, size);
            return result;
        }