Exemplo n.º 1
0
 public static object DeserializeFromStream(Type type, Stream stream)
 {
     if (stream == null)
     {
         return(null);
     }
     using (var reader = new StreamReaderX(stream, UseEncoding))
     {
         return(DeserializeFromString(type, reader.ReadToEnd()));
     }
 }
Exemplo n.º 2
0
 public static T DeserializeFromStream <T>(Stream stream)
 {
     if (stream == null)
     {
         return(default(T));
     }
     using (var reader = new StreamReaderX(stream, UseEncoding))
     {
         return(DeserializeFromString <T>(reader.ReadToEnd()));
     }
 }
Exemplo n.º 3
0
        public static IEnumerable <string> ReadLines(this Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var reader = new StreamReaderX(stream))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    yield return(line);
                }
            }
        }
        public static Task <string> ReadToEndAsync(this Stream stream, Encoding encoding)
        {
            if (stream is MemoryStream ms)
            {
                return(ms.ReadToEndAsync());
            }

            if (stream.CanSeek)
            {
                stream.Position = 0;
            }

            using (var reader = new StreamReaderX(stream, encoding, true, DefaultBufferSize, leaveOpen: true))
            {
                return(reader.ReadToEndAsync());
            }
        }
        public static Task <string> ReadToEndAsync(this MemoryStream ms, Encoding encoding)
        {
            ms.Position = 0;
            try
            {
                var ret = encoding.GetString(ms.GetBuffer(), 0, (int)ms.Length);
                return(ret.InTask());
            }
            catch (UnauthorizedAccessException e)
            {
                Tracer.Instance.WriteWarning("MemoryStream in ReadToEndAsync() wasn't created with a publiclyVisible:true byte[] bufffer, falling back to slow impl");

                using (var reader = new StreamReaderX(ms, encoding, true, DefaultBufferSize, leaveOpen: true))
                {
                    return(reader.ReadToEndAsync());
                }
            }
        }