[net]

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[net]

Pablo Pinto
Hi Everybody:
Hi Everybody:

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);

byte buf[] = new byte[bufferSize];
int bytesRead = inputStream.read(buf);
while (bytesRead != -1)
{
outputStream.write(buf, 0, bytesRead);
Thread.sleep(delay);
bytesRead = inputStream.read(buf);
}


Is it possible to decrease the amount of data that is buffered at the
begining?
Best Regards,

--
Pablo Pinto
Computación y Comunicaciones S.A.
Almirante Lorenzo Gotuzzo 124, Oficina 1500
Teléfonos : 9135704 - 9135700
Reply | Threaded
Open this post in threaded view
|

Re: [net]

Christopher Schultz-2
-----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: [hidden email]
For additional commands, e-mail: [hidden email]