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

            const int buffersize = 100;
            var       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)
                {
                    var 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!
            var result = new byte[size];

            Array.Copy(buffer, result, size);
            return(result);
        }