/// <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(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()); // read obj into buffer byte[] buf = new byte[length]; sock.Read(buf); sock.ClearEndOfLine(); // ready object object o=null; // check for compression if((flag & F_COMPRESSED) != 0) { MemoryStream mem = null; GZipStream gzi = null; 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 mem = new MemoryStream(buf.Length); gzi = new GZipStream(new MemoryStream(buf), CompressionMode.Compress); int count; var tmp = new byte[2048]; while ((count = gzi.Read(tmp, 0, tmp.Length)) > 0) { mem.Write(tmp, 0, count); } // store uncompressed back to buffer buf = mem.ToArray(); } finally { if(mem!=null) { mem.Close(); mem.Dispose(); } if(gzi != null) { gzi.Close(); gzi.Dispose(); } } } // we can only take out serialized objects if((flag & F_SERIALIZED) == 0) { if(_primitiveAsString || asString) { o = Encoding.GetEncoding(_defaultEncoding).GetString(buf); } else { // decoding object try { o = NativeHandler.Decode(buf); } catch(Exception e) { return; } } } else { // deserialize if the data is serialized MemoryStream memStream = null; try { memStream = new MemoryStream(buf); o = new BinaryFormatter().Deserialize(memStream); } catch(SerializationException e) { } finally { if(memStream != null) { memStream.Close(); memStream.Dispose(); } } } // store the object into the cache hm[ key ] = o ; } else if(END == line) { break; } } }
/// <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 (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()); // read obj into buffer byte[] buf = new byte[length]; sock.Read(buf); sock.ClearEndOfLine(); // ready object object o = null; // check for compression if ((flag & F_COMPRESSED) != 0) { MemoryStream mem = null; GZipStream gzi = null; 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 mem = new MemoryStream(buf.Length); gzi = new GZipStream(new MemoryStream(buf), CompressionMode.Compress); int count; var tmp = new byte[2048]; while ((count = gzi.Read(tmp, 0, tmp.Length)) > 0) { mem.Write(tmp, 0, count); } // store uncompressed back to buffer buf = mem.ToArray(); } finally { if (mem != null) { mem.Close(); mem.Dispose(); } if (gzi != null) { gzi.Close(); gzi.Dispose(); } } } // we can only take out serialized objects if ((flag & F_SERIALIZED) == 0) { if (_primitiveAsString || asString) { o = Encoding.GetEncoding(_defaultEncoding).GetString(buf); } else { // decoding object try { o = NativeHandler.Decode(buf); } catch (Exception e) { return; } } } else { // deserialize if the data is serialized MemoryStream memStream = null; try { memStream = new MemoryStream(buf); o = new BinaryFormatter().Deserialize(memStream); } catch (SerializationException e) { } finally { if (memStream != null) { memStream.Close(); memStream.Dispose(); } } } // store the object into the cache hm[key] = o; } else if (END == line) { break; } } }