Esempio n. 1
0
		public byte[] Read(string file)
		{
			CacheReadEntry ce=null;
			lock (this)
			{
				cacherequests++;
				ce = (CacheReadEntry) files[file];
				if (ce == null)
				{	// entry isn't found; create new one
					if (files.Count >= maxFiles)	// Exceeded cache count?
						Reduce();					// yes, we need to reduce the file count

					FileStream fs = null;
					BinaryReader reader = null;
					byte[] bytes;
					try
					{
						fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
						reader = new BinaryReader(fs);
						bytes = new byte[fs.Length];
						int read;
						int totalRead=0;
						while((read = reader.Read(bytes, 0, bytes.Length)) != 0) 
						{
							totalRead += read;
						}
						ce = new CacheReadEntry(file, bytes);
						if (bytes.Length <= this.maxSize)
							files.Add(file, ce);		// don't actually cache if too big
					}
					catch (Exception e)
					{
						Console.WriteLine("File read error: {0} ", e );
						throw e;
					}
					finally
					{
						if (reader != null)
							reader.Close(); 
						if (fs != null)
							fs.Close();
					}
				}
				else
				{
					ce.Timestamp = DateTime.Now;
					cachehits++;
				}
			}
			if (ce != null)
				return ce.Value;
			else
				return null;
		}
Esempio n. 2
0
        public int CompareTo(object obj)
        {
            CacheReadEntry ce = obj as CacheReadEntry;

            long t = this.Timestamp.Ticks - ce.Timestamp.Ticks;

            if (t < 0)
            {
                return(-1);
            }
            else if (t > 0)
            {
                return(1);
            }
            return(0);
        }
Esempio n. 3
0
        public byte[] Read(string file)
        {
            CacheReadEntry ce = null;

            lock (this)
            {
                cacherequests++;
                ce = (CacheReadEntry)files[file];
                if (ce == null)
                {                                // entry isn't found; create new one
                    if (files.Count >= maxFiles) // Exceeded cache count?
                    {
                        Reduce();                // yes, we need to reduce the file count
                    }
                    FileStream   fs     = null;
                    BinaryReader reader = null;
                    byte[]       bytes;
                    try
                    {
                        fs     = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                        reader = new BinaryReader(fs);
                        bytes  = new byte[fs.Length];
                        int read;
                        int totalRead = 0;
                        while ((read = reader.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            totalRead += read;
                        }
                        ce = new CacheReadEntry(file, bytes);
                        if (bytes.Length <= this.maxSize)
                        {
                            files.Add(file, ce);                                        // don't actually cache if too big
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("File read error: {0} ", e);
                        throw e;
                    }
                    finally
                    {
                        if (reader != null)
                        {
                            reader.Close();
                        }
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                }
                else
                {
                    ce.Timestamp = DateTime.Now;
                    cachehits++;
                }
            }
            if (ce != null)
            {
                return(ce.Value);
            }
            else
            {
                return(null);
            }
        }