示例#1
0
        public override void Write(Stream outputStream, ICacheItem cacheItem)
        {
            PictureCacheItem pci    = (PictureCacheItem)cacheItem;
            StringBuilder    output = new StringBuilder();

            output.Append(pci.url + "\n");
            output.Append(pci.creationTime.Ticks + "\n");
            output.Append(pci.filename + "\n");
            output.Append(pci.fileSize + "\n");

            Utils.WriteString(outputStream, output.ToString());
        }
示例#2
0
        public override ICacheItem Read(Stream inputStream)
        {
            string s = Utils.ReadString(inputStream);

            string[] chunks       = s.Split('\n');
            string   url          = chunks[0];
            DateTime creationTime = new DateTime(long.Parse(chunks[1]));
            string   filename     = chunks[2];
            long     fileSize     = long.Parse(chunks[3]);

            PictureCacheItem pci = new PictureCacheItem();

            pci.url          = url;
            pci.creationTime = creationTime;
            pci.filename     = filename;
            pci.fileSize     = fileSize;
            return(pci);
        }
示例#3
0
        /// <summary>
        /// Downloads the picture from a internet and transfers it to a stream object.
        /// </summary>
        /// <param name="url">The url of the image to download.</param>
        /// <returns>A <see cref="Stream"/> object containing the downloaded picture.</returns>
        /// <remarks>
        /// 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.
        /// </remarks>
        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);
        }
示例#4
0
		public override ICacheItem Read(Stream inputStream)
		{
			string s = Utils.ReadString(inputStream);

			string[] chunks = s.Split('\n');
			string url = chunks[0];
			DateTime creationTime = new DateTime(long.Parse(chunks[1]));
			string filename = chunks[2];
			long fileSize = long.Parse(chunks[3]);

			PictureCacheItem pci = new PictureCacheItem();
			pci.url = url;
			pci.creationTime = creationTime;
			pci.filename = filename;
			pci.fileSize = fileSize;
			return pci;
		}