Estou criando uma aplicação que retornará alguns arquivos para o cliente. Esses arquivos não estão num diretorio publico então só podem ser acessados via servlet. Para escrever o arquivo, estou usando um recurso bem ignorante assim:
File file = new File("file.tal");
FileInputStream in = new FileInputStream(file);
int length = in.available();
byte[] cache = new byte[length];
in.read(cache);
response.setContentLength(length);
response.setHeader("Content-Disposition", "filename=" + file.getName());
response.getOutputStream().write(cache);
Bom, os principais problemas que vejo são:
:arrow: Arquivos muito grandes serão um problema para escrever tudo de uma vez só.
:arrow: Ainda sobre arquivos muito grandes, eles tomarão muita memoria e um grande acesso aos arquivos pode acabar sendo um problema.
Então, pensei em fazer algo como:
int TAM_IDEAL = xxx;
File file = new File("file.tal");
FileInputStream in = new FileInputStream(file);
int length = in.available() > TAM_IDEAL ? TAM_IDEAL : in.available();
byte[] cache = new byte[length];
response.setContentLength(length);
response.setHeader("Content-Disposition", "filename=" + file.getName());
while(in.read(cache) != -1) {
response.getOutputStream().write(cache);
response.getOutputStream().flush();
}
Então, que valor colocar em TAMANHO_IDEAL?! Isso vai otimizar alguma coisa?! Algum framework que facilite a vida para fazer o que estou querendo?! Estou usando o Tomcat em conjunto com o Apache no Windows 2000 Server.
Até.