Пример #1
0
        public virtual Uri Write(Stream source, Uri sink)
        {
            if (sink == null)
            {
                throw new ArgumentException("Uri must not be null");
            }

            if (IoMode.HasFlag(IO.IoMode.Write) && sink.IsFile)
            {
                var filename   = IoUtils.UriToFileName(sink);
                var file       = new FileStream(filename, FileMode.Create);
                var target     = new BufferedStream(file);
                var bufferSize = 1024 * 1024;
                var buffer     = new byte[bufferSize];
                var oldPos     = source.Position;
                int readByte   = 0;
                int position   = 0;

                long endpos = source.Length - 1;
                while (position < endpos)
                {
                    readByte = source.Read(buffer, 0, bufferSize);
                    target.Write(buffer, 0, readByte);
                    position += readByte;
                }

                target.Flush();
                target.Close();
                source.Position = oldPos;
            }
            return(sink);
        }
Пример #2
0
        public virtual Stream Read(Uri uri)
        {
            var result = default(Stream);

            if (IoMode.HasFlag(IO.IoMode.Read) && uri.IsFile)
            {
                var filename = IoUtils.UriToFileName(uri);
                var file     = new FileStream(filename, FileMode.Open);
            }
            return(result);
        }
Пример #3
0
        public virtual Content <Stream> Read(Uri source, Content <Stream> sink)
        {
            var result = default(Content <Stream>);

            if (IoMode.HasFlag(IO.IoMode.Read) && source.IsFile)
            {
                var filename = IoUtils.UriToFileName(source);
                var file     = new FileStream(filename, FileMode.Open);

                result = Read(file, Detector.Use(file));

                // test if there is a provider with file's extension:
                if (result == null)
                {
                    var info = Detector.Find(Path.GetExtension(filename).Replace(".", ""));
                    if (info.Magics == null || info.Magics.Length == 0)
                    {
                        result = Read(file, info);
                    }
                }

                if (result != null)
                {
                    if (result.Source == null)
                    {
                        result.Source = source.AbsoluteUri;
                    }
                    if (result.Description == null)
                    {
                        result.Description = source.Segments[source.Segments.Length - 1];
                    }
                }
                else
                {
                    file.Close();
                }
            }
            return(result);
        }