/// <summary> /// Converts standard Stream to IDataStream. /// </summary> /// <param name="stream">Stream to convert. Must be readable.</param> /// <returns></returns> /// <remarks> /// Note that if stream doesn't support seeking then it will be read entirely to memory. /// </remarks> public static IDataStream ToIDataStream(Stream stream) { if (!stream.CanRead) { throw new ArgumentException("Stream must be readable!"); } if (stream.CanSeek) { return(new FromStream(stream)); } else { var list = new StreamList(); int bytesRead = 0; var buffer = new byte[4096]; do { // ByteArray's constructor copies the data, so there is no need to clear the buffer bytesRead = stream.Read(buffer, 0, buffer.Length); if (bytesRead > 0) { list.Add(new ByteArray(buffer, 0, bytesRead)); } } while (bytesRead > 0); list.Freeze(); return(list); } }
/// <summary> /// Returns resulting IDataStream, the builder cannot be used anymore after calling this method. /// </summary> /// <returns></returns> public IDataStream Close() { logger = null; if (length >= threshold) { return(fileBuilder.Close()); } list.Freeze(); return(list); }