Ejemplo n.º 1
0
 /// <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);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Appends data to builded stream
 /// </summary>
 /// <remarks>
 /// With large lengthThreshold it will take a while to create the LoggedFile.
 /// </remarks>
 /// <param name="data"></param>
 /// <exception cref="ArgumentNullException">data</exception>
 public void Append(IDataStream data)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data");
     }
     if (length >= threshold)
     {
         fileBuilder.Append(data);
     }
     else if (length + data.Length >= threshold)
     {
         fileBuilder = logger.CreateFileBuilder();
         fileBuilder.Append(list);
         fileBuilder.Append(data);
         list = null;
     }
     else
     {
         list.Add((IDataStream)data.Clone());
     }
     length += data.Length;
 }