/// <summary> /// Read the next unread record from the stream. Will return <see cref="QueueStreamRecord.Empty"/> if there is no next record in the stream. /// </summary> /// <returns></returns> public QueueStreamRecord ReadNextRecord() { EnsureInstanceNotDisposed(); if (_recordQueue.Count == 0) { return(QueueStreamRecord.Empty); } var next = _recordQueue.Dequeue(); var streamInfo = GetStreamInfoFromHandle(next); if (streamInfo == null) { EnsureInstanceNotDisposed(); throw new InvalidOperationException("unable to access stream for head file"); } streamInfo.Stream.Seek(next.Position + HEADER_SIZE, SeekOrigin.Begin); var data = new ChunkedMemoryStream(); var copied = streamInfo.Stream.CopyTo(data, next.Length); if (copied != next.Length) { _log.WarnFormat("reached EOF in the middle of the record"); return(QueueStreamRecord.Empty); } data.Seek(0, SeekOrigin.Begin); return(new QueueStreamRecord(data, next)); }
//--- Methods --- /// <summary> /// Serialize an <see cref="XDoc"/> to a <see cref="Stream"/> /// </summary> /// <param name="doc">An <see cref="XDoc"/> document</param> /// <returns>A <see cref="Stream"/></returns> public Stream ToStream(XDoc doc) { var data = new ChunkedMemoryStream(); doc.WriteTo(data); data.Position = 0; return(data); }
/// <summary> /// WARNING: This method is thread-blocking. Please avoid using it if possible. /// </summary> public static Result <ChunkedMemoryStream> ToChunkedMemoryStream(this Stream stream, long length, Result <ChunkedMemoryStream> result) { var copy = new ChunkedMemoryStream(); stream.CopyTo(copy, length, new Result <long>(TimeSpan.MaxValue)).WhenDone( v => { copy.Position = 0; result.Return(copy); }, result.Throw ); return(result); }
/// <summary> /// Read the next unread record from the stream. Will return <see cref="QueueStreamRecord.Empty"/> if there is no next record in the stream. /// </summary> /// <returns></returns> public QueueStreamRecord ReadNextRecord() { if (_recordQueue.Count == 0) { return(QueueStreamRecord.Empty); } var next = _recordQueue.Dequeue(); var recordLength = _recordMap[next]; _stream.Seek(next + HEADER_SIZE, SeekOrigin.Begin); var data = new ChunkedMemoryStream(); var copied = _stream.CopyTo(data, recordLength); if (copied != recordLength) { _log.WarnFormat("reached EOF in the middle of the record"); return(QueueStreamRecord.Empty); } data.Position = 0; return(new QueueStreamRecord(data, new QueueStreamHandle(next, _generation))); }