Ejemplo n.º 1
0
        public static byte[] ReadAsByteArray(this Stream stream)
        {
            stream.ArgumentNullCheck("stream");

            using (var ms = new MemoryStream()) {
                CopyTo(stream, ms);
                return ms.ToArray();
            }
        }
Ejemplo n.º 2
0
        public static void ToFile(this Stream stream, string path)
        {
            stream.ArgumentNullCheck("stream");
            if (String.IsNullOrEmpty(path))
                throw new ArgumentException("path is null or empty");

            using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                stream.CopyTo(fs);
        }
Ejemplo n.º 3
0
        public static string ReadAsString(this Stream stream, Encoding encoding = null)
        {
            stream.ArgumentNullCheck("stream");

            var reader = encoding != null
                ? new StreamReader(stream, encoding)
                : new StreamReader(stream);

            return reader.ReadToEnd();
        }
Ejemplo n.º 4
0
        public static XmlDocument ReadAsXmlDocument(this Stream stream)
        {
            stream.ArgumentNullCheck("stream");

            var document = new XmlDocument {
                PreserveWhitespace = true
            };

            document.LoadXml(stream.ReadAsString());

            return document;
        }
Ejemplo n.º 5
0
        public static void CopyTo(this Stream source, Stream target, int bufferLength)
        {
            source.ArgumentNullCheck("source");
            target.ArgumentNullCheck("target");

            var buffer = new byte[bufferLength];
            int count;
            source.Reset();
            do {
                count = source.Read(buffer, 0, buffer.Length);
                target.Write(buffer, 0, count);
            }
            while (count > 0);
        }
Ejemplo n.º 6
0
 public static Stream ToStream(this byte[] bytes)
 {
     return new MemoryStream(bytes.ArgumentNullCheck("bytes"));
 }
Ejemplo n.º 7
0
 public static TempFile ToTempFile(this Stream stream)
 {
     var res = new TempFile();
     res.ProcessPath(path => stream.ArgumentNullCheck("stream").ToFile(path));
     return res;
 }
Ejemplo n.º 8
0
 public static MemoryStream ToMemoryStream(this Stream stream)
 {
     return new MemoryStream()
         .Do(stream.ArgumentNullCheck("stream").CopyTo)
         .Do(s => s.Position = 0);
 }
Ejemplo n.º 9
0
 public static void Reset(this Stream source)
 {
     source.ArgumentNullCheck("source");
     if (source.CanSeek)
         source.Seek(0, SeekOrigin.Begin);
 }
Ejemplo n.º 10
0
 public static string ReadAsBase64(this Stream stream)
 {
     return Convert.ToBase64String(stream.ArgumentNullCheck("stream").ReadAsByteArray());
 }