Olá pessoal, o meu código está rodando, mas com performance ruim na velocidade de transferencia,vi exemplos com InputStream e OUtputStrem, estou usando DataInputStream e DataoutputStream, como faço para usar esse método? ou o que faço para melhorar na velocidade da transferencia?
Obrigado
public void downFile(String fileClient) {
try {
inputMsg = new DataInputStream(MyCliente.getInputStream());
output = new DataOutputStream(new FileOutputStream(fileClient));
int readByte = inputMsg.read();
while (readByte !=-1) {
output.write((byte)readByte);
readByte = inputMsg.read();
}
System.out.println("Download concluído!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void upFile(String fileServer) {
File file = new File(fileServer);
byte[] cache = new byte[10240];
int size = 0;
try {
outputMsg = new DataOutputStream(MyCliente.getOutputStream());
input = new DataInputStream(new FileInputStream(file));
while ((size = input.read(cache)) > -1) {
outputMsg.write(cache, 0, size);
}
System.out.println("Upload concluído!");
} catch (IOException e) {
e.printStackTrace();
}
}
Veja se isso melhora um pouco. Dicas:
a) Não leia ou escreva 1 byte de cada vez. Isso é um verdadeiro veneno.
b) Se puder, use sempre BufferedAlgumaCoisa, como fiz abaixo.
public void downFile(String fileClient) {
BufferedInputStream inputMsg = null;
BufferedOutputStream output = null;
try {
inputMsg = new BufferedInputStream (MyCliente.getInputStream());
output = new BufferedOutputStream (new FileOutputStream (fileClient));
byte[] buffer = new byte[8 * 1024];
for (int nBytesLidos = inputMsg.read (buffer, 0, buffer.length); nBytesLidos > 0; nBytesLidos = inputMsg.read (buffer, 0, buffer.length)) {
output.write (buffer, 0, nBytesLidos);
}
System.out.println("Download concluído!");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (output != null) try { output.close(); } catch (IOException ex) { }
if (inputMsg != null) try { inputMsg.close(); } catch (IOException ex) { }
}
}
public void upFile (String fileServer) {
DataO
}
public void upFile(String fileServer) {
BufferedOutputStream outputMsg = null;
BufferedInputStream input = null;
try {
outputMsg = new BufferedOutputStream (MyCliente.getOutputStream());
input = new BufferedInputStream (new FileInputStream (fileServer));
byte[] buffer = new byte[8 * 1024];
for (int nBytesLidos = input.read (buffer, 0, buffer.length); nBytesLidos > 0; nBytesLidos = input.read (buffer, 0, buffer.length)) {
outputMsg.write (buffer, 0, nBytesLidos);
}
System.out.println("Download concluído!");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (outputMsg != null) try { outputMsg.close(); } catch (IOException ex) { }
if (input != null) try { input.close(); } catch (IOException ex) { }
}
}