FlickrNet.Flickr.DownloadPicture C# (CSharp) Method

DownloadPicture() public method

Downloads the picture from a internet and transfers it to a stream object.
The method checks the download cache first to see if the picture has already been downloaded and if so returns the cached image. Otherwise it goes to the internet for the actual image.
public DownloadPicture ( string url ) : Stream
url string The url of the image to download.
return System.IO.Stream
        public System.IO.Stream DownloadPicture(string url)
        {
            const int BUFFER_SIZE = 1024 * 10;

            PictureCacheItem cacheItem = (PictureCacheItem) Cache.Downloads[url];
            if (cacheItem != null)
            {
                return  new FileStream(cacheItem.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }

            PictureCacheItem picCache = new PictureCacheItem();
            picCache.filename = Path.Combine(Cache.CacheLocation,Guid.NewGuid().ToString());
            Stream read = DoDownloadPicture(url);
            Stream write = new FileStream(picCache.filename, FileMode.Create, FileAccess.Write, FileShare.None);

            byte[] buffer = new byte[BUFFER_SIZE];
            int bytes = 0;
            long fileSize = 0;

            while( (bytes = read.Read(buffer, 0, BUFFER_SIZE)) > 0 )
            {
                fileSize += bytes;
                write.Write(buffer, 0, bytes);
            }

            read.Close();
            write.Close();

            picCache.url = url;
            picCache.creationTime = DateTime.UtcNow;
            picCache.fileSize = fileSize;

            Cache.Downloads.Shrink(Math.Max(0, Cache.CacheSizeLimit - fileSize));
            Cache.Downloads[url] = picCache;

            return new FileStream(picCache.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        }
Flickr