Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  transferFrom returns 0 before end of channel is reached  (Read 267 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2014-06-12 13:17:35 »

Hey, I have this code for downloading a file via HTTP and it mostly works fine. However this crashes for a friend of mine with a very poor network connection ("mobile internet", 2-10kb/s download speed) because transferFrom() returns 0 before the full file is downloaded (eg. downloaded 2327 / 3163 bytes). I know the filesize in advance, however I don't want to loop until the received amount = expectedSize because if I modify the file while someone is downloading it they'll get stuck in an infinite loop. Anyone know how I can fix this?

Thanks,
roland

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
try
{
   URL fileURL = new URL(urlOfFileToDownload);
   InputStream stream = fileURL.openStream();
   
   ReadableByteChannel rbc = Channels.newChannel(stream);
   if (!fileExists(saveLocation))
      createEmptyFile(saveLocation);
   FileOutputStream fos = FileManager.GetFileOutputStream(saveLocation);
   int totalNumBytesTransferred = 0;
   int bytesTransferred = 0;
   do
   {
      bytesTransferred = (int)fos.getChannel().transferFrom(rbc, totalNumBytesTransferred, TRANSFER_CHUNK_SIZE); //TRANSFER_CHUNK_SIZE=512*1024
      totalNumBytesTransferred += bytesTransferred;
   } while (bytesTransferred > 0);
   
   fos.close();
   
   //check file
   if (totalNumBytesTransferred != expectedFileSize) //I know expectedFileSize
      throw new UnsupportedOperationException("Failed to fully download file " + urlOfFileToDownload +": wrong filesize (expected " + expectedFileSize +", received "+totalNumBytesTransferred);
   
   success = true;
}
catch(Exception e)
{
   error = e.getMessage();
   e.printStackTrace();
}
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-06-12 14:13:54 »

Well first off, wouldn't that loop already cause an infinite loop? You're looping through the code so long as you've received bytes, which could go on forever. Is this actually working? Because it shouldn't!

Online trollwarrior1
« Reply #2 - Posted 2014-06-12 14:27:08 »

You should make it like you said, receivedAmount==ExpectedAmount;

You should also make a
1  
long = System.getCurrentTimeMillis()


of when last bytes where received.

If last bytes were received from more than N amount of time, kill the loop, and say to the user that connection has been lost, or some kind of error happened. Basically make a kill switch that would kill your loop if no bytes were received for N amount of time. (N should be time that would allow for some lag, but not big enough that user would get annoyed by looking at stuck UI)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roland
« Reply #3 - Posted 2014-06-12 14:30:16 »

You should make it like you said, receivedAmount==ExpectedAmount;

You should also make a
1  
long = System.getCurrentTimeMillis()


of when last bytes where received.

Thanks Smiley I'll try that and let you know how it goes.
Offline roland
« Reply #4 - Posted 2014-06-12 14:32:23 »

Well first off, wouldn't that loop already cause an infinite loop? You're looping through the code so long as you've received bytes, which could go on forever. Is this actually working? Because it shouldn't!
It can't go on forever because the filesize is fixed Smiley
Online trollwarrior1
« Reply #5 - Posted 2014-06-12 14:32:53 »

I thought I would try to make an easier explanation.

I received 0 bytes. Since I received 0 bytes, I will wait for 10 seconds. If I don't receive bytes > 0 after 10 seconds, I will kill the loop and say that I f**ked up.
Offline roland
« Reply #6 - Posted 2014-06-12 14:33:33 »

I got your other one fine, but thanks anyway  Grin
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (54 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (43 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!