コード例 #1
0
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            // Open or create the file through the underlying (IContentIndexMap, IOdbBackend) couple.
            // Also read/write a ObjectHeader.
            if (mode == VirtualFileMode.Open)
            {
                ObjectId objectId;
                if (url.StartsWith(ObjectIdUrl))
                {
                    ObjectId.TryParse(url.Substring(ObjectIdUrl.Length), out objectId);
                }
                else if (!contentIndexMap.TryGetValue(url, out objectId))
                {
                    throw new FileNotFoundException(string.Format("Unable to find the file [{0}]", url));
                }

                var result = objectDatabase.OpenStream(objectId, mode, access, share);

                // copy the stream into a memory stream in order to make it seek-able
                if (streamFlags == StreamFlags.Seekable && !result.CanSeek)
                {
                    var buffer = new byte[result.Length - result.Position];
                    result.Read(buffer, 0, buffer.Length);
                    return(new DatabaseReadFileStream(objectId, new MemoryStream(buffer), 0));
                }

                return(new DatabaseReadFileStream(objectId, result, result.Position));
            }

            if (mode == VirtualFileMode.Create)
            {
                if (url.StartsWith(ObjectIdUrl))
                {
                    throw new NotSupportedException();
                }

                var stream = objectDatabase.CreateStream();

                // Header will be written by DatabaseWriteFileStream
                var result = new DatabaseWriteFileStream(stream, stream.Position);

                stream.Disposed += x =>
                {
                    // Commit index changes
                    contentIndexMap[url] = x.CurrentHash;
                };

                return(result);
            }

            throw new ArgumentException("mode");
        }
コード例 #2
0
        /// <inheritdoc/>
        public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamFlags = StreamFlags.None)
        {
            // Open or create the file through the underlying (IContentIndexMap, IOdbBackend) couple.
            // Also read/write a ObjectHeader.
            if (mode == VirtualFileMode.Open)
            {
                ObjectId objectId;
                if (url.StartsWith(ObjectIdUrl))
                    ObjectId.TryParse(url.Substring(ObjectIdUrl.Length), out objectId);
                else if (!contentIndexMap.TryGetValue(url, out objectId))
                    throw new FileNotFoundException(string.Format("Unable to find the file [{0}]", url));

                var result = objectDatabase.OpenStream(objectId, mode, access, share);

                // copy the stream into a memory stream in order to make it seek-able
                if (streamFlags == StreamFlags.Seekable && !result.CanSeek)
                {
                    var buffer = new byte[result.Length - result.Position];
                    result.Read(buffer, 0, buffer.Length);
                    return new DatabaseReadFileStream(objectId, new MemoryStream(buffer), 0);
                }

                return new DatabaseReadFileStream(objectId, result, result.Position);
            }

            if (mode == VirtualFileMode.Create)
            {
                if (url.StartsWith(ObjectIdUrl))
                    throw new NotSupportedException();

                var stream = objectDatabase.CreateStream();

                // Header will be written by DatabaseWriteFileStream
                var result = new DatabaseWriteFileStream(stream, stream.Position);

                stream.Disposed += x =>
                    {
                        // Commit index changes
                        contentIndexMap[url] = x.CurrentHash;
                    };

                return result;
            }

            throw new ArgumentException("mode");
        }