Is there any specific reason for netty HttpStaticFileServerHandler example to use RandomAccessFile?
2 Answers
1
votes
I think it so that they can demonstrate the use of ChunkedFile, which requires a RandomAccessFile. And the reason for that seems to be that ChunkedFile needs to be able to get the file's length.
If I am not using https, I need not use
ChunkedFile. So I can chooseFileInputStreaminstead ofRandomAccessFilewhich also returns aFileChannel. But I cannot get fileLength fromFileInputStreamwhich is a required input forDefaultFileRegion. So I think it is necessary to useRandomAccessFile.
That's roughly correct. However, you could potentially get the file length some other way; e.g. using File.length().
Actually, it doesn't make much difference which way you get the FileChannel and the file length.