Olá Pessoal
O método a seguir eu faço comparacao entre InputStream
private boolean isSame( InputStream input1, InputStream input2 ) throws IOException {
//boolean error = false;
try {
byte[] buffer1 = new byte[1024];
byte[] buffer2 = new byte[1024];
try {
int numRead1 = 0;
int numRead2 = 0;
while (true) {
numRead1 = input1.read(buffer1);
numRead2 = input2.read(buffer2);
if (numRead1 > -1) {
if (numRead2 != numRead1) return false;
// Otherwise same number of bytes read
if (!Arrays.equals(buffer1, buffer2)) return false;
// Otherwise same bytes read, so continue ...
} else {
// Nothing more in stream 1 ...
return numRead2 < 0;
}
}
} finally {
//input1.close();
}
} catch (IOException e) {
//error = true; // this error should be thrown, even if there is an error closing stream 2
throw e;
} catch (RuntimeException e) {
//error = true; // this error should be thrown, even if there is an error closing stream 2
throw e;
} finally {
// try {
// input2.close();
// } catch (IOException e) {
// if (!error) throw e;
// }
}
}
Usando o seguinte o arquivo é gravado, mas sem conteúdo:
InputStream in = conn.getInputStream();
InputStream inOld = new FileInputStream(fileOld);
//InputStream inCopia = in;
//InputStream inOldCopia = inOld;
if(isSame(in, inOld)){
System.out.println("mesmo");
}
int i=0;
while ((i = in.read()) != -1){
out.write(i);
}
in.close();
out.close();
System.out.println("Gravação efetuada com sucesso");
Se eu eliminar a linha "if(isSame(in, inOld)){" funciona normalmente. Mas eu gostaria de testar antes de gravar. E o método isSame() esta trazendo o resultado certo quando usado, mas o arquivo fica vazio. Porque isso ?
Obrigado