Byte [] e java.lang.OutOfMemoryError lendo o arquivo por bits
Estou tentando escrever um leitor que lê arquivos por bits, mas tenho um problema com arquivos grandes. Tentei ler um arquivo com 100 mb e demorou mais de 3 minutos, mas funciono
No entanto, então eu tentei arquivo com 500 mb, mas nem sequer começou. Devido a esta linha:
byte[] fileBits = new byte[len];
Agora estou procurando por suluções e não consigo encontrar nenhuma. Talvez alguém tenha resolvido o problema e possa compartilhar algum código, dicas ou idéia.
if (file.length() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("File is too large: " + file.length());
}
int len = (int) file.length();
FileInputStream inputStream = new FileInputStream(file);
try {
byte[] fileBits = new byte[len];
for (int pos = 0; pos < len;) {
int n = inputStream.read(fileBits, pos, len - pos);
if (n < 0) {
throw new EOFException();
}
pos += n;
}
inputStream.read(fileBits, 0, inputStream.available());
inputStream.close();