commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: [net]
Date Wed, 06 Oct 2010 14:49:58 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pablo,

On 10/5/2010 10:48 PM, Pablo Pinto wrote:
> I'm using the FTP API and I need to use a limited bandwidth.
> 
> The following code works pretty good except for one problem. At
> the beginning of the transference the program buffers 64KB of data using all
> the available bandwidth. After that I can control the bandwidth.
> 
> OutputStream outputStream = new FileOutputStream(localFile);
> InputStream inputStream = client.retrieveFileStream(remoteFile);

What type of InputStream are you getting back from
client.retrieveFileStream? Maybe you can cast it to it's real type and
call a method like setBufferSize?

> byte buf[] = new byte[bufferSize];

What is the value of bufferSize?

> int bytesRead = inputStream.read(buf);
> while (bytesRead != -1)
> {
> outputStream.write(buf, 0, bytesRead);
> Thread.sleep(delay);

You could make this smarter by timing the entire transaction and
adjusting the sleep delay to attempt to achieve a specific bandwidth target.

> bytesRead = inputStream.read(buf);

Might I suggest a shorter loop?

int bytesRead;
while (-1 != (bytesRead != inputStream.read(buf)))
{
  outputStream.write(buf, 0, bytesRead);
  // sleep logic
}

inputStream.close(); // probably should be in a finally block

> Is it possible to decrease the amount of data that is buffered at the
> begining?

That's up to the InputStream implementation provided by the "client" object.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkysjJYACgkQ9CaO5/Lv0PB6lQCfRWBvHA+MPxbpPEX1H/TW7phK
AKgAniRpTwEOdB3V3+qfhWk8OiDRnHyb
=mhPb
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message