public static bool TryRead(Stream stream, out LengthedPrefixedString s) { s = null; if (!MatchHeader(stream)) return false; var lengthBytes = new byte[4]; if (stream.Read(lengthBytes, 0, 4) < 4) return false; int length = BitConverter.ToInt32(lengthBytes, 0); var stringBytes = new byte[length]; if (stream.Read(stringBytes, 0, length) < length) return false; try { string data = Encoding.UTF8.GetString(stringBytes); s = new LengthedPrefixedString(data); } catch { return false; } if (!HasTerminator(stream)) return false; return true; }
private IEnumerable <string> GetEntries(string key) { var list = new List <string>(); var bytes = _memcachedClient.Get <byte[]>(key); if (bytes == null) { return(list); } LengthedPrefixedString prefixedString; var memoryStream = new MemoryStream(bytes); while (LengthedPrefixedString.TryRead(memoryStream, out prefixedString)) { list.Add(prefixedString.InternalString); } return(list); }
public static bool TryRead(Stream stream, out LengthedPrefixedString s) { s = null; if (!MatchHeader(stream)) { return(false); } var lengthBytes = new byte[4]; if (stream.Read(lengthBytes, 0, 4) < 4) { return(false); } int length = BitConverter.ToInt32(lengthBytes, 0); var stringBytes = new byte[length]; if (stream.Read(stringBytes, 0, length) < length) { return(false); } try { string data = Encoding.UTF8.GetString(stringBytes); s = new LengthedPrefixedString(data); } catch { return(false); } if (!HasTerminator(stream)) { return(false); } return(true); }