<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic .Net SDK on UWP download stream sends empty buffers on read in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/228908#M12407</link>
    <description>&lt;P&gt;Hi Dropbox dev community!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We're having a bit of a problem with the .Net SDK on our UWP app : we're using &lt;STRONG&gt;response.GetContentAsStreamAsync()&lt;/STRONG&gt; after getting the path of a file on a dropbox account, for progression measurement and having it non blocking for our UI (context : we're on a Hololens inside a Unity app). We used to go for &lt;STRONG&gt;response.GetContentAsByteArrayAsync()&lt;/STRONG&gt;&amp;nbsp;before but on bigger files, the hololens memory just doesn't hold up and usually crashes the entire download task. So we went for a stream for progressively downloading and saving the file locally. This is our current code :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ulong fileSize = response.Response.Size;
int bufferSize = 4 * 1024 * 1024;
var buffer = new byte[bufferSize];
ulong downloadProgress = 0;
using (var stream = await response.GetContentAsStreamAsync())
{
    int byteReadLength = stream.Read(buffer, 0, bufferSize);
    while (byteReadLength &amp;gt; 0)
    {
        using (var outputStream = fileStream.GetOutputStreamAt(downloadProgress))
        {
            using (var writer = new DataWriter(outputStream))
            {
                writer.WriteBytes(buffer);
                await writer.StoreAsync();
                await outputStream.FlushAsync();
            }
        }
        downloadProgress += (ulong)buffer.Length;

        if (downloadProgress &amp;gt; fileSize)
            break;
        if (downloadProgress + (ulong)bufferSize &amp;gt; fileSize)
        {
            bufferSize = (int)(fileSize - downloadProgress);
        }
        byteReadLength = stream.Read(buffer, 0, bufferSize);
    }
}&lt;/PRE&gt;&lt;P&gt;First things first : on usage, this code seems to work, the streaming seems to happen chunk by chunk of 4mb and the save to file does create an adequately sized result on the hard drive. &lt;STRONG&gt;Keep in mind this runs on a Hololens, the main reason we force the file save during the stream read loop is because we do not have the memory capacity to fully load big (300mb+) files into RAM&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But... when trying to open the downloaded file locally, may it be a video, a (really big) text file or a picture, it's always corrupted. And further analysis of the file indicates that :&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;the first 4mb chunk went through correctly&lt;/LI&gt;&lt;LI&gt;the rest is just 0x00 bytes with occasional nonsensical clutter every 100mbs or so&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;So, we kinda have a few questions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;are we doing something wrong?&lt;/LI&gt;&lt;LI&gt;why is there no EOF on stream? We had to do the downloadProgress check stuff to check we weren't getting more data than was expected, because we managed to fill up a Hololens' entire HDD with one download that never ended&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Small remark : yes, we know the SDK is not really compatible with Unity, but at this point in the app, the context is full UWP, completely separate from the Unity main thread. Also, it works with the&amp;nbsp;&lt;STRONG&gt;GetContentAsByteArrayAsync()&amp;nbsp;&lt;/STRONG&gt;method, only that the Hololens doesn't like getting blasted with so much live memory usage.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 May 2019 09:21:20 GMT</pubDate>
    <dc:creator>Opuscope</dc:creator>
    <dc:date>2019-05-29T09:21:20Z</dc:date>
    <item>
      <title>.Net SDK on UWP download stream sends empty buffers on read</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/228908#M12407</link>
      <description>&lt;P&gt;Hi Dropbox dev community!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We're having a bit of a problem with the .Net SDK on our UWP app : we're using &lt;STRONG&gt;response.GetContentAsStreamAsync()&lt;/STRONG&gt; after getting the path of a file on a dropbox account, for progression measurement and having it non blocking for our UI (context : we're on a Hololens inside a Unity app). We used to go for &lt;STRONG&gt;response.GetContentAsByteArrayAsync()&lt;/STRONG&gt;&amp;nbsp;before but on bigger files, the hololens memory just doesn't hold up and usually crashes the entire download task. So we went for a stream for progressively downloading and saving the file locally. This is our current code :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ulong fileSize = response.Response.Size;
int bufferSize = 4 * 1024 * 1024;
var buffer = new byte[bufferSize];
ulong downloadProgress = 0;
using (var stream = await response.GetContentAsStreamAsync())
{
    int byteReadLength = stream.Read(buffer, 0, bufferSize);
    while (byteReadLength &amp;gt; 0)
    {
        using (var outputStream = fileStream.GetOutputStreamAt(downloadProgress))
        {
            using (var writer = new DataWriter(outputStream))
            {
                writer.WriteBytes(buffer);
                await writer.StoreAsync();
                await outputStream.FlushAsync();
            }
        }
        downloadProgress += (ulong)buffer.Length;

        if (downloadProgress &amp;gt; fileSize)
            break;
        if (downloadProgress + (ulong)bufferSize &amp;gt; fileSize)
        {
            bufferSize = (int)(fileSize - downloadProgress);
        }
        byteReadLength = stream.Read(buffer, 0, bufferSize);
    }
}&lt;/PRE&gt;&lt;P&gt;First things first : on usage, this code seems to work, the streaming seems to happen chunk by chunk of 4mb and the save to file does create an adequately sized result on the hard drive. &lt;STRONG&gt;Keep in mind this runs on a Hololens, the main reason we force the file save during the stream read loop is because we do not have the memory capacity to fully load big (300mb+) files into RAM&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But... when trying to open the downloaded file locally, may it be a video, a (really big) text file or a picture, it's always corrupted. And further analysis of the file indicates that :&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;the first 4mb chunk went through correctly&lt;/LI&gt;&lt;LI&gt;the rest is just 0x00 bytes with occasional nonsensical clutter every 100mbs or so&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;So, we kinda have a few questions:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;are we doing something wrong?&lt;/LI&gt;&lt;LI&gt;why is there no EOF on stream? We had to do the downloadProgress check stuff to check we weren't getting more data than was expected, because we managed to fill up a Hololens' entire HDD with one download that never ended&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Small remark : yes, we know the SDK is not really compatible with Unity, but at this point in the app, the context is full UWP, completely separate from the Unity main thread. Also, it works with the&amp;nbsp;&lt;STRONG&gt;GetContentAsByteArrayAsync()&amp;nbsp;&lt;/STRONG&gt;method, only that the Hololens doesn't like getting blasted with so much live memory usage.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you &lt;img class="lia-deferred-image lia-image-emoji" src="https://www.dropboxforum.com/html/@FBF7D2AB59A0D6E861EBF6A36F93B7E2/emoticons/1f642.png" alt=":slightly_smiling_face:" title=":slightly_smiling_face:" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:21:20 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/228908#M12407</guid>
      <dc:creator>Opuscope</dc:creator>
      <dc:date>2019-05-29T09:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: .Net SDK on UWP download stream sends empty buffers on read</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/228996#M12429</link>
      <description>&lt;P&gt;We looked into this, and we believe the issue here is due to this possible behavior from &lt;A href="https://msdn.microsoft.com/en-us/library/system.io.stream.read%28v=vs.110%29.aspx" target="_self"&gt;Stream.Read&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Return Value
Type: System.Int32
The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That means that&amp;nbsp;byteReadLength in the code snippet here won't always necessarilly be a whole 4 MB. The being the case, it looks like you'll need to make two changes:&lt;/P&gt;
&lt;P&gt;- when you write the data to the file, only write as much as&amp;nbsp;byteReadLength&lt;/P&gt;
&lt;P&gt;- when you increment your&amp;nbsp;downloadProgress, only add as much as&amp;nbsp;byteReadLength&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2017 00:11:58 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/228996#M12429</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2017-06-24T00:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: .Net SDK on UWP download stream sends empty buffers on read</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/229259#M12446</link>
      <description>&lt;P&gt;it works much better indeed! Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jun 2017 09:34:23 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Net-SDK-on-UWP-download-stream-sends-empty-buffers-on-read/m-p/229259#M12446</guid>
      <dc:creator>Opuscope</dc:creator>
      <dc:date>2017-06-26T09:34:23Z</dc:date>
    </item>
  </channel>
</rss>

