OK, so, if I understand correctly, you have a very large file, several GB large, full of ints, and these ints are stored in big endian.
My proposal is the following: create a class implementing List<Integer>
over such a large file, and use Collections.sort()
.
That is:
- you take as an argument the file you need sorted;
- you map that file in memory;
- you implement the
List
interface;
- you let
Collections.sort()
do the job.
Once Collections.sort()
is done, you just .force()
the channel from which you obtained the MappedByteBuffer
. If no I/O error, you're good to go, replace the original file.
Yeah, that is quite some work, but it is doable. Demo code on demand.
NOTE: FileChannel.map()
is limited; you may have to create several mappings. I'd recommend splitting into 1 GB mappings. This will quite complicate the implementation of .subList()
, admittedly. Also, be aware that you'll only be able to address up to Integer.MAX_VALUE
ints; which means your file cannot be larger than ((1 << 31 - 1) << 2)
. 1 << 33 is 8 GB.