FileChannel retorna o tamanho incorreto do arquivo na pasta de ativos
Estou tentando ler umFile
da pasta bruta em meus ativos usando umFileInputStream
.
É assim que eu crio oFileInputStream
:
AssetManager assetManager = getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
Depois disso, estou tentando ler os dados doFile
como isso:
FileChannel fileChannel = inputStream.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
IntBuffer intBuffer = mappedByteBuffer.asIntBuffer();
int[] array = new int[intBuffer.limit()];
intBuffer.get(array);
inputStream.close();
fileChannel.close();
Mas isso não funciona. Por algum motivofileChannel.size()
retorna um número enorme. Eu tenho um arquivo de teste com exatamente 13 bytes, masfileChannel.size()
retorna 1126498! Além disso, se eu ignorar o tamanho e apenas começar a ler os bytes retornados, não corresponderão ao meu arquivo de teste!
Então, o que está acontecendo aqui? E existe uma maneira de corrigir isso?