예제 #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()));
     }
 }
예제 #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()));
     }
 }
        public static string ReadToEnd(this Stream stream, Encoding encoding)
        {
            if (stream is MemoryStream ms)
            {
                return(ms.ReadToEnd());
            }

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

            using (var reader = new StreamReaderX(stream, encoding, true, DefaultBufferSize, leaveOpen: true))
            {
                return(reader.ReadToEnd());
            }
        }
        public static string ReadToEnd(this MemoryStream ms, Encoding encoding)
        {
            ms.Position = 0;
            try
            {
                var ret = encoding.GetString(ms.GetBuffer(), 0, (int)ms.Length);
                return(ret);
            }
            catch (UnauthorizedAccessException e)
            {
                Tracer.Instance.WriteWarning("MemoryStream 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.ReadToEnd());
                }
            }
        }