Esempio n. 1
0
		/// <summary>
		/// This method loads the data from cache into a Hashtable.
		/// 
		/// Pass a SockIO object which is ready to receive data and a Hashtable
		/// to store the results.
		/// </summary>
		/// <param name="sock">socket waiting to pass back data</param>
		/// <param name="hm">hashmap to store data into</param>
		/// <param name="asString">if true, and if we are using NativehHandler, return string val</param>
		private void LoadItems(SockIO sock, Hashtable hm, bool asString) 
		{
			while(true) 
			{
				string line = sock.ReadLine();
				if(log.IsDebugEnabled)
				{
					log.Debug(GetLocalizedString("loaditems line").Replace("$$Line$$", line));
				}

				if(line.StartsWith(VALUE)) 
				{
					string[] info = line.Split(' ');
					string key    = info[1];
					int flag      = int.Parse(info[2], new NumberFormatInfo());
					int length    = int.Parse(info[3], new NumberFormatInfo());

					if(log.IsDebugEnabled)
					{
						log.Debug(GetLocalizedString("loaditems header").Replace("$$Key$$", key).Replace("$$Flags$$", flag.ToString(new NumberFormatInfo())).Replace("$$Length$$", length.ToString(new NumberFormatInfo())));
					}
				
					// read obj into buffer
					byte[] buf = new byte[length];
					sock.Read(buf);
					sock.ClearEndOfLine();

					// ready object
					object o;
				
					// check for compression
					if((flag & F_COMPRESSED) != 0) 
					{
						try 
						{
							// read the input stream, and write to a byte array output stream since
							// we have to read into a byte array, but we don't know how large it
							// will need to be, and we don't want to resize it a bunch
							GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf));
							MemoryStream bos = new MemoryStream(buf.Length);
							
							int count;
							byte[] tmp = new byte[2048];
							while((count = gzi.Read(tmp, 0, tmp.Length)) > 0)
							{
								bos.Write(tmp, 0, count);
							}
							
							// store uncompressed back to buffer
							buf = bos.ToArray();
							gzi.Close();
						}
						catch(IOException e) 
						{
							if(log.IsErrorEnabled)
							{
								log.Error(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e);
							}
							throw new IOException(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e);
						}
					}

					// we can only take out serialized objects
					if((flag & F_SERIALIZED) == 0) 
					{
						if(_primitiveAsString || asString) 
						{
							// pulling out string value
							if(log.IsInfoEnabled)
							{
								log.Info(GetLocalizedString("loaditems retrieve as string"));
							}
							o = Encoding.GetEncoding(_defaultEncoding).GetString(buf);
						}
						else 
						{
							// decoding object
							try 
							{
								o = NativeHandler.Decode(buf);    
							}
							catch(Exception e) 
							{
								if(log.IsErrorEnabled)
								{
									log.Error(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e);
								}
								throw new IOException(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e);
							}
						}
					}
					else 
					{
						// deserialize if the data is serialized
						try 
						{
							MemoryStream memStream = new MemoryStream(buf);
							o = new BinaryFormatter().Deserialize(memStream);
							if(log.IsInfoEnabled)
							{
								log.Info(GetLocalizedString("loaditems deserializing").Replace("$$Class$$", o.GetType().Name));
							}
						}
						catch(SerializationException e) 
						{
							if(log.IsErrorEnabled)
							{
								log.Error(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e);
							}
							throw new IOException(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e);
						}
					}

					// store the object into the cache
					hm[ key ] =  o ;
				}
				else if(END == line) 
				{
					if(log.IsDebugEnabled)
					{
						log.Debug(GetLocalizedString("loaditems finished"));
					}
					break;
				}
			}
		}