public static PooledMemoryStream From(Stream stream, int length) { var total = 0; PooledMemoryStream destination = null; byte[] buffer = bufferManager.TakeBuffer(4096); try { int read; destination = new PooledMemoryStream(); while ((read = stream.Read(buffer, 0, Math.Min(buffer.Length, length - total))) != 0) { total += read; destination.Write(buffer, 0, read); } return destination; } catch { if(destination!=null) destination.Dispose(); throw; } finally { bufferManager.ReturnBuffer(buffer); } }
public static PooledMemoryStream From(Stream stream, int length) { var total = 0; PooledMemoryStream destination = null; byte[] buffer = bufferManager.TakeBuffer(4096); try { int read; destination = new PooledMemoryStream(); while ((read = stream.Read(buffer, 0, Math.Min(buffer.Length, length - total))) != 0) { total += read; destination.Write(buffer, 0, read); } return(destination); } catch { if (destination != null) { destination.Dispose(); } throw; } finally { bufferManager.ReturnBuffer(buffer); } }
public Guid Add(string key, string tag, Guid? etag, RavenJObject metadata, RavenJObject document) { EnsureEtagMatches(key, etag); var stream = new PooledMemoryStream(); metadata.WriteTo(new BsonWriter(stream)); document.WriteTo(new BsonWriter(stream)); var newGuid = Guid.NewGuid(); Storage.Documents.Put(new RavenJObject { {"key", key}, {"tag", tag}, {"etag", newGuid.ToByteArray()} }, stream); return newGuid; }