TotalCross Forums
September 10, 2010, 07:28:22 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: TotalCross - the heaven of mobile application development
 
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Progress Bar para a classe FTP  (Read 812 times)
KalilMaciel
Full Member
***
Posts: 198


The evil just wins when good men cross their arms.

kalilmaciel@hotmail.com
View Profile Email
« on: January 14, 2010, 05:14:05 pm »

Olá todos

Voltando ao forum gostaria de sugerir que ficasse como padrão uma pequena modificação que fiz na classe FTP. Esta classe tem um atributo que é o ProgressInformation, mas esse atributo só mostra quanto foi transferido do arquivo e não a porcentagem da transferência. Fiz uma pequena modificação no método receiveFile da classe FTP. Eu passo mais um parâmetro (um objeto ProgressBar) para que este me mostre a porcentagen da transferência do arquivo. Abaixo segue a modificação que fiz e fica aqui a sugestão para torna-la padrão.

Code:
public int receiveFile(String pathName, Stream outputStream, ProgressBar pbar)
throws FTPConnectionClosedException, totalcross.io.IOException,
InvalidNumberException {
if (pathName == null)
throw new NullPointerException(
"Argument 'pathName' cannot have a null value.");
if (outputStream == null)
throw new NullPointerException(
"Argument 'outputStream' cannot have a null value.");

setType(BINARY);
Socket socket = openReturningPath(true);

//Kalil Maciel
String fsize = sendCommand("SIZE ", pathName, COMPLETED);
int fileSize = Convert.toInt(Convert.tokenizeString(fsize, " ")[1]);

sendCommand("RETR ", pathName, WAITING_FOR_REPLY);
int count;
int total = 0;
log("0 bytes received");
if (progressInfo != null)
progressInfo.transfering(0);
int tries = 3;
if (buffer == null)
buffer = new byte[2048];
while ((count = socket.readBytes(buffer)) >= 0) // guich@561_8: != -1 ->
// > 0
{
if (count > 0) {
tries = 3;
total += count;
logReplace(total + " bytes received");
if (progressInfo != null)
progressInfo.transfering(total);
outputStream.writeBytes(buffer, 0, count);

pbar.setValue((total*100)/fileSize); //KalilMaciel

} else if (tries-- == 0) {
try {
checkResponse(COMPLETED);
break;
} catch (totalcross.io.IOException e) {
log("Cannot read from server.|Received " + total
+ " bytes. Error: " + e.getMessage());
throw e;
}
}
}
socket.close();
return total;
}

Até mais galera Smiley
Logged
guich
Administrator
Sr. Member
*****
Posts: 471



View Profile WWW Email
« Reply #1 on: February 06, 2010, 08:52:21 pm »

Obrigado Kalil! Muito util sua dica. Incorporei na versao 1.16.

abraços

    Guilherme
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!