EldoS | Feel safer!

Software components for data protection, secure storage and transfer

OnReadFile fails on large files

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#36814
Posted: 05/27/2016 02:42:40
by Lutz Moeller (Basic support level)
Joined: 05/27/2016
Posts: 8

Hallo,
I've got a trial key of your CBFS product and started evaluating about 2 days ago. The samples are great and very helpful, but now I've got a problem where I dont find a solution in the samples neither the forum.

My scenario is as follows: I've got a list of file and directory information, which I transfer to the VirtualFile-Class like in the VDisk example to display it within the EnumerateDirectory eventhandler. Everything works fine and displays correct.
I've implemented also the handlers for OnOpenFile and OnReadFile. At the OnOpenFile method I check for the desiredaccess. If the user want to open the file in a program, I initialize a stream to the actual file from an external storage. Then at the OnReadFile method I read from the stream with the given parameters.

Works pretty well on small files (about less than some kb). If I try to open bigger files I got an ArgumentOutOfRangeException. I find out that the "Position" I get from the event in this case is higher than the length of the given buffer. When I open a small file, I get a buffer that's big enough for the whole file and just read the bytes into it, that the given parameters define. On large files the buffer is only the size of the given BytesToRead. I then try to write always from position 0 to the buffer. It seems to load the file step by step and I got no exception but the opened program at last fails because the loaded file is corrupt or broken.

To the point of my question: how can I handle this? Is there something I miss?

Here is my code of the OnReadFile:
Code
private void CbFsReadFile(CallbackFileSystem sender, CbFsFileInfo FileInfo, long Position, byte[] Buffer, int BytesToRead, ref int BytesRead)
{
   try
   {
      Console.WriteLine(string.Format("CbFsReadFile: {0}; Position: {1}; BufferLength: {2}; BytesToRead: {3}; BytesRead: {4}", FileInfo.FileName, Position, Buffer.Length, BytesToRead, BytesRead));

      int pos = (int)Position;
      if(pos + BytesToRead > Buffer.Length)
      {
         //Comment next line to get the argumentoutofrangeexception
         pos = 0;
      }

      do
      {
         int n = _stream.Read(Buffer, pos, BytesToRead);
         if (n == 0)
            break;
         BytesRead += n;
         pos += n;
         BytesToRead -= n;
      } while (BytesToRead > 0);
   }
   catch(ArgumentOutOfRangeException aoe)
   {
      Console.WriteLine(aoe.Message);
      Console.WriteLine("Streamlength: " + _stream.Length);
   }
}

Thank you a lot.
Best regards
Lutz Moeller
#36820
Posted: 05/27/2016 04:01:49
by Volodymyr Zinin (EldoS Corp.)

Hello Lutz,

Thank you for interesting of our product. The Position parameter of the OnRead callback specifies the position inside the file from which you should start reading. The obtained data should be placed to the output buffer from zero position. I.e.
Code
private void CbFsReadFile(CallbackFileSystem sender, CbFsFileInfo FileInfo, long Position, byte[] Buffer, int BytesToRead, ref int BytesRead)
{
  try
  {
    int pos = 0;
    _stream.Position = Position

    do
    {
      int n = _stream.Read(Buffer, pos, BytesToRead);
      if (n == 0)
        break;
      pos += n;
      BytesToRead -= n;
    } while (BytesToRead > 0);
  }
  catch(ArgumentOutOfRangeException aoe)
  {
    Console.WriteLine(aoe.Message);
    Console.WriteLine("Streamlength: " + _stream.Length);
  }
}
#36821
Posted: 05/27/2016 04:31:46
by Lutz Moeller (Basic support level)
Joined: 05/27/2016
Posts: 8

Hi Volodymyr,

thank you! I can't believe I miss that point.
Sorry for my missunderstanding and thanks a lot for the really fast support.

Reply

Statistics

Topic viewed 1266 times

Number of guests: 2, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!