コード例 #1
0
        /// <summary>
        /// Opens the specified file for reading.
        /// </summary>
        /// <param name="dataStorePath">The data store path specifying the file to open.</param>
        /// <returns>An <see cref="ITextReader"/> representing the file opened.</returns>
        public ITextReader ReadText( string dataStorePath )
        {
            try
            {
                if( dataStorePath.NullOrEmpty()
                 || !DataStore.IsValidPath(dataStorePath) )
                    throw new ArgumentException("Invalid data store path!").StoreFileLine();

                lock( this.syncLock )
                {
                    var node = this.GetNodeAt(dataStorePath);
                    if( node.NullReference() )
                        throw new FileNotFoundException("File system entry not found!").StoreFileLine();
                    if( !(node is IDataStoreValue) )
                        throw new FileNotFoundException("File system entry is not a file!").StoreFileLine();

                    var textValue = node as IDataStoreTextValue;
                    if( textValue.NotNullReference() )
                    {
                        var reader = new StringReader();
                        reader.Set(textValue.Content);
                        return reader;
                    }

                    var binaryValue = node as IDataStoreBinaryValue;
                    if( binaryValue.NotNullReference() )
                    {
                        var ms = new MemoryStream(capacity: binaryValue.Content.Count);
                        ms.Write(binaryValue.Content.Array, binaryValue.Content.Offset, binaryValue.Content.Count);
                        ms.Position = 0;
                        return IOWrapper.ToTextReader(ms);
                    }

                    throw new Exception("Data store node value is neither text, nor binary based!").Store("nodeType", node.GetType());
                }
            }
            catch( Exception ex )
            {
                ex.Store("dataStorePath", dataStorePath);
                throw;
            }
        }