//--------------------------------------------------------------------- public override void ReadFileContent(Commands.ReadFileContentCommand Command) { if (this._Root.Length == 0) { throw new Exceptions.InvalidOperationException("ReadContent", "FileSystem root is undefined"); } Pathname ItemPathname = Pathname.Append(this._Root, Command.in_Pathname); using (FileStream stream = File.OpenRead(ItemPathname)) { if ((Command.in_Offset < 0) || (Command.in_Offset >= stream.Length)) { throw new Exceptions.InvalidOperationException("ReadContent", "Offset is outside of expected range."); } long nLength = Command.in_Length; if (nLength < 0) { nLength = (stream.Length - Command.in_Offset); } if ((Command.in_Offset + nLength) > stream.Length) { nLength = (stream.Length - Command.in_Offset); } byte[] bytes = new byte[nLength]; stream.Position = Command.in_Offset; int cb = stream.Read(bytes, 0, bytes.Length); if (cb != nLength) { throw new Exceptions.InvalidOperationException("ReadContent", "Read error."); } Command.out_Content = bytes; } return; }
//--------------------------------------------------------------------- public override void ReadFileContent(Commands.ReadFileContentCommand Command) { if (this._ShellAdapter.Root.Length == 0) { throw new Exceptions.InvalidOperationException("ReadContent", "FileSystem root is undefined"); } string syspath = this.MakeSystemPathname(this._ShellAdapter.Root, Command.in_Pathname); // Check file. Commands.ReadItemCommand ReadCmd = new Commands.ReadItemCommand(); ReadCmd.in_Pathname = syspath; this.Read(ReadCmd); if (ReadCmd.out_Item == null) { throw new Exceptions.InvalidOperationException("ReadContent", "Item does not exist."); } if (ReadCmd.out_Item.IsFolder) { throw new Exceptions.InvalidOperationException("ReadContent", "Cannot read content from a folder."); } if ((Command.in_Offset < 0) || (Command.in_Offset >= ReadCmd.out_Item.Size)) { throw new Exceptions.InvalidOperationException("ReadContent", "Offset is outside of expected range."); } // Get file. string TempFilename = Path.GetTempFileName(); string ShellCommand = "get -preservetime " + syspath + " " + TempFilename; string output = this._ShellAdapter.ExecuteCommand(ShellCommand); string[] lines = output.Split(new string[] { "\n" }, StringSplitOptions.None); // Read file bytes. using (FileStream stream = File.OpenRead(TempFilename)) { long nLength = Command.in_Length; if (nLength < 0) { nLength = (stream.Length - Command.in_Offset); } if ((Command.in_Offset + nLength) > stream.Length) { nLength = (stream.Length - Command.in_Offset); } byte[] bytes = new byte[nLength]; stream.Position = Command.in_Offset; int cb = stream.Read(bytes, 0, bytes.Length); if (cb != nLength) { throw new Exceptions.InvalidOperationException("ReadContent", "Read error."); } Command.out_Content = bytes; } // Return, OK. return; }
//--------------------------------------------------------------------- public void Invoke(CommandContext Command) { if (Command == null) { } else if (Command is Commands.SettingsCommand) { Commands.SettingsCommand SettingsCommand = (Commands.SettingsCommand)Command; this.Settings(SettingsCommand); } else if (Command is Commands.ListEntriesCommand) { Commands.ListEntriesCommand ListCommand = (Commands.ListEntriesCommand)Command; this.List(ListCommand); } else if (Command is Commands.CreateItemCommand) { Commands.CreateItemCommand CreateCommand = (Commands.CreateItemCommand)Command; this.Create(CreateCommand); } else if (Command is Commands.ReadItemCommand) { Commands.ReadItemCommand ReadCommand = (Commands.ReadItemCommand)Command; this.Read(ReadCommand); } else if (Command is Commands.UpdateItemCommand) { Commands.UpdateItemCommand UpdateCommand = (Commands.UpdateItemCommand)Command; this.Update(UpdateCommand); } else if (Command is Commands.DeleteItemCommand) { Commands.DeleteItemCommand DeleteCommand = (Commands.DeleteItemCommand)Command; this.Delete(DeleteCommand); } else if (Command is Commands.ReadFileContentCommand) { Commands.ReadFileContentCommand ReadContentCommand = (Commands.ReadFileContentCommand)Command; this.ReadFileContent(ReadContentCommand); } else if (Command is Commands.WriteFileContentCommand) { Commands.WriteFileContentCommand WriteContentCommand = (Commands.WriteFileContentCommand)Command; this.WriteFileContent(WriteContentCommand); } else { } return; }
public abstract void ReadFileContent(Commands.ReadFileContentCommand Command);
//--------------------------------------------------------------------- public byte[] ReadFileContent(string Pathname, int Offset, int Length) { Commands.ReadFileContentCommand ReadContentCommand = new Commands.ReadFileContentCommand(Pathname, Offset, Length); this.ReadFileContent(ReadContentCommand); return(ReadContentCommand.out_Content); }
//-------------------------------------------------------------------------------- public override void ReadFileContent(Commands.ReadFileContentCommand Command) { throw new NotImplementedException(); }