1
votes

Is there any specific reason for netty HttpStaticFileServerHandler example to use RandomAccessFile?

2

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 choose FileInputStream instead of RandomAccessFile which also returns a FileChannel. But I cannot get fileLength from FileInputStream which is a required input for DefaultFileRegion. So I think it is necessary to use RandomAccessFile.

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.

0
votes

A RAF is required to support zero-copy which requires a FileChannel which is most commonly acquired from a RAF.